`

jsp/servlet中的相对路径

阅读更多
1.基本概念

绝对路径:

绝对路径就是你的主页上的文件或目录,在硬盘上的真正路径(URL或物理路径)。
例如:
绝对物理路径:C:\xyz\test.txt 代表了test.txt文件的绝对路径。
绝对URL路径: http://www.sun.com/index.htm 代表了一个URL绝对路径。

根路径:

对当前路径而言,相对与某个基准目录的路径。

相对路径:

相对当前根路径时,使用的路径。
例如:使用相对路径时,
"/"代表根路径,"./" 代表当前路径,
"../"代表上级目录。

从上面可以看出:绝对路径 = 根路径+相对路径


2.在JSP/Servlet中的相对路径

假设web应用名称为webapp。
我把以 "/webapp" 开头的路径称为相对绝对路径,简称:绝对路径;
其它的路径,称为相对相对路径,简称:相对路径。(有点绕口)



在使用路径访问一个客户端或服务器端的对象之前,首先看一下服务器端和客户端的根路径地址。


服务器端的根路径地址:

服务器端根路径的地址指的是相对于web应用的地址。
即相对于http://192.168.0.1:8080/webapp/
这个地址是由服务器端解析的。
(不同于前台html/jsp和javascript中的相对地址,他们是由客户端浏览器解析的)

我们在客户端或在服务器端访问或使用,服务器端的资源时,
使用的根路径是:http://192.168.0.1:8080/webapp/

比如servlet中的forward:
request.getRequestDispatcher(address);
因为address是在服务器端解析的,所以,要forward到a.jsp应该这么写:
request.getRequestDispatcher("/user/a.jsp");
这个/是当前web应用webapp的路径,
所以其绝对路径就是:/webapp/user/a.jsp

服务器端所有对象的根路径都是:
http://192.168.0.1:8080/webapp/

客户端的根路径地址 :

客户端根路径的地址是相对于web服务器根目录的地址。
即相对于http://192.168.0.1:8080/
这个地址是由客户端浏览器解析的


比如index.jsp中的form表单的action属性的地址是相对于
服务器根目录(http://192.168.0.1:8080/)的。
使用http://192.168.0.1:8080/webapp/index.jsp
访问index.jsp时,如果提交到a.jsp:
(访问客户端的资源)

方式一:
action="/user/a.jsp"
使用相对路径访问方式,
此时"/"代表根路径:http://192.168.0.1:8080/
结果:404错误


方式二:
action="./user/a.jsp"
使用相对路径访问方式,
此时"./" 代表当前路径:http://192.168.0.1:8080/webapp/
结果:正确

方式三:
action="/webapp/user/a.jsp"
使用绝对路径方式
结果:正确

Javascript也是在客户端解析的,所以其相对路径的使用和form表单一样。


3、客户端使用相对路径访问弊端:

情形之一:
如果客户端的jsp页面是通过action/servlet跳转访问的,
则jsp页面的根路径变为访问action/servlet的路径。
导致在JSP/HTML页面中引用CSS/Javascript/Action等属性时,
不能正确访问。


因此在客户端使用的地址最好是绝对路径


4、在客户端获取绝对路径前缀

如果所有链接都使用手动添加"/webapp"作为前缀,难免有些死板。
在jsp页面中,可以使用<%=request.getContextPath()%>获取
<%=request.getContextPath()%>的值就是:/webapp

EL表达式:${pageContext.request.contextPath}的值也是:/webapp

在项目根目录下创建一个文件:/webapp/varfile/include.jsp
在文件中设置一个变量:
<c:set var="ctx" value="${pageContext.request.contextPath}"  scope="page"  />

在另一个文件中使用:
<%@include file="/webapp/varfile/include.jsp"%>
<link href="${ctx}/resources/style/main.css" rel="stylesheet" type="text/css" />







引用:
http://www.blogjava.net/simie/archive/2007/07/29/133094.html





---------------------------------
服务器端,Servlet的路径,学习:



例如:
String file = request.getRequestURI();
if (request.getQueryString() != null) {
   file += '?' + request.getQueryString();
}
URL reconstructedURL = new URL(request.getScheme(),
                               request.getServerName(),
                               request.getServerPort(),
                               file);
out.println(URL.toString());


Remember the following three points:
1. Request URI = context path + servlet path + path info.
2. Context paths and servlet paths start with a / but do not end with it.
3. HttpServletRequest provides three methods getContextPath(),
    getServletPath() and getPathInfo() to retrieve the context path,
    the servlet path, and the path info, respectively, associated with a request.


Identifying the servlet path
To match a request URI with a servlet, the servlet container follows a simple algorithm.
Once it identifies the context path, if any, it evaluates the remaining part of the
request URI with the servlet mappings specified in the deployment descriptor, in the
following order. If it finds a match at any step, it does not take the next step.

1 The container tries to match the request URI to a servlet mapping. If it finds a
match, the complete request URI (except the context path) is the servlet path. In
this case, the path info is null.
2 It tries to recursively match the longest path by stepping down the request URI
path tree a directory at a time, using the / character as a path separator, and determining
if there is a match with a servlet. If there is a match, the matching part
of the request URI is the servlet path and the remaining part is the path info.
3 If the last node of the request URI contains an extension (.jsp, for example),
the servlet container tries to match it to a servlet that handles requests for the
specified extension. In this case, the complete request URI is the servlet path
and the path info is null.
4 If the container is still unable to find a match, it will forward the request to the
default servlet. If there is no default servlet, it will send an error message indicating
the servlet was not found.





---------
引自:
http://yaodong.yu.blog.163.com/blog/static/121426690200993104353382/?fromdm&fromSearch&isFromSearchEngine=yes













--
  • 大小: 4.6 KB
分享到:
评论

相关推荐

    JSP、Servlet中的相对路径和绝对路径(包括路径问题).mht

    JSP、Servlet中的相对路径和绝对路径(包括路径问题),解决繁琐的路径问题。

    java获取路径的各种方法

    关于JSP/Servlet中的相对路径和绝对路径。2.1服务器端的地址服务器端的相对地址指的是相对于你的web应用的地址,这个地址是在服务器端解析的(不同于html和javascript中的相对地址,他们是由客户端浏览器解析的)

    浅谈Servlet转发到JSP页面的路径问题(必看)

    一、现象与概念 1. 问题 在Servlet转发到JSP页面时,此时浏览器...解决方案:超链接用绝对路径而不是相对路径。若/代表的是站点的根目录,在其前面加上contextPath。 &lt;a&gt;/TestServlet rel=external nofollow &gt;To B

    JSP的相对路径如何计算深入研究

    要在jsp中使用图片,如何计算相对路径?经过Servlet,struts转发后又如何计算相对路径,下面我们带着疑问看看本

    J2ee中的路径问题(word文档)

    jsp和sevlet中的相对路径和绝对路径!以及获取路径的方法!jsp 和 servlet的路径对比!文档很简单实用 !

    jsp servlet 入门学习资料-新手一看就懂

    4.1.2 JSP中的相对路径 4.2 注释 4.3 指令 4.3.1 page指令 4.3.2 include指令 4.3.3 taglib指令 4.4 内置对象 4.5 脚本元素 4.5.1 声明 4.5.2 表达式 4.5.3 脚本代码 4.6 动作 4.6.1 id和scope属性 ...

    JavaWeb开发技术-JSP动作元素.pptx

    JSP动作元素 Java Servlet Jsp XML ...JAVA WEB开发技术 JSP动作元素 语法 描述 jsp:include 在页面被请求的时候引入一个文件 jsp:useBean ...jsp:setProperty ...指定被引入资源的相对路径 指定是否将当前页面的输

    基于JSP+Servlet的房源出租管理系统源码+数据库+项目说明(适合毕业设计和大作业).zip

    【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习...数据库已经配置好图片的相对路径了。 ---

    struts2+spring2+ibates

    5、JSP/Servlet中获得当前应用的相对路径和绝对路径 JSP中获得当前应用的相对路径和绝对路径 根目录所对应的绝对路径:request.getRequestURI() 文件的绝对路径:application.getRealPath(request.getRequestURI...

    网页教程《跟姐姐学JSP》

    3.4.1. 绝对路径与相对路径 3.4.1.1. 相对路径 3.4.1.2. 绝对路径 3.4.2. forward导致找不到图片 4. 四个作用域 4.1. 何为作用域 4.2. 例子:在线列表 5. 结合javabean实现CRUD 5.1. 概念和命名方式 5.2. Read...

    java-servlet-api.doc

    所有的JavaServlet都会直接地或间接地执行javax.servlet.Servlet接口,这样它才能在一个Servlet引擎中运行。Servlet引擎是Web服务器按照JavaServletAPI定制的扩展。Servlet引擎提供网络服务,能够理解MIME请求,并...

    第4章 JSP服务器对象

    (2) 包含文件的路径名一般来说是相对路径,不需要什么端口、协议和域 名。 例如: “error.jsp” 、“/templates/onlinestore.html”、 “/beans/calendar.jsp”等。 (3) 包含文件中不能使用,&lt;/html&gt;,,&lt;/body...

    JSP高级编程

    4.1.2 JSP中的相对路径 4.2 注释 4.3 指令 4.3.1 page指令 4.3.2 include指令 4.3.3 taglib指令 4.4 内置对象 4.5 脚本元素 4.5.1 声明 4.5.2 表达式 4.5.3 脚本代码 4.6 动作 4.6.1 id和...

    jsp从入门到高级编程

    4.1.2 JSP中的相对路径 4.2 注释 4.3 指令 4.3.1 page指令 4.3.2 include指令 4.3.3 taglib指令 4.4 内置对象 4.5 脚本元素 4.5.1 声明 4.5.2 表达式 4.5.3 脚本代码 4.6 动作 4.6.1 id和scope属性 ...

    JSP高级教程

    4.1.2 JSP中的相对路径 4.2 注释 4.3 指令 4.3.1 page指令 4.3.2 include指令 4.3.3 taglib指令 4.4 内置对象 4.5 脚本元素 4.5.1 声明 4.5.2 表达式 4.5.3 脚本代码 4.6 动作 4.6.1 id和scope属性 ...

    fckedit编辑器

    上面文件中两个servlet的映射分别为:/editor/filemanager/browser/default/connectors/jsp/connector 和/editor/filemanager/upload/simpleuploader,需要在两个映射前面加上/FCKeditor, 即改为/FCKeditor/...

    超市管理系统

    相对路径时相对于url(不建议) 统一: /admin/user/* /admin/provider/* ... &lt;url-pattern&gt;/admin/*&lt;/url-pattern&gt; 如何处理登录问题? ①方式1 login.jsp放在WEB-INF /admin/user/toLogin...

    JSP静态导入与动态导入使用详解

     JSP页面第一次被请求时,会被JSP引擎转译成Servlet的Java文件,然后再被编译成字节码文件执行。JSP指令标记为JSP页面转译提供整个页面的相关信息。  include指令用于在JSP页面静态插入一个文件,被插入的文件可以...

Global site tag (gtag.js) - Google Analytics