How to calculate the relative path of JSP and study _jsp programming

Source: Internet
Author: User
Problem Description:

How do I calculate a relative path to use a picture in a/jsp/index.jsp file? How do I calculate the relative path after servlet,struts forwarding?

Directory structure:

Application Name: Demo

Demo

----Webroot

----Images

----Go.gif

----JSP

----index.jsp

----CSS

------------------------------------------------------------------------------

The first case: Direct access to JSP files

URL is http://localhost/Context path/jsp/index.jsp

To refer to the Go.gif file in index.jsp:

1, use the path of the decision

/images/go.gif '/>

Browser Search method: Domain name +/context path/images/go.gif, can be found.

2. Use relative path



Browser Search method: Through the Address bar analysis, index.jsp directory (JSP) in the upper level of the directory (WebRoot) under the Images/go.gif file.

3. Use base href

Write <%=request.getcontextpath ()%> too troublesome, you can add the following code at the top of each JSP file

<% String Path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <base href= "<%=basePath%>" >



Browser Search method: BasePath value Http://localhost/Context path/plus images/go.gif, can be found.

------------------------------------------------------------------------------

in the second case: the servlet forwards to the JSP

1. Use relative path

URL is http://localhost/Context path/servlet_2 (forwarded to/jsp/index.jsp)

Error:

According to/jsp/index.jsp path calculation, get

That's right:



Reason:

The index.jsp is saved under the server-side/jsp/index.jsp directory, but the browser does not know the existence of the/jsp/directory by forwarding, because it is not reflected in the address bar. So the server-side/jsp/directory does not have an impact on the relative path

Browser Search by: Analyze Http://localhost/Context path/servlet_2 through the address bar, find Images/go.gif file relative to servlet_2 directory (/)

2. Use relative path

URL is http://localhost/Context path/servlet/ser/ser/servlet_1 (forwarded to/jsp/index.jsp)

"/servlet/ser/ser/servlet_1 is configured in the Web.xml file.

Error:

According to/jsp/index.jsp path calculation, get

That's right:



Reason:

The index.jsp is saved under the server-side/jsp/index.jsp directory, but the browser does not know the existence of the/jsp/directory by forwarding, because it is not reflected in the address bar. So the server-side/jsp/directory does not have an impact on the relative path

Browser search by the Address bar analysis Http://localhost/Context path/servlet/ser/ser/servlet_1, relative to the Servlet_1 directory (Ser) in the previous level of the directory of the upper level directory of the directory ( /) Images/go.gif file

3, use the path of the decision

/images/go.gif '/>

------------------------------------------------------------------------------

Summary:The relative path is the browser through the address bar analysis, and server-side file storage path is not related to the use of servlet,struts forwarded to a JSP file, a JSP in the server storage location is/a/b/c/d/f/g.jsp, but after the servlet, When struts forwards, the browser's address bar may not necessarily be the/a/b/c/d/f/level. So the relative path calculation is based on the browser address bar.

Namespaces can be used in struts2 to ensure that the directory hierarchy in the browser's address bar is consistent with the server-side directory hierarchy, so the programmer calculates the relative path through the server-side directory hierarchy, which is normal in the browser.

But we understand the principle that even if we don't use namespaces, we have a great control.
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.