文档章节

RequestDispatcher

JavaSon712
 JavaSon712
发布于 2017/06/01 22:03
字数 487
阅读 11
收藏 0

RequestDispatcher只有两个方法:

** public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException;**

public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException;

两个方法都是服务器端的请求跳转,唯一的区别就是forward用户最后接收到的是第二个servletresponse响应,而include方法用户可以同时接收到第一个和第二个servletresponse响应。

上代码:

package handler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

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.sql.DataSource;

/**
 * Servlet implementation class LoginHandler
 */
public class LoginHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginHandler() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		DataSource dataSource = (DataSource) request.getSession().getServletContext().getAttribute("dataSource");
		try {
			Connection connection = dataSource.getConnection();
			String sql = " select count(1) from user where name = ? and password = ?";
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, name);
			statement.setString(2, password);
			ResultSet resultSet = statement.executeQuery();
			resultSet.next();
			int count = resultSet.getInt(1);
			if (count == 1) {
				System.out.println("登录成功");
				RequestDispatcher rd = request.getRequestDispatcher("welcome");
				response.getWriter().append("登录成功\n");
				response.getWriter().append("come from LoginHandler servlet \n");
				rd.forward(request, response);
				/* rd.include(request, response);*/
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

package handler;

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;

/**
 * Servlet implementation class WelcomeServlet
 */
public class WelcomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WelcomeServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		String name=request.getParameter("name");
		PrintWriter pw= response.getWriter();
		pw.append("欢迎"+name+"登录");
		pw.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

web.xml

	<servlet>
		<servlet-name>LoginHandler</servlet-name>
		<display-name>LoginHandler</display-name>
		<description></description>
		<servlet-class>handler.LoginHandler</servlet-class>
	</servlet>
	<servlet>
		<servlet-name>welcome</servlet-name>
		<description></description>
		<servlet-class>handler.WelcomeServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>LoginHandler</servlet-name>
		<url-pattern>/LoginHandler</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>welcome</servlet-name>
		<url-pattern>/welcome</url-pattern>
	</servlet-mapping>

在浏览器中输入:

http://localhost:8080/study_web/LoginHandler?name=liyuhang&password=123321

当使用代码:

rd.forward(request, response);

浏览器的响应如下:

欢迎liyuhang登录

当使用代码:

/* rd.include(request, response);*/

浏览器响应如下:

登录成功 come from LoginHandler servlet 欢迎liyuhang登录

图解二者的区别:

输入图片说明

输入图片说明

© 著作权归作者所有

共有 人打赏支持
JavaSon712
粉丝 11
博文 181
码字总数 31801
作品 0
延安
程序员
私信 提问

暂无文章

计算游戏权重代码

$list = [ [ 'pt' => 1, //权重序号 'pv' => 0, //权重值 ], [ 'pt' => 2, 'pv' => 10, ], [ 'pt' => 3, ......

我才是张先生
12分钟前
5
0
智能分单算法

算法描述 目前在物流,企业用工等领域,都有着大量的通过算法对接到的订单进行智能分配的需求。本文模拟的是用户下订单,然后商家接到订单,由配送人员进行派送的场景。在实际的应用中类似于...

鱼煎
14分钟前
0
0
即将开源 | 2亿用户背后的Flutter应用框架Fish Redux

背景 在闲鱼深度使用 Flutter 开发过程中,我们遇到了业务代码耦合严重,代码可维护性糟糕,如入泥泞。对于闲鱼这样的负责业务场景,我们需要一个统一的应用框架来摆脱当下的开发困境,而这也...

阿里云云栖社区
19分钟前
0
0
Ubuntu上搭建比特币运行环境

Ubuntu版本:16.04.3 Bitcoin Core版本:0.16 1. 比特币运行依赖的开源库 (1)必须依赖的库 库 目的 描述 libssl 加密 随机数生成,椭圆曲线加密算法 libboost 工具 线程库,数据结构等 li...

LoSingSang
24分钟前
1
0
IDE 插件新版本发布,开发效率 “biu” 起来了

近日,Cloud Toolkit正式推出了面向 IntelliJ 和 Eclipse 两个平台的新款插件,本文挑选了其中三个重大特性进行解读,点击文末官网跳转链接,可查看详细的版本说明。 本地应用一键部署到任何...

zhaowei121
29分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部