JSP中获取当前绝对路径

错误:


String realPath = " http:// " + request.getServerName() + " : " + request.getServerPort() + request.getContextPath();
String realPath = " http:// " + request.getServerName() + " : " + request.getServerPort() + request.getServletPath();


正确:


String realPath = " http:// " + request.getServerName() + " : " + request.getServerPort() + request.getContextPath() + request.getServletPath().substring( 0 ,request.getServletPath().lastIndexOf( " / " ) + 1 );

如果想连带获取页面的名称时则去掉最后的substring即可.

你可能感兴趣的:(jsp)