JSP与Servlet的跳转及得到路径方法整理

今天整理项目的流程,在JSP和Servlet之间跳来跳去,曾经一段时间,我都是把Servlet路径定义为“/SomeServlet”,也即定义为根目录,因为兼容性比较好,但是用了MyEclipse之后,新建的Servlet默认路径是“/servlet/SomeServlet”,这样写便于管理,另外就是更适合单独为Servlet设置Filter(关于Filter的设置可以参考这篇文章)。而我的JSP文件目前是放在项目的根目录,也即形成下图这样的路径结构:

/ProjectRoot/
  |--servlet/
  |  |--Servlet1
  |  |--Servlet2
  |
  |--myJsp1.jsp
  |--myJsp2.jsp

其中Servlet跳转有两种方式:
1、sendRedirect()方式

response.sendRedirect(String targetUrl);

2、RequestDispather方式

RequestDispatcher requestDispatcher  =  request.getRequestDispatcher(String targetUrl);
requestDispatcher.forward(request, response);


第一种方式是给用户浏览器发送通知,然后由浏览器再给服务器发送跳转请求,所以比较类似用户自己去点URL的跳转,这种方式如果需要传参给跳转页面,需要使用Session或者使用GET方式将参数显式的写在targetUrl里(如:ooxx.jsp?id=1),而且大部分情况下由于GET方法的局限性,这种跳转方式只能带较为简单的参数。

而第二种方式有点类似C#中的Server.Transfer()方法,即服务器端跳转,从现象上看就是用户的浏览器内容发生了变化,但是浏览器的地址栏不变还是老地址。这种方式由服务器直接控制request及response的走向及参数,从命令行的参数上就可以看出这一点。这样方便程序员控制参数的传递,几乎可以传递任何类型的参数,只要简单的使用setAttribute()方法即可:

request.setAttribute(String attriName, Object attriValue);


但是也就是因为它是服务器端跳转,所以用户浏览器的地址栏是不发生变化的。那么,如果项目路径结构如上图所示的情况,那么:
1、从JSP跳转向Servlet时
只要简单的使用相对路径“serlvet/SomeServlet”即可。

2、从Servlet跳转向另一个Servlet时
因为Servlet都在相同路径下,所以可以直接写相对路径,如“./SomeServlet”或直接“SomeServlet”。

3、从Servlet跳转向JSP时
因为Servlet路径为“servlet/SomeServlet”,所以如果要使用RequestDispather方式跳转,JSP页面在接参数时,会将地址栏的地址作为当前目录寻找自己需要的方法、JavaScript、CSS等。所以经常有朋友遇到JavaScript报错“Ext未定义”就是因为JSP页面找不到Ext的js文件。所以这种情况,需要使用绝对路径来告诉JSP去哪里得到这些资源。JAVA有关获得路径的方法较多,测试如下:

项目根目录:http://localhost:8080/TestProject/
JSP测试:http://localhost:8080/TestProject/TestPath.jsp

 1 <% @ page language="java" contentType="text/html; charset=UTF-8"
 2    pageEncoding="UTF-8"
%>
 3 <! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
 4 < html >
 5 < head >
 6 < meta  http-equiv ="Content-Type"  content ="text/html; charset=UTF-8" >
 7 < title > Insert title here </ title >
 8 </ head >
 9 < body >
10 <% = " request.getContextPath() =  "   +  request.getContextPath()  +   " <BR /> " %>
11 <% = " request.getServletPath() =  "   +  request.getServletPath()  +   " <BR /> " %>
12 <% = " request.getRequestURI() =  "   +  request.getRequestURI()  +   " <BR /> " %>
13 <% = " request.getRequestURL() =  "   +  request.getRequestURL()  +   " <BR /> " %>
14 <%
15    String realPath = session.getServletContext().getRealPath("/");
16
%>
17 <% = " request.getRealPath(/ " // " ) =  "   +  realPath  +   "" %>
18 </ body >
19 </ html >

返回结果:

request.getContextPath() = /TestProject
request.getServletPath() = /TestPath.jsp
request.getRequestURI() = /TestProject/TestPath.jsp
request.getRequestURL() = http://localhost:8080/TestProject/TestPath.jsp
request.getRealPath("/") = C:/Tomcat/webapps/TestProject/


Servlet测试

 1 package  servlet;
 2
 3 import  java.io.IOException;
 4 import  java.io.PrintWriter;
 5
 6 import  javax.servlet.ServletException;
 7 import  javax.servlet.http.HttpServlet;
 8 import  javax.servlet.http.HttpServletRequest;
 9 import  javax.servlet.http.HttpServletResponse;
10 import  javax.servlet.http.HttpSession;
11
12 public   class  TestPath  extends  HttpServlet  {
13
14    private static final long serialVersionUID = 3093731648408094325L;
15
16    public void doGet(HttpServletRequest request, HttpServletResponse response)
17            throws ServletException, IOException {
18
19        response.setContentType("text/html");
20        PrintWriter out = response.getWriter();
21        out.println("request.getContextPath() = " + request.getContextPath() + "<BR />");
22        out.println("request.getServletPath() = " + request.getServletPath() + "<BR />");
23        out.println("request.getRequestURI() = " + request.getRequestURI() + "<BR />");
24        out.println("request.getRequestURL() = " + request.getRequestURL() + "<BR />");
25        HttpSession session = request.getSession();
26        String realPath = session.getServletContext().getRealPath("/");
27        out.println("request.getRealPath(/"//") = " + realPath + "");
28        out.flush();
29        out.close();
30    }

31
32    public void doPost(HttpServletRequest request, HttpServletResponse response)
33            throws ServletException, IOException {
34        doGet(request, response);
35    }

36
37}

返回结果:

request.getContextPath() = /TestProject
request.getServletPath() = /servlet/TestPath
request.getRequestURI() = /TestProject/servlet/TestPath
request.getRequestURL() = http://localhost:8080/TestProject/servlet/TestPath
request.getRealPath("/") = C:/Tomcat/webapps/TestProject/


这样就一目了然了,另外要特别说下getRealPath()这个方法,用于得到URL的物理磁盘路径,以前的写法很简单request.getRealPath(String path)即可。但是此方法已被废弃。现在要用ServletContext.getRealPath(String path)。也就是说要先得到ServletContext对象,而这个对象获得方式有好几种,比较简单的无非是从Session中获得:

HttpSession session = request.getSession();
String realPath = session.getServletContext().getRealPath("/");

还有几种方法同样可以获得ServletContext:

Javax.servlet.http.HttpSession.getServletContext()
Javax.servlet.jsp.PageContext.getServletContext()
Javax.servlet.ServletConfig.getServletContext()


以上。

参考资料:
http://hi.baidu.com/fytcm/blog/item/298975d7e796aedaa044df0a.html
http://hi.baidu.com/javagt/blog/item/6b7a68f4ebc3b3d8f2d385e3.html
http://www.blogjava.net/flysky19/articles/98006.html
http://bbs.chinaunix.net/viewthread.php?tid=383861

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值