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”
  • 相关阅读:
    .c 文件取为.o文件
    wildcard 处理全部文件
    专家解读Linux操作系统内核中的GCC特性
    Yeoman:适合现代Web应用的现代工作流
    【转】nodejs
    node.js
    2019暑假集训 种树
    2019.6.5 NOIP2014 day2 t2 寻找道路
    2019.6.1 最优贸易
    2019.5.11 海淀区赛之杯子
  • 原文地址:https://www.cnblogs.com/bulrush/p/6651213.html
Copyright © 2011-2022 走看看