<%=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。参考servlet中的接口:
request.getScheme();
返回的协议名称,默认是http
request.getServerName()
返回的是你浏览器中显示的主机名,你自己试一下就知道了
getServerPort()
获取服务器端口号
request.getContextPath()应该是得到项目的名字,如果项目为根目录,则得到一个"",即空的字条串。如果项目为abc, <%=request.getContextPath()% > 将得到abc,服务器端的路径则会自动加上,<a href="XXXX.jsp"> 是指当前路径下的这个xxx.jsp页面,有时候也可以在head里设置html:base来解决路径的问题,不过用的最多的还是request.getContextPath。
在js文件中得到request.getContextPath()的值,不想在JSP中写太多的Javascript代码:
一种方法是用hidden:
<input type=hidden name=contextPath value=<%= request.getContextPath()>
再就是在本页面的js里面也是可以使用Scriptlet来赋值的:
var a = '<%= request.getContextPath()>'
或者你赋值给一个hidden的控件都是可以的,然后js取,这样js可以不用写在jsp里。
实际应用中,一般用来解决jsp测试和生产环境路径不同的问题:
<%
String appContext = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + appContext;
%>
分享到:
相关推荐
request.getcontextPath()详解
java中request.getcontextPath()的意思.doc
response.sendRedirect(request.getContextPath()+"/admin/login.jsp"); } public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //...
Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
所有的jsp页面中的链接 添加 request.getContextPath() , 确保路径正确使用!! 代码中使用到了重定向等页面跳转,添加 request.getContextPath() , 确保路径正确使用!! dao 层中的接口不要随意修改,如果需要...
所有的jsp页面中的链接 添加 request.getContextPath() , 确保路径正确使用!! 代码中使用到了重定向等页面跳转,添加 request.getContextPath() , 确保路径正确使用!! dao 层中的接口不要随意修改,如果需要...
if (session.getAttribute("person") == null) ... response.sendRedirect(request.getContextPath() + "/login.jsp"); }else{ request.getRequestDispatcher("fall.jsp").forward(request, response); }
本文实例讲述了JSP实现简单的用户登录并...String path = request.getContextPath(); String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”; %>
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD H
var so = new SWFObject("<%=request.getContextPath()%>/js/player/CuPlayerMiniV4.swf","CuPlayerV4","600","410","9","#000000"); so.addParam("allowfullscreen","true"); so.addParam("allowscriptaccess",...
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> ``` 该语句用于获取当前网页的相对路径 例如: 访问...
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML> <html> <head> ...
dwr开发包,常规客户端...<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/TestService.js'> <script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'> ...
String getContextPath() ---web应用的名称 String getQueryString() ---- get提交url地址后的参数字符串 username=zhangsan&password=123 注意:request获得客户机(客户端)的一些信息 request.getRemoteAddr() -...
用户管理系统。语言JAVA public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, ... response.sendRedirect(request.getContextPath()+"/manager.jsp"); }
My97DatePicker日期控件使用引入js脚本目录,注意必须用<%=request.getContextPath() %>来获取项目根目录+脚本目录的方式,否则日期控件会无效!!! <script type="text/javascript" src="<%=request....
NULL 博文链接:https://javachikuang.iteye.com/blog/262264
String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 的详解
response.sendRedirect(request.getContextPath()); }else{ chain.doFilter(request, response); } } web.xml配置: <filter-name>accessFilter <filter-class>com.loginFilter</filter-class> ...
JSP的request对象实例详解 一 request对象定义 客户端的请求信息被封装在request对象中,通过它才能了解客户的需求,然后做出响应。它是HttpServletRequest类的实例。request对象具有请求域,即完成客户端的请求...