Relative and absolute paths in the JSP, servlet

Source: Internet
Author: User
Tags relative

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/
web.xml
<servlet-mapping>
<servlet-name>handleservlet</servlet-name>
<url-pattern>/handleservlet</url-pattern>此映射是相对于当前web应用的
</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");%>

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.