HttpSession object is used to store entire session with a specific client. We can store, retrieve and remove attribute from HttpSession object. Any servlet can have access to HttpSession object throughout the getSession()
method of the HttpServletRequest object.
HttpSession对象用于存储与特定客户端的整个会话。 我们可以存储,检索和删除HttpSession对象的属性。 任何Servlet都可以通过HttpServletRequest对象的getSession()
方法访问HttpSession对象。
Servlet:HttpSession的工作方式 (Servlet: How HttpSession works)
![how HttpSession works](https://i-blog.csdnimg.cn/blog_migrate/29a9cfe3d27050f4c497d49dd64f6213.png)
On client's first request, the Web Container generates a unique session ID and gives it back to the client with response. This is a temporary session created by web container.
根据客户端的第一个请求, Web容器会生成一个唯一的会话ID,并将其返回给客户端并进行响应。 这是由Web容器创建的临时会话。
The client sends back the session ID with each request. Making it easier for the web container to identify where the request is coming from.
客户端将每个请求发送回会话ID。 使Web容器更容易识别请求的来源。
The Web Container uses this ID, finds the matching session with the ID and associates the session with the request.
Web容器使用该ID,找到具有该ID的匹配会话,并将该会话与请求相关联。
Servlet:HttpSession接口 (Servlet: HttpSession Interface)
![Using HttpSession in Servlet](https://i-blog.csdnimg.cn/blog_migrate/a1b93a02fefd54901f2f6a1431e111dc.png)
Servlet HttpSession的一些重要方法 (Some Important Methods of Servlet HttpSession)
Methods | Description |
---|---|
long getCreationTime() | returns the time when the session was created, measured in milliseconds since midnight January 1, 1970 GMT. |
String getId() | returns a string containing the unique identifier assigned to the session. |
long getLastAccessedTime() | returns the last time the client sent a request associated with the session |
int getMaxInactiveInterval() | returns the maximum time interval, in seconds. |
void invalidate() | destroy the session |
boolean isNew() | returns true if the session is new else false |
void setMaxInactiveInterval(int interval) | Specifies the time, in seconds,after servlet container will invalidate the session. |
方法 | 描述 |
---|---|
long getCreationTime() | 返回创建会话的时间,以格林尼治标准时间1970年1月1日午夜开始的毫秒数为单位。 |
字符串getId() | 返回一个字符串,其中包含分配给会话的唯一标识符。 |
长getLastAccessedTime() | 返回客户端上次发送与会话相关的请求的时间 |
int getMaxInactiveInterval() | 返回最大时间间隔,以秒为单位。 |
无效invalidate() | 破坏会议 |
布尔isNew() | 如果会话是新会话,则返回true,否则返回false |
void setMaxInactiveInterval(int interval) | 指定servlet容器使会话无效之后的时间(以秒为单位)。 |
完整示例演示HttpSession的用法 (Complete Example demonstrating usage of HttpSession)
All the files mentioned below are required for the example.
该示例需要下面提到的所有文件。
index.html
index.html
<form method="post" action="Validate">
User: <input type="text" name="user" /><br/>
Password: <input type="text" name="pass" ><br/>
<input type="submit" value="submit">
</form>
web.xml
web.xml
<web-app..>
<servlet>
<servlet-name>Validate</servlet-name>
<servlet-class>Validate</servlet-class>
</servlet>
<servlet>
<servlet-name>Welcome</servlet-name>
<servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Validate</servlet-name>
<url-pattern>/Validate</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Welcome</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Validate.java
Validate.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Validate extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("user");
String pass = request.getParameter("pass");
if(pass.equals("1234"))
{
//creating a session
HttpSession session = request.getSession();
session.setAttribute("user", name);
response.sendRedirect("Welcome");
}
}
}
Welcome.java
Welcome.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Welcome extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String user = (String)session.getAttribute("user");
out.println("Hello "+user);
}
}