关于servlet中的路径的几个函数的例子:
假设有如下请求的路径:
http://192.168.1.191:8083/CitizenWeb/TestServlet?ywdm=01&czmd=02&dd=00030
1. /CitizenWeb
request.getContextPath();
2. TestServlet
request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/") + 1);
3. /CitizenWeb/TestServlet
request.getRequestURI();
4. http://192.168.1.191:8083/CitizenWeb/TestServlet
request.getRequestURL()
5. dd=00030&ywdm=01&czmd=02
getUrl(request)
private String getUrl(HttpServletRequest request)
{
String url = "";
Enumeration param = request.getParameterNames();
while (param.hasMoreElements()) {
String pname = param.nextElement().toString();
if (!pname.equalsIgnoreCase("pagenum") && !pname.equalsIgnoreCase("submit"))
url += pname + "=" + request.getParameter(pname) + "&";
}
if (url.endsWith("&")) {
url = url.substring(0, url.lastIndexOf("&"));
}
return url;
}
6. http://192.168.1.191:8083/CitizenWeb/TestServlet?ywdm=01&czmd=02&dd=00030
request.getRequestURL()+"?"+getUrl(request);