Five Jump Methods of JSP Pages

Five Jump Methods for JSP Pages

1. RequestDispatcher.forward()

  It works on the server side. When forward() is used, the Servlet engine passes the HTTP request 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 uses forward() to redirect to b.jsp. At this time, all the information submitted by the form can be obtained in b.jsp, and the parameters are automatically passed. But forward() cannot redirect to the jsp file with frame, it can be redirected to There is an html file with a frame, and forward() cannot be passed with parameters behind, such as servlet?name=frank, this is not possible, you can pass response.setAttribute("name", name) to the next page in the program.

  After redirection, the URL in the browser address bar 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"); //定向的页面
  rd.forward(request, response);
}
  通常在servlet中使用,不在jsp中使用。

2. response.sendRedirect()

  It works on the user's browser, sendRedirect() can be passed with parameters, such as servlet?name=frank to the next page, and it can be redirected to a different host, sendRedirect() can be redirected with frame. jsp file.

  After redirection, 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 the response is in the jsp page Implicit object, so you can use response.sendRedirect() to directly implement relocation in jsp pages.

  Notice:

  (1) When using response.sendRedirect, there cannot be HTML output in front;

  This is not absolute, no HTML output actually means that no HTML can be sent to the browser. In fact, the current server has a cache mechanism, generally at 8K (I mean JSP SERVER), which means that unless you close the cache, or you use out.flush() to force a refresh, then before using sendRedirect, A small amount of HTML output is also allowed.

  (2) After response.sendRedirect, it should be followed by a return.

  We already know that response.sendRedirect is redirected by the browser, so the actual action will only take place after the page is processed. Now that you have made a turn, what is the meaning of the output after it? And there may be a steering failure due to the output behind.

  Compare:

  (1) Dispatcher.forward() is the redirection of the control right in the container, and the redirected address will not be displayed in the address bar of the client browser;

  (2) response.sendRedirect() is a complete redirect, the browser will get the redirected address and resend the request link. In this way, the link address after the jump can be seen 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, 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 RequestDispatcher, so it bears the imprint of the RequestDispatcher.forward() method.

  If there is a lot of output before, the previous output has filled the buffer and will automatically output to the client, then this statement will not work, this point should be paid special attention.

  Also note: it cannot change the browser address, and refreshing it will result in repeated submissions

4. Modify the Location property of the HTTP header to redirect

  Redirect the page by directly modifying the address bar by setting.

  The jsp file code is as follows:

<%
 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
 String newLocn = "/newpath/jsa.jsp";
 response.setHeader("Location",newLocn);
%>

5. After staying on a page for a few seconds in JSP, automatically redirect to another page

  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 the target.html page. 300 in the code is the refresh delay time, in seconds. targer.html is the target page you want to turn to, if it is this page, it will automatically refresh this page.

  As can be seen from the above, you can use setHeader to automatically redirect to another page after a page stays for a few seconds.

  Key code:
       String content=stayTime+";URL="+URL;
       response.setHeader("REFRESH",content);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989214&siteId=291194637