最近再用Servlet,但是被各种目录搞得七荤八素,然后用下面的代码理顺了:)
public static void show(HttpServlet servlet,HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("*************** REQUEST PATH");
System.out.println("*************** request.getContextPath(): " + request.getContextPath());
//System.out.println("*************** request.getRealPath(\"/\"): "+request.getRealPath("/"));//这个目前已经画删除线了
System.out.println("*************** servlet.getServletContext().getRealPath(\"/\"): "+servlet.getServletContext().getRealPath("/"));
System.out.println("*************** servlet.getServletContext().getRealPath(\"/WEB-INF\"): "+servlet.getServletContext().getRealPath("/WEB-INF"));
System.out.println("*************** servlet.getServletContext().getRealPath(\"/WEB-INF/pic\"): "+servlet.getServletContext().getRealPath("/WEB-INF/pic"));
System.out.println("*************** request.getPathInfo(): " + request.getPathInfo());
System.out.println("*************** request.getServletPath(): " + request.getServletPath());
System.out.println("*************** request.getRequestURI(): " + request.getRequestURI());
System.out.println("*************** request.getRequestURL(): " + request.getRequestURL());
System.out.println("*************** System.getProperty(\"java.io.tmpdir\"): "+System.getProperty("java.io.tmpdir"));
System.out.println("*************** File.separator: "+File.separator);
}
相信看完就全明白了。