There are several interfaces to get the path in HttpServletRequest: Getrequesturi/getcontextpath/getservletpath/getpathinfo
These interfaces are different from each other and can be distinguished by this code:
1@WebServlet ("/hello.view")2 Public classFirstservletextendsHttpServlet {3 /*....*/4 protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {5 //TODO auto-generated Method Stub6Response.setcontenttype ("Text/html;charset=utf-8");7PrintWriter out =Response.getwriter ();8String name = Request.getparameter ("name");9Out.println (");TenOut.println ("); OneOut.println ("<title>hello servlet </title>"); AOut.println ("); -Out.println ("<body>"); theOut.println ("<br>requesturi:" +Request.getrequesturi ()); -Out.println ("<br>contextpath:" +Request.getcontextpath ()); -Out.println ("<br>servletpath:" +Request.getservletpath ()); -Out.println ("<br>pathinfo:" +request.getpathinfo ()); +Out.println ("</body>"); -Out.println ("); + out.close (); A } at /*....*/
The name of this project is Firstservlet, and the output after it is run is as follows:
RequestUri:/firstservlet/hello.view, which is equivalent to the full path of the current page, but does not include the parameter information in the URL.
ContextPath:/firstservlet, this is equivalent to the virtual directory name in IIS
Servletpath:/hello.view, this is actually the name of the page.
Pathinfo:null, this is actually requesturi-contextpath-servletpath, that is, if the servlet is in a subdirectory, then the subdirectory name is shown here.
Learn several path information related to java--jsp&servlet--httpservletrequest starting from 0