欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp跳转getRequestDispatcher()和sendRedirect()的区别

程序员文章站 2022-06-29 13:18:29
1.request.getrequestdispatcher()是请求转发,前后页面共享一个request ;response.sendredirect()是重新定向,前后...

1.request.getrequestdispatcher()是请求转发,前后页面共享一个request ;
response.sendredirect()是重新定向,前后页面不是一个request。

2.requestdispatcher.forward()是在服务器端运行;
httpservletresponse.sendredirect()是通过向客户浏览器发送命令来完成.
所以requestdispatcher.forward()对于浏览器来说是“透明的”;
而httpservletresponse.sendredirect()则不是。

3.servletcontext.getrequestdispatcher(string url)中的url只能使用绝对路径; 而servletrequest.getrequestdispatcher(string url)中的url可以使用相对路径。因为servletrequest具有相对路径的概念;而servletcontext对象无次概念。

requestdispatcher对象从客户端获取请求request,并把它们传递给服务器上的servlet,html或jsp。它有两个方法:

1.void forward(servletrequest request,servletresponse response)

用来传递request的,可以一个servlet接收request请求,另一个servlet用这个request请 求来产生response。request传递的请求,response是客户端返回的信息。forward要在response到达客户端之前调用,也 就是 before response body output has been flushed。如果不是的话,它会报出异常。

2.void include(servletrequest request,servletresponse response)

用来记录保留request和response,以后不能再修改response里表示状态的信息。

如果需要把请求转移到另外一个web app中的某个地址,可以按下面的做法:
1. 获得另外一个web app的servletconext对象(currentservletcontext.getcontext(uripath)).

2. 调用servletcontext.getrequestdispatcher(string url)方法。

eg:servletcontext.getrequestdispatcher(“smserror.jsp”).forward(request,response);

代码实例:
index.jsp:

复制代码 代码如下:

<%@ page language="java" import="java.util.*" pageencoding="gbk"%>
<% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %>

my jsp 'index.jsp' starting page
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="this is my page" />
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<form action="servlet/session" method="post">
  用户名:<input type="text" name="username" />

  密码:<input type="password" name="password" />

  <input type="submit" />
  </form>

session.java:

复制代码 代码如下:

import java.io.ioexception;
import java.io.printwriter;

import javax.servlet.requestdispatcher;
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 session extends httpservlet {

    /**
     * constructor of the object.
     */
    public session() {
        super();
    }

    /**
     * destruction of the servlet.

     */
    public void destroy() {
        super.destroy(); // just puts "destroy" string in log
        // put your code here
    }

    /**
     * the doget method of the servlet.

     *
     * this method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws servletexception if an error occurred
     * @throws ioexception if an error occurred
     */
    public void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        dopost(request, response);
    }

    /**
     * the dopost method of the servlet.

     *
     * this method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws servletexception if an error occurred
     * @throws ioexception if an error occurred
     */
    public void dopost(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        string username = "";
        string password = "";
        username = request.getparameter("username");
        password = request.getparameter("password");
        httpsession session = request.getsession();
        session.setattribute("username", username);
        session.setattribute("password", password);
        request.setattribute("name", username);
        request.setattribute("pwd", password);

        requestdispatcher dis = request.getrequestdispatcher("/getsession.jsp");
        dis.forward(request, response);

        /*
        response.sendredirect("http://localhost:8080/sessiontest/getsession.jsp");
        */
                //这个路径必须是这样写,而不能像上面的request.getrequestdispatcher那样使用相对路径
                //  而且要是使用response.sendredirect的话在下面的session.jsp中不能通过request.getattribute来获取request对象
                //因为前后使用的不是同一个request,但是session可以,因为session会一直存在直到用户关闭浏览器
    }

    /**
     * initialization of the servlet.

     *
     * @throws servletexception if an error occurs
     */
    public void init() throws servletexception {
        // put your code here
    }

}

getsession.jsp:

复制代码 代码如下:

<%@ page language="java" import="java.util.*" pageencoding="gbk"%>
<% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %>

my jsp 'getsession.jsp' starting page

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="this is my page" />
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  <%   out.print("");   string username = (string)session.getattribute("username");  
string password = (string)session.getattribute("password");  
string name = (string)request.getattribute("name");  
string pwd = (string)request.getattribute("pwd"); 
 out.println("username " + username + " password " +password);
 //如果上面是使用response.sendredirect的话就不能获取到name和pwd  
 out.println("name " + name + "pwd "+ pwd);   
  %>