RequestDispatcher
RequestDispatcher
JavaSon712 发表于6个月前
RequestDispatcher
  • 发表于 6个月前
  • 阅读 11
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

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登录

图解二者的区别:

输入图片说明

输入图片说明

标签: Java
共有 人打赏支持
粉丝 7
博文 95
码字总数 21837
×
JavaSon712
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: