Jsp-five ways of page jumping

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. However, 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 at the back, such as servlet?name=frank, this will not work, 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

 
 
  1. publicvoid doPost(HttpServletRequest request,HttpServletResponse response)    
  2. throws ServletException,IOException   
  3. {   
  4.   response.setContentType("text/html; charset=gb2312");   
  5.   ServletContext sc = getServletContext();   
  6.   RequestDispatcher rd = null;   
  7.   rd = sc.getRequestDispatcher( "/index.jsp" );  //Directed page   
  8.   rd.forward(request, response);   
  9. }   

Usually used in servlets, not in 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

 
 
  1. publicvoid doPost(HttpServletRequest request,HttpServletResponse response)    
  2. throws ServletException,IOException   
  3. {   
  4.   response.setContentType("text/html; charset=gb2312");   
  5.   response.sendRedirect("/index.jsp");   
  6. }  

Since the response is an implicit object in the jsp page, you can use response.sendRedirect() to directly realize the relocation in the jsp page.

Notice:

◆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.

◆ 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've made a turn, what's the point of the output after it? And there may be a steering failure due to the output behind.

Compare :

◆ 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;

◆response.sendRedirect() is a complete redirection, and the browser will get the redirection 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

通过设置直接修改地址栏来实现页面的重定向。 jsp文件代码如下:

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

5. JSP中实现在某页面停留若干秒后,自动重定向到另一页面

在html文件中,下面的代码:

<meta http-equiv="refresh" content="300; url=target.jsp">

它的含义:在5分钟之后正在浏览的页面将会自动变为target.html这一页。代码中300为刷新的延迟时间,以秒为单位。targer.html为你想转向的目标页,若为本页则为自动刷新本页。

由上可知,可以通过setHeader来实现某页面停留若干秒后,自动重定向到另一页面。 关键代码:

 
 
  1. String content=stayTime+";URL="+URL;   
  2. response.setHeader("REFRESH",content);  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325859960&siteId=291194637