zoukankan      html  css  js  c++  java
  • JSP与Servlet的跳转及得到路径方法整理(转)

    今天整理项目的流程,在JSP和Servlet之间跳来跳去,曾经一段时间,我都是把Servlet路径定义为“/SomeServlet”,也即定义为根目录,因为兼容性比较好,但是用了MyEclipse之后,新建的Servlet默认路径是“/servlet/SomeServlet”,这样写便于管理,另外就是更适合单独为Servlet设置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文件。

    解决方法:可以访问相对路径"..\\myJsp1.jsp" 。..\\表示上跳一层目录,同理..\\..\\表示上跳两层。

    有时需要使用绝对路径来告诉JSP去哪里得到这些资源。JAVA有关获得路径的方法较多,测试如下:

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

    TestPath.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding
    ="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        
    <head>
            
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            
    <title>Insert title here</title>
        
    </head>
        
    <body>
            
    <%="request.getContextPath() = "
                        
    + request.getContextPath() + "<BR />"%>
            
    <%="request.getServletPath() = "
                        
    + request.getServletPath() + "<BR />"%>
            
    <%="request.getRequestURI() = "
                        
    + request.getRequestURI() + "<BR />"%>
            
    <%="request.getRequestURL() = "
                        
    + request.getRequestURL() + "<BR />"%>
            
    <%
                
    String realPath = session.getServletContext().getRealPath("/");
            
    %>
            
    <%="request.getRealPath(\"/\") = " + realPath + ""%>
        
    </body>
    </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测试

    TestPath.java
    package servlet;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    public class TestPath extends HttpServlet {

        
    private static final long serialVersionUID = 3093731648408094325L;

        
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, IOException {

            response.setContentType(
    "text/html");
            PrintWriter out 
    = response.getWriter();
            out.println(
    "request.getContextPath() = " + request.getContextPath()
                    
    + "<BR />");
            out.println(
    "request.getServletPath() = " + request.getServletPath()
                    
    + "<BR />");
            out.println(
    "request.getRequestURI() = " + request.getRequestURI()
                    
    + "<BR />");
            out.println(
    "request.getRequestURL() = " + request.getRequestURL()
                    
    + "<BR />");
            HttpSession session 
    = request.getSession();
            String realPath 
    = session.getServletContext().getRealPath("/");
            out.println(
    "request.getRealPath(\"/\") = " + realPath + "");
            out.flush();
            out.close();
        }

        
    public void doPost(HttpServletRequest request, HttpServletResponse response)
                
    throws ServletException, IOException {
            doGet(request, response);
        }

    }


    返回结果:

    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://blog.csdn.net/zhaojp0411/archive/2009/04/03/4045339.aspx

    JSP跳转方式与Servlet跳转方式的区别 http://blog.sina.com.cn/s/blog_406127500100cb6n.html

  • 相关阅读:
    7.Flask-上传文件和访问上传的文件
    Python 数字模块
    Django之模板语法
    decimal模块
    python中的计时器:timeit模块
    6.Flask-WTForms
    Django之ORM跨表操作
    公司 邮件 翻译 培训 长难句 16
    公司 邮件 翻译 培训 长难句 15
    公司 邮件 翻译 培训 长难句 14
  • 原文地址:https://www.cnblogs.com/myparamita/p/1500400.html
Copyright © 2011-2022 走看看