开发者

How do you access second parameter of a servlet's web.xml file?

开发者 https://www.devze.com 2022-12-24 23:16 出处:网络
Say in my web.xml file, I define a servlet like so: <url-pattern>/MyURL/*</url-pattern> How do i access anyth开发者_开发技巧ing passed in the * in my servlet? I\'m planning to use use t

Say in my web.xml file, I define a servlet like so:

<url-pattern>/MyURL/*</url-pattern>

How do i access anyth开发者_开发技巧ing passed in the * in my servlet? I'm planning to use use this scheme for pretty(-ish) URLs.


The HttpServletRequest#getPathInfo() is exactly for this purpose.

String path = request.getPathInfo();

That's all. No need to substring the servlet path from it as suggested in another answer here. Also see my answer on your other question.


In the HttpServlet's doGet or doPost method you can use the getRequestURI method of the HttpServletRequest object to retrieve the path part of the URL. Since it sounds like you also want to chop off the part of the path that mapped to the serlvet could use the getServletPath method and then do something like this:

String path = request.getRequestURI();
if(path.startsWith(request.getServletPath())) {
    path = path.substring(request.getServletPath().length());
}
0

精彩评论

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

关注公众号