Get virtual path of the invoked servlet inside the servlet code
If I've something like this:
<servlet>
<display-name>Step</display-name>
<servlet-name>Step</servlet-name>
<servlet-class>com.foo.AServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Step</servlet-name>
<url-pattern>/aservlet/*</url-pattern>
</servlet-mapping>
开发者_开发技巧
and the servlet is invoked by a request of the form /aservlet/ABC
then is there a way to get the value "ABC" in the code? i.e inside the doGet() or doPost() methods of the class AServlet?
THe easiest thing to do is,
String path = request.getPathInfo();
Which returns "/ABC".
public void doGet(HttpServletRequest request, HttpServletResponse response){
String uriRequest = request.getRequestURI();
//parse to obtain only the last part
String uriRequest = uriRequest.substring(uriRequest.lastIndexOf("/")+1);
}
Same thing for doPost()
.
精彩评论