Jsp

推荐列表 站点导航

当前位置:首页 > 脚本编程 > Jsp >

JSP_jsp跳转getRequestDispatcher()和sendRedirect()的区别,1.request.

来源:网络整理  作者:  发布时间:2020-12-22 11:12
jsp跳转getRequestDispatcher()和sendRedirect()的区别,1.request.getRequestDispatcher()是请求转发,前后页面共享一个request ;response.s...
    public session() {
        super();
    <!--
    <!--

复制代码 代码如下:

<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.xp.cn/"; %>
     * @param response the response send by the server to the client
    }
String name = (String)request.getAttribute("name");  

     * @throws IOException if an error occurred
     * Constructor of the object.
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
            throws ServletException, IOException {

复制代码 代码如下:

     * @throws IOException if an error occurred
     *
My JSP 'index.jsp' starting page
     */

        /*
     */
                //因为前后使用的不是同一个request,但是session可以,因为session会一直存在直到用户关闭浏览器


  用户名:<input type="text" />
response.sendRedirect()是重新定向,前后页面不是一个request。

2.RequestDispatcher.forward()是在服务器端运行;

2. 调用ServletContext.getRequestDispatcher(String url)方法。

    }
    -->
        request.setAttribute("name", username);

如果需要把请求转移到另外一个Web App中的某个地址,可以按下面的做法:
    /**
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.xp.cn/"; %>

复制代码 代码如下:

        // Put your code here
String pwd = (String)request.getAttribute("pwd"); 

    /**
<meta http-equiv="expires" content="0" />
String password = (String)session.getAttribute("password");  
所以RequestDispatcher.forward()对于浏览器来说是“透明的”;
    public void init() throws ServletException {
        super.destroy(); // Just puts "destroy" string in log

而HttpServletResponse.sendRedirect()则不是。

        HttpSession session = request.getSession();
        dis.forward(request, response);

getsession.jsp:

        password = request.getParameter("password");
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
     * @throws ServletException if an error occurs
     * @throws ServletException if an error occurred
     *
    public void destroy() {
     * Initialization of the servlet.

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


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

        RequestDispatcher dis = request.getRequestDispatcher("/getsession.jsp");
  <%   out.print("");   String username = (String)session.getAttribute("username");  
        String password = "";
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
import javax.servlet.ServletException;
     */

  %>
        */

3.ServletContext.getRequestDispatcher(String url)中的url只能使用绝对路径; 而ServletRequest.getRequestDispatcher(String url)中的url可以使用相对路径。因为ServletRequest具有相对路径的概念;而ServletContext对象无次概念。

    /**
        doPost(request, response);
<form action="servlet/session" method="post">
My JSP 'getsession.jsp' starting page
<meta http-equiv="pragma" content="no-cache" />
     * The doPost method of the servlet.
 //如果上面是使用response.sendRedirect的话就不能获取到name和pwd  

     * @param response the response send by the server to the client

     * The doGet method of the servlet.
    /**

eg:ServletContext.getRequestDispatcher(“smserror.jsp”).forward(request,response);

                //这个路径必须是这样写,而不能像上面的request.getRequestDispatcher那样使用相对路径
    }
  密码:<input type="password" />

<meta http-equiv="cache-control" content="no-cache" />
import java.io.IOException;
     * @throws ServletException if an error occurred
    public void doPost(HttpServletRequest request, HttpServletResponse response)
     */
<meta http-equiv="description" content="This is my page" />

1.request.getRequestDispatcher()是请求转发,前后页面共享一个request ;
        session.setAttribute("password", password);
        username = request.getParameter("username");

代码实例:


import javax.servlet.http.HttpServletRequest;
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>


        session.setAttribute("username", username);
HttpServletResponse.sendRedirect()是通过向客户浏览器发送命令来完成.
 out.println("username " + username + " password " +password);
import javax.servlet.http.HttpSession;


     *
<meta http-equiv="cache-control" content="no-cache" />
import javax.servlet.http.HttpServlet;

session.java:
index.jsp:
     * This method is called when a form has its tag value method equals to post.
}
    /**

1. 获得另外一个Web App的ServletConext对象(currentServletContext.getContext(uripath)).


     * This method is called when a form has its tag value method equals to get.

        <link type="text/css" href="https://www.xp.cn/styles.css">
  </form>
        response.sendRedirect("http://localhost:8080/sessiontest/getsession.jsp");
     * @param request the request send by the client to the server
import javax.servlet.http.HttpServletResponse;

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

    -->


 out.println("name " + name + "pwd "+ pwd);   
     *
     * Destruction of the servlet.
     *
    }
        <link type="text/css" href="https://www.xp.cn/styles.css">
  <input type="submit" />

public class session extends HttpServlet {

2.void include(ServletRequest request,ServletResponse response)

     * @param request the request send by the client to the server
                //  而且要是使用response.sendRedirect的话在下面的session.jsp中不能通过request.getAttribute来获取request对象
            throws ServletException, IOException {
    }
     */

import java.io.PrintWriter;
        String username = "";

1.void forward(ServletRequest request,ServletResponse response)

import javax.servlet.RequestDispatcher;
    public void doGet(HttpServletRequest request, HttpServletResponse response)

<meta http-equiv="pragma" content="no-cache" />
        // Put your code here
        request.setAttribute("pwd", password);
<meta http-equiv="expires" content="0" />
<meta http-equiv="description" content="This is my page" />

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/jsp/7099.shtml

最新文章
热门文章
Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

JSP_jsp跳转getRequestDispatcher()和sendRedirect()的区别,1.request.

2020-12-22 编辑:

    public session() {
        super();
    <!--
    <!--

复制代码 代码如下:

<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.xp.cn/"; %>
     * @param response the response send by the server to the client
    }
String name = (String)request.getAttribute("name");  

     * @throws IOException if an error occurred
     * Constructor of the object.
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
            throws ServletException, IOException {

复制代码 代码如下:

     * @throws IOException if an error occurred
     *
My JSP 'index.jsp' starting page
     */

        /*
     */
                //因为前后使用的不是同一个request,但是session可以,因为session会一直存在直到用户关闭浏览器


  用户名:<input type="text" />
response.sendRedirect()是重新定向,前后页面不是一个request。

2.RequestDispatcher.forward()是在服务器端运行;

2. 调用ServletContext.getRequestDispatcher(String url)方法。

    }
    -->
        request.setAttribute("name", username);

如果需要把请求转移到另外一个Web App中的某个地址,可以按下面的做法:
    /**
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.xp.cn/"; %>

复制代码 代码如下:

        // Put your code here
String pwd = (String)request.getAttribute("pwd"); 

    /**
<meta http-equiv="expires" content="0" />
String password = (String)session.getAttribute("password");  
所以RequestDispatcher.forward()对于浏览器来说是“透明的”;
    public void init() throws ServletException {
        super.destroy(); // Just puts "destroy" string in log

而HttpServletResponse.sendRedirect()则不是。

        HttpSession session = request.getSession();
        dis.forward(request, response);

getsession.jsp:

        password = request.getParameter("password");
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
     * @throws ServletException if an error occurs
     * @throws ServletException if an error occurred
     *
    public void destroy() {
     * Initialization of the servlet.

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


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

        RequestDispatcher dis = request.getRequestDispatcher("/getsession.jsp");
  <%   out.print("");   String username = (String)session.getAttribute("username");  
        String password = "";
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
import javax.servlet.ServletException;
     */

  %>
        */

3.ServletContext.getRequestDispatcher(String url)中的url只能使用绝对路径; 而ServletRequest.getRequestDispatcher(String url)中的url可以使用相对路径。因为ServletRequest具有相对路径的概念;而ServletContext对象无次概念。

    /**
        doPost(request, response);
<form action="servlet/session" method="post">
My JSP 'getsession.jsp' starting page
<meta http-equiv="pragma" content="no-cache" />
     * The doPost method of the servlet.
 //如果上面是使用response.sendRedirect的话就不能获取到name和pwd  

     * @param response the response send by the server to the client

     * The doGet method of the servlet.
    /**

eg:ServletContext.getRequestDispatcher(“smserror.jsp”).forward(request,response);

                //这个路径必须是这样写,而不能像上面的request.getRequestDispatcher那样使用相对路径
    }
  密码:<input type="password" />

<meta http-equiv="cache-control" content="no-cache" />
import java.io.IOException;
     * @throws ServletException if an error occurred
    public void doPost(HttpServletRequest request, HttpServletResponse response)
     */
<meta http-equiv="description" content="This is my page" />

1.request.getRequestDispatcher()是请求转发,前后页面共享一个request ;
        session.setAttribute("password", password);
        username = request.getParameter("username");

代码实例:


import javax.servlet.http.HttpServletRequest;
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>


        session.setAttribute("username", username);
HttpServletResponse.sendRedirect()是通过向客户浏览器发送命令来完成.
 out.println("username " + username + " password " +password);
import javax.servlet.http.HttpSession;


     *
<meta http-equiv="cache-control" content="no-cache" />
import javax.servlet.http.HttpServlet;

session.java:
index.jsp:
     * This method is called when a form has its tag value method equals to post.
}
    /**

1. 获得另外一个Web App的ServletConext对象(currentServletContext.getContext(uripath)).


     * This method is called when a form has its tag value method equals to get.

        <link type="text/css" href="https://www.xp.cn/styles.css">
  </form>
        response.sendRedirect("http://localhost:8080/sessiontest/getsession.jsp");
     * @param request the request send by the client to the server
import javax.servlet.http.HttpServletResponse;

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

    -->


 out.println("name " + name + "pwd "+ pwd);   
     *
     * Destruction of the servlet.
     *
    }
        <link type="text/css" href="https://www.xp.cn/styles.css">
  <input type="submit" />

public class session extends HttpServlet {

2.void include(ServletRequest request,ServletResponse response)

     * @param request the request send by the client to the server
                //  而且要是使用response.sendRedirect的话在下面的session.jsp中不能通过request.getAttribute来获取request对象
            throws ServletException, IOException {
    }
     */

import java.io.PrintWriter;
        String username = "";

1.void forward(ServletRequest request,ServletResponse response)

import javax.servlet.RequestDispatcher;
    public void doGet(HttpServletRequest request, HttpServletResponse response)

<meta http-equiv="pragma" content="no-cache" />
        // Put your code here
        request.setAttribute("pwd", password);
<meta http-equiv="expires" content="0" />
<meta http-equiv="description" content="This is my page" />

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/jsp/7099.shtml

相关文章

风云图片

推荐阅读

返回Jsp频道首页