Relative and absolute paths in the JSP, servlet

Source: Internet
Author: User
Tags relative client root directory
Js|servlet JSP, relative paths and absolute paths in the servlet


Absolute path and relative path problems in JSP and servlet I've been bothering you for days, and after trying to share some of the experience with you.

Premise: Suppose your HTTP address is HTTP://192.168.0.1/your Web application is WebApp, then your Web application URL is http://192.168.0.1/webapp/

directory structure for Web applications:

webapp/

web-inf/

classes/

lib/

Xml

<servlet-mapping>

<servlet-name>handleservlet</servlet-name>

<url-pattern>/handleservlet</url-pattern> This mapping is relative to the current Web application

</servlet-mapping>

user/

a.jsp

b.jsp

images/

css/

js/



All relative paths are started with "/". For example:/image/a.gif,/user/main.jsp, you know the relative path in HTML is this:

There is an HTML file: a.html, which has the <link href= "One.css" rel= "stylesheet" type= "Text/css", where the href attribute represents the path of the referenced CSS file.

ONE.CSS: Indicates that one.css and A.HMTL are in the same directory

USER/ONE.CSS: Indicates that ONE.CSS is in the subdirectory user of the directory where a.html resides.

.. /ONE.CSS: Indicates that the ONE.CSS is located in the top level of A.HMTL directory,

.. /.. /ONE.CSS: Indicates that the ONE.CSS is located in the upper level of the directory at the A.HMTL level,

./: Represents and A.HMTL the same directory

We call this relative path the HTML relative path

1, server-side address

The relative address on the server side refers to the address of your Web application, which is resolved on the server side (unlike the relative addresses in HTML and JavaScript, They are parsed by the client browser, which means that the relative address in the JSP and servlet at this time should be relative to your Web application, that is, relative to the http://192.168.0.1/webapp/.

The places it uses are:

Forwarder:servlet in Request.getrequestdispatcher (address); This address is resolved on the server side, so You have to forwarder to a.jsp should write this: 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 in JSP ("/rtccp/user/a.jsp");%>

2, the client's address

The relative addresses in all HTML are relative to the HTTP://192.168.0.1/, not the http://192.168.0.1/webapp/.

The address of the form's action attribute in HTML should be relative to HTTP://192.168.0.1/, so if you submit to a.jsp as: action= "/webapp/user/a.jsp" ; submit to Servlet for action= "/webapp/handleservlet"

JavaScript is also parsed on the client side, so the relative path is the same as the form form.

3, site root directory and CSS path issues

We call such a relative path/webapp/... a relative path relative to the site's root directory.

When a CSS is introduced into a JSP, if its relative path is the same as that of the current JSP file, forwarder the JSP in a servlet that is not the same as the path to the JSP, it will find that the CSS style does not work at all. This is because the path of the CSS when forwarding in the servlet is relative to the servlet's relative path rather than the JSP's path. So this is not the way to use this in a JSP: <link href= "One.css" rel= "stylesheet" type= "Text/css" > or <link href= ". /.. /one.css "rel=" stylesheet type= "Text/css" > Similar to Href= "One.css" and ... /.. /ONE.CSS's HTML relative path is relative to the file that references the CSS. Forwarding in the servlet is relative to the servlet's relative path, because the JSP path and servlet path are not the same, so the reference must be wrong.

So this time, to use the site root directory, is relative to the HTTP://192.168.0.1/directory, to "/" start.

Therefore, the above error should be corrected to the relative directory of the site root directory similar to the href= "/webapp/one.css". This makes it possible to use the defined CSS style correctly when the servlet is forwarded and the JSP is relative to the site root.





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.