JAVA WEB of the Session object

Session object session

What is the session object:

client and server side of a conversation (request-response)

session used to do?

  • Store or read customer-related information
  • Compared with the request wider role

Why session?

HTTP is a stateless protocol, the client issues a request (request) to the server, the server returns a response (Response), the connection is closed, the relevant information is not retained on the server side connection

How to use the session?

  • Gets the session object ID
    • Each session object unique identifier session ID
  • session object data store
    • Save user data generated during a visit
  • Lifespan session object

Gets session object ID

session.getID(); //获取当前用户session对象的ID

  • Web server automatically creates a session object for each user, and assigns a user ID is different from the other.

save session data

Data Creation

public void setAttribute (String key, Object obj) // Save the parameters specified obj object to the session object, key for the specified keyword (variable names).

session.setAttribute(“username” ,“张三”);

data collection

public Object getAttribute (String key) // get the object of the session key is key.

session.getAttribute(“username” );

Remove the bound data

session.removeAttriubte (“username” );

Get all the critical session object

public Enumeration getAttributeNames( )//产生一个枚举对象,获取session中的各个对象所对应的关键字。

session of the examples

Here Insert Picture Description

  • If it is to redirect the jump page, then when the browser does not automatically save the value of the cookie, session life cycle exists only in a page. Jump page old session object is deleted, a new page will create a new session, resulting in session.getAttribute () Gets the value of all Null. Because the eclipse default built-in browser cookie is not worth saving, so when I first test has been wrong.
  • Later tests on the success of Chrome, Chrome default is to save the cookie, Chrome will not save the cookie set, then the test results with the built-in browser eclipse exactly the same result, would create a new session
  • If it is forwarded jump page, you do not need so much to consider. Forwarding does not remove the request in response (cookie may be automatically saved)
    request.getRequestDispatcher("other.jsp").forward(request, response); //转发

session life cycle

  • When using session needs to determine whether the session 过期, because the session has the lifespan, usually 20-30 minutes, so if you use expired session object appears unexpected error
  • Analyzing method :
    • If the user closes the browser
    • Whether the session object callsinvalidate()方法
    • whether the session object is longest set 发呆时间(????)

A determining lifetime of several methods

 //设置有效期
public boolean isNew()  //session是否为新创建的
 //获取两个请求最大时间间隔
public int getCreationTime()  //获取session创建时间

Second, the session timeout management

  • Returns the last time the client requests related to the session end
getLastAccessedTime()
  • Back in two seconds maximum time interval requests a session
public int getMaxInactiveInterval()
  • Second session to set the validity period of the unit
public void setMaxInactiveInterval(int max Value)
  • Destruction session
    • Although not a long time when the client sends a request to the server, session objects himself, but for some real-time statistics of the number of online sites (eg, chat rooms), and so every time after the session expired before the exact statistics of the number, it is not enough ( ???), so it needs to take the initiative to kill the session.
session.invalidate()

session method of family bucket

public void setAttribute(String Key,Object obj):session对象类似于散列表,可以调用该方法将参数Object指定的
对象obj添加到session对象中,并为其添加的对象指定了一个索引关键字。如果添加的两个对象的关键字相同,
则先前添加的对象被清。
 public Object getAttribute(String Key):获取session对象中含有的关键字是Key的对象,由于任何对象都可以添加
到session对象中,所以在该方法取回对象时应进行强制类型转换。
 public Enumeration getAttributeName():session对象调用该方法产生一个枚举对象.该枚举对象使用nextElement()
方法遍历Session对象所含有的全部对象。
 public long getCreationTime(): Session对象调用该方法要以获取对象创建的时间,单位是毫秒,从197071
日午夜起至该对象创建时刻所走过的毫秒数。
 public long getLastAccessedTime():获取当前session对象最后一次被操作的时间。
 public int getMaxInactiveInterval():得到允许session对象处于非活动状态的最长时间。
 public vodi setMaxInactiveInterval(int n):设置允许session对象处于非活动状态的最长时间。(单位是秒)。
 public void removeAttribute(String Key):从当前session对象中删除关键字是key的对象。
 public String getId():获取session对象的编号。
 invalidate():使Session对象无效。
 long getCreationTime():返回session对象的创建时间
 public String getId():返回session创建时JSP引擎为它设置的唯一ID号。
 long getLastAccessedTime():返回此session中客户端最近一次的请求时间。
 int getMaxInactiveInterval():返回两次请求间隔多长时间此session被取消(ms)。
 5String[] getValueNames():返回一个包含此session中所有可用属性的数组。
 boolean isNew():返回服务器创建的一个session,客户端是否已经加入。
 void setMaxInactiveInterval():设置两次请求间隔多长时间此session被取消(ms)
Published 22 original articles · won praise 0 · Views 152

Guess you like

Origin blog.csdn.net/weixin_42649617/article/details/104849787