I did a jsp&servlet path state test that took me a couple of hours to post the results to a shared

Source: Internet
Author: User
Tags include relative
Js|servlet PS: Here is how to paste HTML, so I have to format the text!




Path State test

Objective
Test JSP jumps to each other, servlet jumps the state of the absolute path and relative path under the JSP environment
To find a common solution


Resources

The war package that was made
Mywar

Make up a file
Jsp
a.jsp

b/
b.jsp
ccc.jsp


Servlet

Testservlet.java Mapping:/layer1/layer2/testservlet


Process description
1. Jump directly from a.jsp to b.jsp
Or
Submitted by a.jsp to Testservlet,testservlet Select the corresponding jump method according to A's submission (Forward/sendredirect)
Then jump to b.jsp
2. b.jsp contains a series of hyperlinks, to show the different ways of the jump after the situation of the hyper-chain
/a.jsp
.. /a.jsp
.. /.. /a.jsp
.. /.. /.. /a.jsp

ccc.jsp
/b/ccc.jsp
.. /ccc.jsp
.. /b/ccc.jsp
.. /.. /ccc.jsp
.. /.. /b/ccc.jsp
.. /.. /.. /ccc.jso
.. /.. /.. /b/ccc.jsp

[contextpath]/b/b.jsp



Test results


Phrase substitution

http://localhost:8080/=> ip/
/mywar/=> war/
http://localhost:8080/MyWAR/=> ip/war/
/mywar => [ContextPath] (Request.getcontextpath ())





Jump way directly jump <a. > Servlet/forward servlet/sendredirect
Requestdispatcher.forward Response.sendredirect
---------------------+---------------------+----------------------------------+------------------------
Path to jump in servlet x/b/b.jsp/war/b/b.jsp
---------------------+---------------------+----------------------------------+------------------------
After the Jump browser's address bar ip/war/b/b.jsp Ip/war/layer1/layer2/testservlet ip/war/b/b.jsp
---------------------+---------------------+----------------------------------+------------------------
Link List Browser status bar link address in b.jsp
<a href= "Here's the description"
---------------------+---------------------+----------------------------------+------------------------
/a.jsp ip/a.jsp ip/a.jsp ip/a.jsp
.. /a.jsp ip/war/a.jsp ip/war/layer1/a.jsp ip/war/a.jsp
.. /.. /a.jsp ip/a.jsp ip/war/a.jsp ip/a.jsp
.. /.. /.. /a.jsp ip/a.jsp ip/a.jsp ip/a.jsp

ccc.jsp ip/war/b/ccc.jsp ip/war/layer1/layer2/ccc.jsp ip/war/b/ccc.jsp
/b/ccc.jsp ip/b/ccc.jsp ip/b/ccc.jsp ip/b/ccc.jsp
.. /ccc.jsp ip/war/ccc.jsp ip/war/layer1/ccc.jsp ip/war/ccc.jsp
.. /b/ccc.jsp ip/war/b/ccc.jsp ip/war/layer1/b/ccc.jsp ip/war/b/ccc.jsp
.. /.. /ccc.jsp ip/ccc.jsp ip/war/ccc.jsp ip/ccc.jsp
.. /.. /b/ccc.jsp ip/b/ccc.jsp ip/war/b/ccc.jsp ip/b/ccc.jsp
.. /.. /.. /ccc.jsp ip/ccc.jsp ip/ccc.jsp ip/ccc.jsp
.. /.. /.. /b/ccc.jsp ip/b/ccc.jsp ip/b/ccc.jsp ip/b/ccc.jsp


[Contextpath]/b/b.jsp ip/war/b/b.jsp ip/war/b/b.jsp ip/war/b/b.jsp
---------------------+---------------------+----------------------------------+------------------------




Summarize

As you can see from the list above, only the bottom row is meaningful and it is correct to jump the link address in any way
1. The root of "/" in the JSP is "IP" and does not contain a war path
2. The root of the "/" represented in forward in the servlet is "ip/war/", including the war path
3. The root of the "/" used in Sendredirect in the servlet is IP and does not contain a war path
4. Jump in forward in the servlet, the browser's address bar is still the servlet and does not change
5. In the servlet to jump Sendredirect, the browser's address bar changes to the destination path
6. Relative path determination is based on the path shown in the browser's address bar, rather than the specific directory structure, so
Jump to a page in the same directory using <a href= "filename" >link</a> this way is not permanent
(Do not be surprised to be an ASP, the JSP and servlet mentioned here:))
7. In order to avoid the JSP jumps Jsp,servlet jumps Jsp,forward the way jumps, the sendredirect jumps produces the path question,
For JSP and servlet using Sendredirect jump, use the direct-band
The absolute path of the container path [String request.getcontextpath ()] can be completely resolved, namely:
1) <%
String ContextPath = Request.getcontextpath ();
String url = contextpath + "/user/login.jsp";
%>

<a href= "<%=url%>" > Login</a>
2) .....
String ContextPath = Request.getcontextpath ();
String TargetPath = contextpath + "/user/login.jsp";
RequestDispatcher rd = Request.getrequestdispatcher (TargetPath);
Rd.forward (request, response);
......

For a servlet that uses forward jumps, do not add a container path, otherwise the container path is repeated, for reasons see 2

8. If the use of ".. /"Too much will not be a serious problem," ... /"link up to up to" ip/"
9. In addition, in the use
<jsp:include page = "url" flush= "true"/>
<%@ include file= "url"%>
<%@ page errorpage = "url"%>,
Here the URL uses the "/" to represent the root of "ip/war/", including the war path



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.