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即可。
错误:
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即可。