Servlet 中的 HttpSession 接口
什么是会话?
在网络术语中,会话只是两个系统相互通信的有限时间间隔。这两个系统可以共享客户端-服务器或对等关系。但是,在 Http 协议中,通信的状态是不被维护的。因此,在 http 协议上工作的 Web 应用程序使用了几种不同的技术,包括Session Tracking ,这意味着维护用户的状态(数据),以便识别他/她。
为了在 servlet 中实现会话跟踪,cookie 一直是最常用的技术之一。但是,它们有以下缺点:
- 他们只能保留文本信息。
- 它们依赖于浏览器。因此,如果客户端禁用它们,您的 Web 应用程序将无法使用它们
- 单个 cookie 可以包含不超过 4kb 的信息
如何在Java servlet 中为每个用户创建具有唯一会话 ID 的会话
为此,servlet 提供了一个名为'HttpSession' Interface 的接口。下图解释了 Http Sessions 在 servlet 中是如何工作的:
HttpSession 接口中的方法
Method | Description |
---|---|
public HttpSession getSession() | Gets the HttpSession object. If the request doesn’t have a session associated with it, a new session is created |
public HttpSession getSession(boolean create) | Gets the session associated with the request. If not already present, then a new one is created based on the value of the boolean argument passed into it |
public String getId() | Returns the unique session id |
public long getCreationTime() | It returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT. |
public long getLastAccessedTime() | It returns the time when this session was last accessed, measured in milliseconds since midnight January 1, 1970 GMT. |
public long getLastAccessedTime() | It returns the time when this session was last accessed, measured in milliseconds since midnight January 1, 1970 GMT. |
public void invalidate() | Invalidates the session |
Servlet 中 Http Session 的优点
- 任何类型的对象都可以存储到会话中,可以是文本、数据库、数据集等。
- 会话的使用不依赖于客户端的浏览器。
- 会话是安全和透明的
Http会话的缺点
- 由于会话对象存储在服务器上而导致的性能开销
- 由于数据的序列化和反序列化而产生的开销
使用 HttpServlet 接口进行会话跟踪的示例:在下面的示例中,HttpServlet 类的 setAttribute() 和 getAttribute() 方法用于在一个 servlet 的会话范围内创建一个属性,并从另一个 servlet 的会话范围内获取该属性。
- 索引.html
- 第一的。Java
// The first servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; < div class = "noIdeBtnDiv" > public class First extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { try { /*Declaration of the get method*/ response.setContentType("text/html"); // Setting the content type to text PrintWriter out = response.getWriter(); String n = request.getParameter("userName"); /*Fetching the contents of the userName field from the form*/ out.print("Welcome " + n); // Printing the username HttpSession session = request.getSession(); /* Creating a new session*/ session.setAttribute("uname", n); /*Setting a variable uname containing the value as the fetched username as an attribute of the session which will be shared among different servlets of the application*/ out.print("visit"); // Link to the second servlet out.close(); } catch (Exception e) { System.out.println(e); } } }
- 第二。Java
// The second servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SecondServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) try { /*Declaration of the get method*/ response.setContentType("text/html"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(false); /*Resuming the session created in the previous servlet using the same method that was used to create the session. The boolean parameter 'false' has been passed so that a new session is not created since the session already exists*/ String n = (String)session.getAttribute("uname"); out.print("Hello " + n); out.close(); } catch (Exception e) { System.out.println(e); } } }
- web.xml
s1 First s1 /servlet1 s2 Second s2 /servlet2
输出:
索引.html:
小服务程序1:
小服务程序2: