Servlet:什么是HttpSession?

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
  1. 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容器创建的临时会话。

  2. 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容器更容易识别请求的来源。

  3. 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

Servlet HttpSession的一些重要方法 (Some Important Methods of Servlet HttpSession)

MethodsDescription
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);
    }
}

翻译自: https://www.studytonight.com/servlet/httpsession.php

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值