getRequestDispatcher()与sendRedirect()区别(二)

前段时间只是写出了getRequestDispatcher()与sendRedirect()的区别,但是代码并没有怎么实现,今天才想了起来,顺便贴出来我的代码供大家分享,大家可以通过这个传送门来先看看区别然后再看代码就清楚多了getRequestDispatcher()与sendRedirect()区别

代码也很简单

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">
	--></pre>
<form action="servlet/session" method="post">
  用户名:<input type="text" name="username" />

  密码:<input type="password" name="password" />

  <input type="submit" />
  </form>
<pre>

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);    
  %>

由于本人也是新手,最近才开始学习javaweb,要是有什么说的不对的地方还请您不吝赐教,谢谢!

getRequestDispatcher()与sendRedirect()区别(二)》上有1条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注