天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

jsp跳轉getRequestDispatcher()和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);   
  %>

jsp技術jsp跳轉getRequestDispatcher()和sendRedirect()的區別,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 久久视频这有精品63在线国产 | 最近日本MV字幕免费观看视频 | 超碰视频在线 | 国产GV无码A片在线观看 | 亚洲午夜久久久久中文字幕 | 久久精品熟一区二区三区 | 天天操天天干天天透 | 99久久国产综合精品 | 国产午夜小视频 | 精品久久免费视频 | 让人爽到湿的小黄书 | 亚洲一卡二卡三卡四卡无卡麻豆 | 毛片大片免费看 | 91亚洲 欧美 国产 制服 动漫 | 日本性hd| 午夜精品久久久久久久爽牛战 | 男人把女人桶到爽免费看视频 | 在线精品一卡乱码免费 | 亚洲黄色在线 | 成人无码精品一区二区在线观看 | 日欧一片内射VA在线影院 | 国产免费福利在线视频 | 欧美精品做人一级爱免费 | 久青草影院 | 又黄又粗又爽免费观看 | 亚洲薄码区 | 国产东北男同志videos网站 | 在线亚洲精品国产一区麻豆 | 67194成网页发布在线观看 | 哇嘎在线精品视频在线观看 | 国产成人国产在线观看入口 | 亚洲国产精品无码中文字满 | 久久re视频这里精品一本到99 | 国产69精品久久久久乱码免费 | 女人精69xxxxx舒心 | 夜色爽爽爽久久精品日韩 | 亚洲 日韩 欧美 另类 蜜桃 | 夜月视频直播免费观看 | 消息称老熟妇乱视频一区二区 | 国产午夜AV无码无片久久96 | 男生射女生 |