Jsp read address orientation

Source: Internet
Author: User

Response. sendRedirect ()
It 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 used to redirect jsp files with frame.
After redirection, the URL of the redirection page will appear in the address bar of the browser.
For example, redirection in servlet
Code content

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 () in the jsp page to directly implement relocation.

 

 

Note:

(1) when response. sendRedirect is used, no HTML output is available.

This is not absolute. The absence of HTML output means that HTML 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). response. sendRedirect should be followed by a 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). Request Dispatcher. forward () is the redirection of control in the container, and the address after the redirection 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, so it carries the marks of the RequestDispatcher. forward () method. If there are a lot of output before <jsp: forward>, and the previous output will automatically output to the client when the buffer is full, this statement will not work, so 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
You can directly modify the address bar to redirect the page.
The jsp file code is as follows:
Code content: 5. When a page is displayed 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:
Code content


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

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.