JavaEE---Servlet会话跟踪之Session

<html>
<head>
<title>Session</title>
</head>
<body>
 <center>
   <form action="servlet/FirstServlet" method="POST">
     <table>
       <tr>
        <td>输入数据:<input type="text" name="count"></td>
        </tr>
     </table>
     <center><input type="submit"></center>
   </form>
 </center>
</body>
</html>
package servlet;

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;
import javax.servlet.http.HttpSession;

public class FirstServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public FirstServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * 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);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        request.setCharacterEncoding("GBK");
        response.setContentType("text/html;charset=GBK");
        PrintWriter out = response.getWriter();
        String str = request.getParameter("count");
        request.setAttribute("request_param", str);
        HttpSession session = request.getSession();
        session.setAttribute("session_param", str);
        out.println("<a href='SecondServlet'>NextPage</a>");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
package servlet;

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;
import javax.servlet.http.HttpSession;

public class SecondServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SecondServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * 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);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
         Object obj = request.getAttribute("request_param");
         String request_param = null;
         if(obj != null){
        	 request_param = obj.toString();
         }else{
        	 request_param = "null";
         }
         HttpSession session = request.getSession();
         Object obj2 = session.getAttribute("session_param");
         String session_param = null;
         if(obj2 != null){
        	 session_param = obj2.toString();
         }else{
        	 session_param = "null";
         }
         response.setContentType("text/html;charset=GBK");
         PrintWriter out = response.getWriter();
         out.println("<h2>请求对象中的参数是:"+request_param+"</h2>");
         out.println("<h2>Session对象中的参数是:"+session_param+"</h2>");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

Session常用的方法如下:

public void setAttribute(String name,Object value);

public object getAttribute(String name);

public void removeAttribute(String name);

public Enumeration getAttributeNames();

public void invalidate();用于销毁Session对象

public Boolean isNew();

public long getCreationTime();

public long getLastAccessedTime();

public int getMaxInactiveInterval(int seconds);允许客户请求的最长时间

ServletContext getServletContext();

public String getId();

其中:

HttpSession session = request.getSession()或者HttpSession session = request.getSession(true);用于获取当前的Session;

前者如果该会话对象不存在则会创建一个新的会话;

getSession(boolean create)返回当前请求的会话,如果当前请求不属于任何会话,而且create为true的话则会创建一个新的会话,否则返回null,此后所有来自同一个对象的请求都属于这个会话,通过它的getSession(false)返回的是当前的会话。

这里需要厘清的是:当前会话的概念:

在应用程序有一个ServletContext的中心上下文;

里面有一个或者多个Session会话,每一个Session对应一个客户端。

在每个Session里面有一个或者多个request。每个request对应于每个Servlet

这也是上面的例子结果是,request_param为null而session_param为用户输入的字符串(注意设置字符编码)

Session的生命周期为:

1)客户端向服务器第一次发送请求的时候,request中并没有SessionID

2)此时服务器会创建一个Session对象,并分配一个SessionID。Session对象保存在服务器端,此时为新建的状态,调用session.isNew()方法会返回true

3)当服务器端处理完毕之后,会将SessionID通过response对象传回到客户端,浏览器负责保存到当前的进程中

4)当客户端再次发送请求的时候,会同时将SessionID发送给服务器

5)服务器根据传递过来的SessionID将这次的请求与保存在服务器端的Session对象联系起来,此时Session已经不处于新建状态

6)循环执行过程3-5,一直到Session超时或者被销毁。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值