Five redirect methods for JSP pages

Source: Internet
Author: User

1. requestdispatcher. forward () is used on the server. When forward () is used, the servlet engine transmits the HTTP request from the current servlet or JSP to another servlet, JSP, or common HTML file, that is, your form is submitted to. JSP, in. JSP uses forward () to redirect to B. JSP. At this time, all information submitted by form is in B. JSP can be obtained, and parameters are automatically transmitted. however, forward () cannot be redirected to a JSP file with a frame. It can be redirected to an HTML file with a frame. At the same time, forward () cannot be passed with parameters after it, such as Servlet? Name = Frank. If this is not the case, you can use response. setattribute ("name", name) in the program to upload it to the next page. The URL in the browser address bar remains unchanged after redirection. Example: redirect public void dopost (httpservletrequest request, httpservletresponse response) in servlet throws servletexception, ioexception {response. setcontenttype ("text/html; charset = gb2312"); servletcontext SC = getservletcontext (); requestdispatcher RD = NULL; RD = SC. getrequestdispatcher ("/index. JSP "); // directed page Rd. forward (request, response);} is usually used in servlet, not in JSP.

 

2. response. sendredirect () works on the user's browser. sendredirect () can be passed with parameters, such as Servlet? Name = Frank is uploaded to the next page, and can be redirected to different hosts. sendredirect () can be redirected to a frame.. after redirection, the URL of the redirection page will appear in the browser address bar. Example: redirect 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 the JSP page, you can use response. sendredirect () is directly relocated.

Note: (1) when response. sendredirect is used, there cannot be HTML output. This is not absolute. html output cannot be sent to a browser. In fact, the current server has a cache mechanism, generally at 8 K (I mean JSP server), which means that unless you disable the cache or you use out. flush () force refresh. A small amount of HTML output is allowed before sendredirect is used.

(2) After response. sendredirect, follow the return statement. We already know that response. sendredirect is switched through a browser, so there will be actual actions only after page processing is complete. Now that you have to turn, what is the significance of the subsequent output? In addition, the redirection may fail due to the subsequent output.

Comparison: (1) dispatcher. forward () is the redirection of control in the container. The redirection address is not 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, the link address after the jump is displayed in 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, to jump to a resource on another server, you must use the httpservletresponse. sendrequest () method.

 

3. <JSP: Forward page = ""/> the underlying part is implemented by requestdispatcher. Therefore, it carries the marks of the requestdispatcher. Forward () method. If there are a lot of outputs before, and the previous output has filled the buffer and will be automatically output to the client, then this statement will not work. Pay special attention to this. Note: The browser address cannot be changed. Refresh will cause repeated submission.

 

4. Modify the location attribute of the HTTP header to 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. after a page is implemented in JSP for several seconds, it is automatically redirected to another page in the HTML file. The following code: <meta http-equiv = "refresh" content = "300; url = target. JSP "> its meaning: after five minutes, the page automatically becomes target.html. In the code, 300 is the refresh delay time, in seconds. Targer.html: The target page you want to switch to. If this page is set, the page is automatically refreshed. As shown in the preceding figure, you can use setheader to automatically redirect a page to another page after several seconds. Key code: String content = staytime + "; url =" + URL; response. setheader ("refresh", content );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.