Forward request:
The implementation method is:
request.getRequestDispatcher("forwarding path"). forward(request,response);
The forwarding path must be in the same web application
Forwarded source code:
request.getRequestDispatcher("/test.jsp").forward(request, response); //perhaps request.getRequestDispatcher("test.jsp").forward(request, response);
Although the content of the two paths is the same, the concept is not the same. Directly access the test.jsp page from the local without / without
You can go to the test.jsp page with / from the TestForward server
If you need to take some parameters to jsp, you must use the way to jump from the server, that is, request forwarding
Here is an example of request forwarding:
package com.sunkang.req; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** *@Time:2020 10:14:29 am, January 11, 2010 *@Author:783344627@qq.com *Test request forwarding and characteristics */ public class TestForward extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //test System.out.println("TestForward.doGet()"); //Store an attribute in the map set of the request request.setAttribute("name", "Di Ali Gerba"); //Forward the request to test.jsp request.getRequestDispatcher("test.jsp").forward(request, response); //Map set (more than one) Map Map in request object } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>This is jsp</title> </head> <body> <h1>hehe </h1> <% String name = (String)request.getAttribute("name"); out.write("from request Of map Collection fetched data "+name); %> </body> </html>
Redirect:
There are three forms:
1, Orientation in the same web application
2, Orientation in different web applications
3, External web application internal orientation
1, Implementation mode: in the same web application
Direct to the path. Let's assume it's in the same folder: day1
response.sendRedirect("http://localhost/day1/test.jsp");
It can be abbreviated as:
response.sendRedirect("/day/test.jsp");
It can also be further simplified:
response.sendRedirect(" test.jsp");
2, Implementation mode: day1, Day2 are assumed in different web applications
response.sendRedirect("http://localhost/day2/test.jsp");
It can be abbreviated as:
response.sendRedirect("/day2/test.jsp");
3, Implementation mode: external web application
response.sendRedirect("http://www.baidu.com");
Extension content: forward only requests once, and Redirect requests once. The server requests jsp again
The request forwarding address bar does not change, but the redirection address bar does
For request forwarding, we should pay attention to the forwarding process: the browser accesses the server, then the server wraps the request according to the request and forwards it to the jsp file. At this time, the jsp file is equivalent to a response instruction and then returns the data required by the browser. From the beginning to the end, the request sent by the browser to the request forwarded to the jsp is a
request.
Redirection: the main process we want to direct is that the browser sends a request to the server, and the server encapsulates the request to the request. When we need to direct the jsp and other files, we will return a response to the browser. This response is to tell the browser that you need to resend a request to access the jsp. At this time, the browser will send another request. This process includes two requests. When the service It's a response process when the browser is returned by the
Response to return a response (Redirect)
Because redirection is the second point of execution, the first set value will be pass ed