JSP, relative paths in servlets and absolute paths

xiaoxiao2021-03-06  41

JSP, relative paths in servlets and absolute paths

The absolute path in JSP and Servlets and relative path problems have plagued me for several days. After work, they will share them with everyone.

Prerequisites: 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 of the web application:

WebApp /

WEB-INF /

Classes /

LIB /

Web.xml

handleServlet

/ handleservlet This mapping is relative to the current web application

User /

A.jsp

B.Jsp

Images /

CSS /

JS /

All relative paths are started by "/". Such as: /image/a.gif ,/user/main.jsp, you know that the relative path in HTML is like this:

There is an HTML file: A.html, where , where the HREF property represents the path to the referenced CSS file.

One.css: Represents one.css and a.hmtl in the same directory

User / one.css: Represents one.css in the subdirectory user in the directory where A.html is located.

../one.css: Represents one.css in a.hmtl last level directory,

. ../../one.css: Represents one.css located in the previous directory of the A.hmtl last level directory,

Represents and A.hmtl Same Contents

We call the above relative path to HTML relative path

1, the address of the server

The relative address of the server refers to the address relative to your web application, this address is parsed on the server (different from the relative address in HTML and JavaScript, "which is parsed by the client browser) that is, this time The relative address in JSP and Servlet should be relative to your web application, ie relative to http://192.168.0.1/webapp/.

The place where it uses:

Forwarder: Request.getRequestDispatcher (address) in servlet; this address is parsed on the server, so you want Forwarder to A.JSP should write this: Request.getRequestDispatcher ("/ user / a.jsp") this / relative 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 JSP;%>

2, the address of the client

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

The address of the Action property of the Form form in HTML should be relative to http://192.168.0.1/, so if submitted to a.jsp: action = "/ webApp / user / a.jsp"; submit to servlet For Action = "/ WebApp / HandleServlet" JavaScript is also parsed by the client, so its relative path is the same as the FORM form.

3, site root root directory and CSS path problem

We call like this relative path / WebApp / .... For the relative path relative to the site root directory.

When CSS is introduced into the JSP, if its relative path is relative to the current JSP file, while the CSS style is found at all, this CSS style does not work at all. This is because the path of the CSS is the path to the relative path relative to this servlet in the servlet. So this time you can't use this path in JSP: or Similar to href = "one.css" and ../../one.css's HTML relative path is relative path relative to files that reference this CSS. When the servlet is forwarded relative to this servlet, because the JSP path and the servlet path are different, this reference is definitely an error.

So this time, you have to use the site root directory, which is the beginning of "/" relative to http://192.168.0.1/.

Therefore, the above error should be corrected to the relative directories of the site root directory similar to the hREF = "/ WebApp / One.css". This will use the defined CSS style when servlet forward and JSP is relative to the site root directory.

Said so much, I don't know if you know, what questions leave a message, everyone exchanges!

转载请注明原文地址:https://www.9cbs.com/read-79324.html

New Post(0)