Use request.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.
The following example demonstrates it:
URL http://www.host.com/servlets/HelloEcho/extra/info?height=100&width=200
getRequestURI /servlets/HelloEcho/extra/info
getServletPath /servlets/HelloEcho
getPathInfo /extra/info
getQueryString height=100&width=200
This is useful if your form is self-referential; that is, it generates a form which calls itself again.
For example:
out.println(“<FORM METHOD=POST ACTION=\”" + request.encodeURL(req.getServletPath()) + “\”>”);
out.println(“<INPUT NAME=value>”);
out.println(“<INPUT TYPE=submit>”);
out.println(“</FORM>”);
Note: encodeURL() adds session information if necessary.
Filed under: 1 | Leave a Comment »