开发者

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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜