java path

The request.getContextPath() <%=request.getContextPath()%> in jsp
   is to solve the problem of relative path and can return the root path of the site.
But it’s ok if you don’t need it. For example, <a href="<%=request.getContextPath()%>/catalog.jsp">I
can use <a href="catalog.jsp"> directly. These two files It is in the same directory. For
example if you want to generate a file and put it in a directory on the server, you can use request.getContextPath()+/dir to form a complete directory structure!
But in the JSP file, through request. The path obtained by getContextPath() is empty, why?

You have not configured the path attribute in the context, so your project file is in the root directory, which is equivalent to path=""; that is, you directly enter your The server ip will go to your jsp page, not the default page of tomcat; so the string you get through request.getContextPath() is empty; it gets the virtual directory;
if you want to get the actual physical path of the project file , you can pass: <%=request.getRealPath("/")%>, so the page will output: d:/web
request.getScheme();
The returned protocol name, the default is http
request.




Assuming that your web application name is news, you enter the request path in the browser:
http://localhost:8080/news/main/list.jsp
, then execute the following line of code and print the following result:
1. System.out .println(request.getContextPath());
print result: /news
   2, System.out.println(request.getServletPath());
print result: /main/list.jsp
3, System.out.println(request.getRequestURI ());
print result: /news/main/list.jsp
4. System.out.println(request.getRealPath("/"));
print result: F:\Tomcat 6.0\webapps\news\test

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326450401&siteId=291194637