getRealPath和getContextPath

EL中获取项目根路径:${pageContext.request.contextPath}

1、request.getContextPath();    // 应用的web目录的名称 ,例如 : /Test

2、request.getRealPath();   // 获取项目的真实路径参数要以“/”开头  , http://localhost:8080/Test/

request.getRealPath() 这个方法已经不推荐使用了,代替方法是:

request.getSession().getServletContext().getRealPath();

在JSP中可以这样用

  request.getSession().getServletContext().getRealPath("/") 得到站点的绝对地址

在servlet或者struts中还可以这样用

  this.getServletContext().getRealPath("/");

返回的协议名称,默认是http

request.getScheme();



返回的是你浏览器中显示的主机名

request.getServerName();



获取服务器端口号

getServerPort();