JSP创建时的<% String path = request.getContextPath();%>
2017-03-26 20:54
519 查看
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
这个语句用来拼装当前网页的相对路径。
比如,页面内有一个连接,完整的路径应该是 http://localhost:80/mytest/authen/login.jsp
其中http://localhost:80是服务器的基本路径,mytest是当前应用程序的名字,则根路径应该是那么http://localhost:80/mytest/。
有了这个 basePath 以后,我的页面内容的连接,我不想写全路径,我只要写 authen/login.do就可以了。服务器会自动把 <base ...>指定的路径和页面内的相对路径拼装起来,组成完整路径。
如果没有这个 basePath,那么我页面的连链接就必须写全路径,否则服务器会找不到。
request.getSchema()可以返回当前页面使用的协议,就是上面例子中的“http”
request.getServerName()可以返回当前页面所在的服务器的名字,就是上面例子中的“localhost"
request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80,
request.getContextPath()可以返回当前页面所在的应用的名字,就是上面例子中的mytest
这四个拼装起来,就是当前应用的跟路径了
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
这个语句用来拼装当前网页的相对路径。
比如,页面内有一个连接,完整的路径应该是 http://localhost:80/mytest/authen/login.jsp
其中http://localhost:80是服务器的基本路径,mytest是当前应用程序的名字,则根路径应该是那么http://localhost:80/mytest/。
有了这个 basePath 以后,我的页面内容的连接,我不想写全路径,我只要写 authen/login.do就可以了。服务器会自动把 <base ...>指定的路径和页面内的相对路径拼装起来,组成完整路径。
如果没有这个 basePath,那么我页面的连链接就必须写全路径,否则服务器会找不到。
request.getSchema()可以返回当前页面使用的协议,就是上面例子中的“http”
request.getServerName()可以返回当前页面所在的服务器的名字,就是上面例子中的“localhost"
request.getServerPort()可以返回当前页面所在的服务器使用的端口,就是80,
request.getContextPath()可以返回当前页面所在的应用的名字,就是上面例子中的mytest
这四个拼装起来,就是当前应用的跟路径了
相关文章推荐
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- <a href="<%=request.getContextPath()%>/XXXX.jsp"> 和<a href="XXXX.jsp"> 的区别?
- JSP之项目路径问题(${pageContext.request.contextPath},<%=request.getContextPath()%>以及绝对路径获取)
- JSP中语句<% String path = request.getContextPath(); String basePath...的含义
- <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getSe
- js中获取 <%=request.getContextPath()%>
- js中获取 <%=request.getContextPath()%>
- WEBROOT根目录 <%=request.getContextPath()%>
- <%=request.getContextPath()%>的作用
- ${pageContext.request.contextPath}和<%=request.getContextPath()%>
- js中获取 <%=request.getContextPath()%>
- <jsp:include page="${pageContext.request.contextPath}/xxx.jsp"></jsp:include> 引起引入错误
- <%=request.getContextPath()%>的作用
- <%=request.getContextPath()%>
- String path = request.getContextPath(); JSP中的相对Path的设置
- ${pageContext.request.contextPath}和<%=request.getContextPath()%>
- 转载【js中获取 <%=request.getContextPath()%>】
- String path = request.getContextPath(); JSP中的相对Path的设置