Detailed explanation of five JSP page Jump methods

Time:2021-7-30

1. RequestDispatcher.forward()
It works on the server side. When forward () is used, the servlet engine transmits HTTP requests from the current servlet or JSP to another servlet, JSP or ordinary HTML file, that is, your form is submitted to a.jsp. In a.jsp, forward () is used to redirect to b.jsp. At this time, all the information submitted by the form can be obtained in b.jsp, Parameters are automatically passed. However, forward() cannot be redirected to a JSP file with a frame, but can be redirected to an HTML file with a frame. Meanwhile, forward() cannot be passed with parameters, such as servlet? Name = Frank. This does not work. It can be passed to the next page through response.setattribute (“name”, name) in the program.
After resetting, the browser address bar URL remains unchanged.
Example: redirect in Servlet
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
  response.setContentType(“text/html; charset=gb2312”);
  ServletContext sc = getServletContext();
  RequestDispatcher rd = null;
  rd = sc.getRequestDispatcher(“/index.jsp”); // Directed pages
  rd.forward(request, response);
}
It is usually used in servlets, not JSPS.
  2. response.sendRedirect()
It works on the user’s browser side. Sendredirect() can pass parameters, such as servlet? Name = Frank to the next page. At the same time, it can redirect to different hosts. Sendredirect() can redirect JSP files with frame
After resetting, the URL of the redirected page will appear on the browser address bar
Example: redirect in Servlet
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
  response.setContentType(“text/html; charset=gb2312”);
  response.sendRedirect(“/index.jsp”);
}
Because response is an implicit object in a JSP page, you can use response. Sendredirect() to directly implement relocation in a JSP page.
Note:
(1) when response.sendredirect is used, HTML output is not allowed in front;
This is not absolute. No HTML output actually means that no HTML can be sent to the browser. In fact, current servers have a cache mechanism, which is generally 8K (I mean JSP server), which means that unless you turn off the cache or use out. Flush() to force refresh, a small amount of HTML output is allowed before using sendredirect.
(2) response.sendredirect should be followed by a return.
We already know that response.sendredirect does the steering through the browser, so there will be actual actions only after the page processing is completed. Now that you have to make a turn, what’s the meaning of the output? And the steering may fail due to the subsequent output.
Comparison:
(1) dispatcher. Forward() is the change of control in the container, and the address after the change will not be displayed in the address bar of the client browser;
(2) response. Sendredirect() is a complete jump. The browser will get the jump address and resend the request link. In this way, you can see the link address after jump from the address bar of the browser.
The former is more efficient. When the former can meet the needs, try to use the requestdispatcher. Forward () method.
Note: in some cases, for example, if you need to jump to a resource on another server, you must use the httpservletresponse. Sendrequest() method.
  3. <jsp:forward page=”” />
Its underlying part is implemented by the requestdispatcher, so it bears the imprint of the requestdispatcher. Forward () method.
If there are many outputs before, and the previous output has filled the buffer, it will be automatically output to the client, then the statement will not work, which should be paid special attention.
Also note: it cannot change the browser address. Refreshing will lead to repeated submission
4. Modify the location attribute of HTTP header to redirect
Redirect the page by directly modifying the address bar.
The JSP file code is as follows:
<%
 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
 String newLocn = “/newpath/jsa.jsp”;
 response.setHeader(“Location”,newLocn);
%>
5. JSP realizes automatic redirection to another page after a certain page stays for several seconds
In the HTML file, the following code:
<meta http-equiv=”refresh” content=”300; url=target.jsp”>
Its meaning: after 5 minutes, the page you are browsing will automatically become target.html. 300 in the code is the delay time of refresh, in seconds. Targer.html is the target page you want to turn to. If it is this page, it will refresh this page automatically.
It can be seen from the above that you can use setheader to automatically redirect to another page after a page stays for several seconds.
Key codes:
String content=stayTime+”;URL=”+URL;
response.setHeader(“REFRESH”,content);

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]