jsp/servlet中的相对路径

[b][color=brown]1.基本概念[/color][/b]

[b][color=indigo]绝对路径:[/color][/b]

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

[b][color=indigo]根路径:[/color] [/b]

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

[b][color=indigo]相对路径:[/color][/b]

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

[color=brown]从上面可以看出:绝对路径 = 根路径+相对路径[/color]


[b][color=brown]2.在JSP/Servlet中的相对路径[/color][/b]

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


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


[b][color=indigo]服务器端的根路径地址:[/color][/b]

服务器端根路径的地址指的是相对于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/

[b][color=indigo]客户端的根路径地址 :[/color][/b]

客户端根路径的地址是相对于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/
[b]结果:[/b][color=red]404错误[/color]


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

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

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


[b][color=brown]3、客户端使用相对路径访问弊端:[/color][/b]

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


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


[b][color=brown]4、在客户端获取绝对路径前缀[/color][/b]

如果所有链接都使用手动添加"/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" />


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


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

[img]http://dl.iteye.com/upload/attachment/0078/8664/68bbd9b5-8543-38f1-bdd7-6b9a52184f9a.png[/img]

例如:
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.


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


--
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值