Relative and absolute paths in the "Go" jsp

Source: Internet
Author: User

1. First identify two concepts:

Server path: Shape:http://192.168.0.1/ Path

Web application path: The path of Http://192.168.0.1/yourwebapp :

2. A description of the relative path and the absolute path:

In a servlet, "/" represents a directory of Web apps. and the relative representation of the physical path. For example : "./" represents the current directory, ". /"represents the parent directory. This similar representation also belongs to the relative path. This is consistent with how paths are expressed in Linux!

3. About the relative path and absolute path in Jsp/servlet.

3.1 Server-side address

The relative address of the server side refers to the address that is relative to your Web application, which is resolved on the server side (different from the relative addresses in HTML and JavaScript, which are parsed by the client browser), that is, the relative addresses in the JSP and servlet should be phase- For your web app, that is, relative to http://192.168.0.1/webapp/.

Where they are used are:
Request.getrequestdispatcher (address) in Forward:servlet, this address is parsed on the server side, so you forward to a.jsp should write: Request.getrequestdispatcher ("/user/a.jsp") this/relative to the current Web application WebApp, its absolute address is:http://192.168.0.1/webapp/user/a.jsp. Sendredirect: <%response.sendredirect ("/rtccp/user/a.jsp") in the JSP;%>

3.2 Address of the client

The relative addresses in all the HTML pages are relative to the root of the server (HTTP://192.168.0.1/), not http://192.168.0.1/webapp/(with the directory of the Web app under the directory). The address of the Action property of the form form in HTML should be relative to the server root directory (http://192.168.0.1/), so if committed to a.jsp: action= "/webapp/ User/a.jsp "or action=" <%=request.getcontextpath ()% > "/user/a.jsp;
The actiom= "/webapp/handleservlet" JavaScript that is submitted to the servlet is also parsed on the client, so its relative path is the same as the form form.


Therefore, in general, in the Jsp/html page and other references such as css,javascript.action attributes are best added
<%=request.getcontextpath ()%> To ensure that the referenced files belong to a directory in the Web App. In addition, you should try to avoid the use of similar ".", "./", ". /.. /"Similar relative path relative to the location of the file, so that when the file is moved, it is prone to problems.

To put it simply:

All paths are requests for resources from the server. The difference is that this path is on the server side also client parsing!

If you want to write down this path is to be resolved on the server side, then the root of this path should be like http://192.168.0.1/webapp/in this form, that is, the path of WebApp is the root path. For example, in the Struts2 formulation of the Oobbs system (the name of the Web application is: Oobbs):

<action name= "Getforumbyid" class= "forumaction" method= "Getforumbyid" >
<result name= "Input" >/</result>
<result name= "Success" >/site/forumlist.jsp</result>
</action>

The path above is:/site/forumlist.jsp instead of /oobbs/site/forumlist.jsp

And if we write this path in the clinet end to parse, then the path root becomes http://192.168.0.1/, that is, the path of the server is the root path. At this point, if you want to request some resources in the background, such as jsp,action, then you must start from WebApp, write your path!

Like what:

The form in our landing page:

<form action= "/Oobbs/site/j_spring_security_check" method= "POST" >

This path must start with/oobbs!

"Note:" Above reprinted from: http://blog.csdn.net/bluishglc/article/details/5346876

./indicates the contents of your current directory
Like what:
Webroot
/home
home.jsp
/page
top.jsp
left.jsp
/img
Logo.png
/web-inf
/lib
Xml

If your access path is at the level of http://localhost:8080/Project/page

When you http://localhost:8080/Project/page/top.jsp to switch to http://localhost:8080/Project/home/home.jsp,
Then you refer to the home.jsp file in the top.jsp file with a relative path of <a href= ". /home/home.jsp "

Relative and absolute paths in the "Go" jsp

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.