request.getContextPath()使用
2014-02-19 23:06
309 查看
request.getcontextPath() 详解
<%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径。
但不用也可以,比如<a href="<%=request.getContextPath()%>/catalog.jsp">直接用<a href="catalog.jsp">也行,这两个文件是在同一个目录下的。比如你要生成一个文件放在服务器上得一个目录下,你可以使用request.getContextPath()+/dir,组成一个完整得目录结构!
但在jsp文件里,通过request.getContextPath()得到的路径却为空,为什么?
你在context中没有配置path属性,所以你的工程文件就是在根目录下,相当于path="";即是你直接在浏览器中输入你的服务器ip就会到你的jsp页面,而不是tomcat的默认页面;所以你通过request.getContextPath()得到的字符串是为空的;它是获得虚目录的;
如果你想得到工程文件的实际物理路径,可通过:<%=request.getRealPath("/")%>,这样页面就会输出:d:/web
request.getScheme();
返回的协议名称,默认是http
request.getServerName()
返回的是你浏览器中显示的主机名,你自己试一下就知道了
getServerPort()
获取服务器端口号
request.getContextPath()应该是得到项目的名字,如果项目为根目录,则得到一个"",即空的字条串,如果项目为abc, <%=request.getContextPath()% >/ 将得到abc/,服务器端的路径则会自动加上,<a href="xxx.jsp"> 应该就是指当前路径下的这个xxx.jsp页面,有时候也可以在head里设置html:base来解决路径的问题,不过用的最多的还是request.getContextPath。
<%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径。
但不用也可以,比如<a href="<%=request.getContextPath()%>/catalog.jsp">直接用<a href="catalog.jsp">也行,这两个文件是在同一个目录下的。比如你要生成一个文件放在服务器上得一个目录下,你可以使用request.getContextPath()+/dir,组成一个完整得目录结构!
但在jsp文件里,通过request.getContextPath()得到的路径却为空,为什么?
你在context中没有配置path属性,所以你的工程文件就是在根目录下,相当于path="";即是你直接在浏览器中输入你的服务器ip就会到你的jsp页面,而不是tomcat的默认页面;所以你通过request.getContextPath()得到的字符串是为空的;它是获得虚目录的;
如果你想得到工程文件的实际物理路径,可通过:<%=request.getRealPath("/")%>,这样页面就会输出:d:/web
request.getScheme();
返回的协议名称,默认是http
request.getServerName()
返回的是你浏览器中显示的主机名,你自己试一下就知道了
getServerPort()
获取服务器端口号
request.getContextPath()应该是得到项目的名字,如果项目为根目录,则得到一个"",即空的字条串,如果项目为abc, <%=request.getContextPath()% >/ 将得到abc/,服务器端的路径则会自动加上,<a href="xxx.jsp"> 应该就是指当前路径下的这个xxx.jsp页面,有时候也可以在head里设置html:base来解决路径的问题,不过用的最多的还是request.getContextPath。
相关文章推荐
- request.getSchema(),request.getServerName(),request.getServerPort(),request.getContextPath()的含义及使用
- request.getContextPath()使用
- 使用C(JSTL)标签实现不写<%= request.getContextPath() %>
- JSP_request.getContextPath()
- String path = request.getContextPath
- 【转】getContextPath、getServletPath、getRequestURI的区别
- request.getContextPath获取绝对路径
- 网页代码中用<%=request.getContextPath()%>和不用的区别?
- getContextPath、getServletPath、getRequestURI的区别
- getContextPath、getServletPath、getRequestURI的区别
- ${pageContext.request.contextPath}路径使用
- request.getContextPath()用el表达式怎么写,绝对路径:request.getRealPath
- request.getContextPath()
- String cannot be resolved to a type - The method getContextPath() from the type HttpServletRequest r
- <%=request.getContextPath()%>
- ${pageContext.request.contextPath}和<%=request.getContextPath()%>
- request.getRealPath不推荐使用
- getContextPath、getServletPath、getRequestURI的区别
- The method getContextPath() from the type HttpServletRequest refers to the missing type
- getContextPath、getServletPath、getRequestURI的区别