zoukankan      html  css  js  c++  java
  • 请求转发和重定向

    response.sendRedirect("Student.jsp");重定向

    request.getRequestDispatcher("Student.jsp").forward(request, response);请求转发

    区别:1:请求转发:地址栏不发生变化。

          重定向:地址栏发生变化

       2:重定向的request不是同一个。

    public class ListAllStudentsServlet extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            StudentDao sd=new StudentDao();
            List<Student> students=sd.getAll();
            //HttpSession session=request.getSession();
           // session.setAttribute("students",students);
    request.setAttribute("students",students); 这个时候用重定向会发生空指针异常,但是请求转发不会,这是因为request和页面中的request不是同一个,页面中的没有取到值。 response.sendRedirect(
    "Student.jsp"); //request.getRequestDispatcher("Student.jsp").forward(request, response); } }

    <%@page import="com.mvc.entity.Student"%>
    <%@page import="java.util.List"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        
        <% 
            List<Student> stus = (List<Student>)request.getAttribute("students");
        %>
         
        <table border="1" cellpadding="10" cellspacing="0">
        
            <tr>
                <th>FlowId</th>
                <th>Type</th>
                <th>IdCard</th>
                <th>ExamCard</th>
                <th>StudentName</th>
                <th>Location</th>
                <th>Grade</th>
                <th>Delete</th>
            </tr>
            
            <% 
                for(Student student: stus){
            %>
                    <tr>
                        <td><%= student.getFlowId() %></td>
                        <td><%= student.getType() %></td>
                        <td><%= student.getIdCard() %></td>
                        <td><%= student.getExamCard() %></td>
                        <td><%= student.getStudentName() %></td>
                        <td><%= student.getLocation() %></td>
                        <td><%= student.getGrade() %></td>
                        <td><a href="deleteStudent?flowId=<%=student.getFlowId() %>">Delete</a></td>
                    </tr>
            <%        
                }
            %>
        
        </table>
        
    </body>
    </html>

    2.请求次数与请求响应对象

    重定向中有两次请求,而请求转发中只有一次请求。重定向中生成两组不同的请求响应对象,而请求转发过程只有一组,所以可以利用他来传request对象。

    3.传值

    重定向传值只能依靠地址后加?,而请求转发可以使用request对象的attribute方法。

    4.路径参数

    重定向中的路径参数为绝对路径。请求转发中的路径为相对路径。所以在使用重定向的时候经常这样使用:

    Response.sendRedirect(Request.getContenxtPath+"/show");

    onclick="window.location='<%=request.getContextPath()%>/CustomerServlet?customerId=<%=customerId%>'"


    http://greenyouyou.blog.163.com/blog/static/13838814720113791934667/

      • 在浏览器端:“/”表示的是一台WEB服务器,“http://机器IP:8080”
      • 在服务器端(请求转发):“/”表示的是一个WEB服务器端的应用,“http://机器IP:8080/Web应用”
      • 在服务器端(重定向):“/”表示的是一个WEB服务器,“http://机器IP:8080”
  • 相关阅读:
    智器SmartQ T7实体店试用体验
    BI笔记之SSAS库Process的几种方案
    PowerTip of the Day from powershell.com上周汇总(八)
    PowerTip of the Day2010071420100716 summary
    PowerTip of the Day from powershell.com上周汇总(十)
    PowerTip of the Day from powershell.com上周汇总(六)
    重新整理Cellset转Datatable
    自动加密web.config配置节批处理
    与DotNet数据对象结合的自定义数据对象设计 (二) 数据集合与DataTable
    在VS2003中以ClassLibrary工程的方式管理Web工程.
  • 原文地址:https://www.cnblogs.com/bulrush/p/6651213.html
Copyright © 2011-2022 走看看