Wednesday, August 12, 2009

How do I get the name of the currently executing script?

Use req.getRequestURI() or req.getServletPath(). The former returns the path to the script including any extra path information following the name of the servlet; the latter strips the extra path info. For example:

URL http://www.javasoft.com/servlets/HelloServlet/jdata/userinfo?pagetype=s3&pagenum=4
getRequestURI ------------ servlets/HelloServlet/jdata/userinfo
getServletPath ------------ servlets/HelloServlet/
getPathInfo ------------ /jdata/userinfo
getQueryString ------------ pagetype=s3&pagenum=4

No comments:

Post a Comment