Introduction and use examples of session in Servlet
HttpServletRequest has two overloaded getsession () methods. One accepts a Boolean value and the other does not take any parameters. The function of the getsession () method is the same as that of the getsession (true) method, that is, if the corresponding client has generated a session, the old session will be returned. Otherwise, This method will generate a session ID and bind it with the corresponding client. If getsession (false) means that if the corresponding client already has a corresponding session, the old session will be returned, otherwise no new session will be generated. You can use the isnow () method on the httpsession object to determine whether the session is new
Httpsession common methods
Public void setAttribute (string name, object value) binds the value object to the session with name name. Public object getattribute (string name) obtains the attribute value of name. If the attribute does not exist, null public void removeattribute (string name) is returned to delete the name attribute from the session. If it does not exist, it will not be executed and errors will not be thrown Public enumeration getattributenames() returns the enumeration value related to the session. Public void invalidate() invalidates the session, At the same time, delete the attribute object public Boolean isnew() to detect whether the current customer is a new session. Public long getcreationtime() returns the session creation time. Public long getlastaccessedtime() returns the time when the web container receives the last request from the customer within the session time. Public int getmaxinactivitinterval() Returns the maximum time requested by the client during the session in seconds. Public void setmaxinactivitinterval (int seconds) allows the client to request the maximum time. ServletContext getservletcontext() returns the context of the current session. The ServletContext object enables the servlet to communicate with the web container. Public string getid() returns the identification number during the session
A simple example of saving information to a session
sessionlogin. html