Path problems in Java, JSP Web projects

Source: Internet
Author: User

Paths in Java Web projects, with relative and absolute paths

What is a relative path? A simple example

We have Project test post address http://localhost:8080/test/

Page qiantai/login.jsp page link <a href= "index.jsp"/>

The final address of the relative addressing link of the JSP page is http://localhost:8080/Test/qiantai/index.jsp

A relative path is a process that first takes the path to the current file, which is http://localhost:8080/test/qiantai/

Then splicing the link to the address index.jsp so the result is as above ... qiantai/index.jsp


In the CSS, JS reference to prevent background forwarding caused by the problem, should try to use the absolute path


The first scenario

JSP page comes with two path parameters path and BasePath

At the beginning of the page, there is this code

<%
String path = Request.getcontextpath (); Get current path such as http://localhost:8080/test/qiantai/

Get the address of this project such as http://localhost:8080/test/assignment to BasePath

String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";

Put the project path BasePath into the PageContext and read it later with the EL expression

Pagecontext.setattribute ("BasePath", basepath);

%>

Link instead <a href="${pagescope.basepath}qiantai/index.jsp">

Thus constituting an absolute path http://localhost:8080/test/qiantai/index.jsp

&NBSP; But there's a downside to this: we have to add in front of each link &NBSP, " ${pagescope.basepath} "&NBSP; &NBSP;&NBSP; &NBSP;

Second scenario

Using the base tag in HTML (recommended)

Introduction to Base :base element can specify the datum of all links in the page

URL By default, links in the page (including the address of the style sheet, script, and image) are relative to the current page's address (that is, the request URL in the browser's address bar). &NBSP, &NBSP;&NBSP;

We can use the href attribute in the <base> tag to set all the "relative base URLs".

<!-- base needs to be put in the head--

<BASE&NBSP; href= " http://localhost:8080/ test/" >   or <base href=" <%=basePath%> "/>

This allows you to use the relative path directly <a href= "index.jsp"/>

after setting the <base>, the relative path is the path in base, and no longer the request path of the browser address ~ ~ ~  

< Span style= "Background-color:rgb (255,255,255)" > We can see the HTML code returned by the JSP, including the   <base  href= " http://localhost:8080/ test/" > content

< Span style= "font-size:12px" > that is, all &NBSP encountered in this HTML file; " relative links (for example:   <a  href= " login.jsp " Span style= "word-wrap:normal; Word-break:normal; line-height:21px; Color:rgb (50,62,50); Font-family:simsun; font-size:14px ">>)", all relative to base 

< Span style= "word-wrap:normal; Word-break:normal; line-height:21px; Color:rgb (50,62,50); Font-family:simsun "> So there's no need to worry about forwarding operations (forward) or   errors caused by different request addresses (i.e.:   http:  Span style= "word-wrap:normal; Word-break:normal; line-height:21px; Color:rgb (50,62,50); Font-family:simsun; font-size:14px ">404 ) ...

can also be very good to avoid due to the forwarding of CSS or JS reference address error problem, that the path of the page is base-based.



Path problems in Java, JSP Web projects

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.