Path and jump in JSP

Source: Internet
Author: User

In JSP (Serlvet), there are two ways to "transform" the page, the first redirect, and the second forwarding:
First, the redirect, it's called the way it is.
Response.sendredirect ("uri");
The feature is that the converted URI address is displayed in the address bar but the data in the previous request cannot be transmitted to the new page #
Redirect is to have the server send a request again #
If the browser sees the sendredirect inside the response, it will resend a request, then the data of the previous request is naturally gone.


Forward:
The implementation is this way:
RequestDispatcher dispatcher=request.getrequestdispatcher ("b.jsp") Dispatcher.forward (Request,response);


It features just the opposite of redirection, the forwarding of the new address will not appear on the browser address bar, but the request information can still be used in the new page #

Roughly the following:


Step 2 and Step 3 are running inside the server, it is transparent to the user (browser), then the new address is definitely not displayed in the Address bar, and the previous request information can be used naturally #

First look at the project frame diagram:


Resource reference

Let's look at a reference to a resource in the JSP first


b.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%> This is the first <br/>    This is the second <br/>    This is the third <br/>   /images/b.jpg "/>  This is fourth <br/>     This is the fifth one <br/>    

When we visit b.jsp directly, the effect is as follows


According to the above figure, we can at least get a conclusion
in JSP, the default root path is not the address of the project, but only the port number of the layer # that is http://ip:port/
Therefore, we in the JSP reference picture, JS and other resources, can be used in two ways
Relative address: Is the first picture of the look #
Absolute address: The address starts with "/" and includes the project name # the way to access the fourth picture, the Tomcat container resolves to This is the fourth one <br/>


Server-side jump OK let's look at the first example:
A.JSP:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%>  <a href=". /testservlet "> go to Page b </a>  


Testservlet.java
    protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { c1/>//    request.getrequestdispatcher ("jsp/b.jsp"). Forward (request, response);           Request.getrequestdispatcher ("/jsp/b.jsp"). Forward (request, response);    
If you use a server jump, then the default path is the current project, so the above two URL methods (regardless of whether the URL before the "/") can be #
(I explain that this default path is not the same as the default path of the JSP mentioned above, which means that at the code level, or in the container, the root path refers to the path of the current project.)
OK, we're jumping to b.jsp.
Look at the effect

Because the server-side jump, address or servlet, and did not change #

So everyone think Http://localhost:8900/PathTest/testServlet at this address (remember is the server-side jump, "b.jsp" This page is transparent to the user, you do not know exactly what you are visiting that page # on the other hand , the address of the servlet is: Http://localhost:8900/PathTest/testServlet, the directory is: http://localhost:8900/PathTest)

At this point, access the picture 1 mode: /images/b.jpg access to the image address is actually: http://localhost:8900/images/b.jpg how can there be photos?

So, in the project, picture one of the relative path of the way is no way to both worlds #

So it's best to use, picture four access way, with absolute path #


Client Jump

Let's change the servlet as follows:

    protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { C2/>response.sendredirect ("/jsp/b.jsp");      

Go to a.jsp, then click on the b page, the effect is as follows:


We replaced the servlet with:

    protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { C5/>response.sendredirect ("jsp/b.jsp");    
Click to go to the B page, the address bar becomes http://localhost:8900/PathTest/jsp/b.jsp

Show picture one with picture four

From the above example, we can draw a conclusion

When the client jumps, its path is not relative to the current item #

So, if you use an absolute path, then you have to add the project name as follows:

    protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { C1/>response.sendredirect (Request.getcontextpath () + "/jsp/b.jsp");    }



Resources:

This paper has a wide range of references

http://cxshun.iteye.com/blog/1123913

This blog is hereby stated and expressed thanks





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Path and jump in JSP

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.