文档章节

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
粉丝 10
博文 148
码字总数 25363
作品 0
延安
程序员

暂无文章

并发编程—Java多线程总结

目录 先了解几个概念 多线程:进程和线程是一对多的关系,一个进程(一个程序),由不同的线程来运行。有共享的空间也有独立的空间。 并行: 同时进行,拿两个cpu来跑同样的程序同样的代码片段...

Java干货分享
9分钟前
0
0
Windows Update真的需要向Linux学习

| 虽然简单地将驱动程序从典型的Windows更新中分离出来可能是一种防止这种情况发生的方法,但是Linux甚至更进一步,让用户能够更好地控制他们可以安装的驱动程序。像Ubuntu和Linux Mint这样的...

Linux就该这么学
10分钟前
0
0
Linux学习-0926

4.5/4.6 磁盘格式化 4.7/4.8 磁盘挂载 4.9 手动增加swap分区 一、磁盘格式化 磁盘进行分区后如果不进行格式化,是无法使用的。 linux系统的文件类型: 可以使用使用以下方式进行查看linux系统...

wxy丶
11分钟前
0
0
elasticsearch安装

elasticsearch安装 一、下载: wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.1.tar.gz 二、解压: tar zxvf elasticsearch-5.6.1.tar.gz 三、改名: ......

小杰java
13分钟前
0
0
Vue router传参 四

传递的方法 //传递<router-link :to="{path:'',query:{id:123}}">产品</router-link>//获取this.$route.query.id 这里可以传params 相当于POST 但 :to里面只能是name query 相当......

大灰狼wow
23分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部