Request.getcontextpath Get Absolute path

Source: Internet
Author: User
Tags server port

Project requirements: All JSP pages must be forwarded by action, not directly in the Address bar link JSP or <a href= "aaa.jsp" ></a>

My application is as follows:

1.LoginFilter Java code   public class loginfilter implements filter {          public void dofilter (servletrequest request,  servletresponse response,                filterchain chain)  throws IOException, ServletException {                        httpservletrequest hrequest =  (httpservletrequest) request;            HttpServletResponse hResponse =  (HttpServletResponse) response;                        string uri = hrequest.getrequesturi ();            system.out.println ("Uri====== "+uri";                       TBussinessInfo info =   (Tbussinessinfo)  hrequest.getsession () . getattribute ("user");                       if (  "/b2cplatform/jsp/phone/login/login.jsp". Equals (URI)  | |   "/b2cplatform/". Equals (URI)                      | |   "/b2cplatform/jsp/phone/login/truntologin.jsp". Equals (URI)  )             {                Chain.dofilter (request, response);           }            else           {                hresponse.sendredirect (Hrequest.getcontextpath ());//Do not allow direct access to JSP pages unless login.jsp page             }       }               public void destroy ()  {}           public void init (filterconfig arg0)  throws servletexception {}   }  

2.web.xml XML Code <!--login filter--> <filter> <filter-name>LoginFilter</filter-name> <filter-class>com.hanpeng.b2c.phone.priv.LoginFilter</filter-class> </filter> < Filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>*.jsp</url-pattern > </filter-mapping>

Ii. other information on the Internet

<%=request.getcontextpath ()%> is the problem of resolving relative paths, and can return to the root path of the site.

<a href= "<%=request.getcontextpath ()%>/xxx.jsp" >//The absolute path is obtained

<a href= "xxx.jsp" >//So the relative path is obtained

<a href= "<%=request.getcontextpath ()%>/xxxx.jsp" > can more effectively prevent the failure of the connection.

Request.getcontextpath () Gets the name of the project, and if the item is the root, gets a "", an empty note string,
If the project is DZJC, <%=request.getcontextpath ()% >/will get dzjc/, and the server-side path will be automatically added,
<a href= "xxx.jsp" > should refer to the Xxx.jsp page under the current path, sometimes also in the head set Html:base
To solve the problem of the path, but the most used or Request.getcontextpath.

El to represent the same functionality with El: ${pagecontext.request.getcontextpath ()}

Request.getscheme ();
The name of the protocol returned, the default is HTTP

Request.getservername ()
Returns the host name displayed in your browser

Getserverport ()
Get Server port number

If you want the actual physical path to the project file, you can pass: <%=request.getrealpath ("/")%>, so that the page will output: D:/web

Now the Request.getrealpath ("") method is deprecated.
can use
Servletcontext.getrealpath (java.lang.String) instead. Request.getsession (). Getservletcontext (). Getrealpath () gets the actual physical path of the engineering file, which is the absolute address

Simple usage:

String Path=request.getcontextpath ();//path =/oa

Request.setattribute ("path", path);

Apply (EL) ${path}/images/xx.jpg in a page (absolute positioning: oa/images/xx.jpg)

Can solve the problem of complex relative path locating failure.

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.