HttpServletRequest has two overloaded getsession () methods, one that takes a Boolean type, the other with no arguments, and the GetSession () method and the GetSession (true) method function, That is, if the corresponding client has already generated a session, it will return the old one, otherwise this method will produce a session ID and bind to the corresponding client, if GetSession (false) Indicates that if the corresponding client already has a corresponding session, then the old session is returned, otherwise no new session will be generated. You can use the Isnow () method on the HttpSession object to determine whether this session is a new
HttpSession Common methods
public void setattribute (String name,object value)
Bind a value object to a session with a name name
public Object GetAttribute (String name)
Gets the property value of name and returns NULL if the property does not exist
public void RemoveAttribute (String name)
Removes the Name property from the session and does not throw an error if it does not exist.
Public enumeration Getattributenames ()
Returns the and session-related enumeration values
public void Invalidate ()
Invalidates the session while deleting the Property object
Public Boolean isnew ()
Used to detect whether the current customer is a new session
Public long GetCreationTime ()
return session creation Time
Public long Getlastaccessedtime ()
Returns the time that the Web container received the last request from the customer during session time
public int Getmaxinactiveinterval ()
Returns the maximum time that a customer requests during a session is seconds
public void Setmaxinactiveinterval (int seconds)
Maximum time allowed for customer requests
ServletContext Getservletcontext ()
Returns the context of the current session in which the ServletContext object enables the servlet to communicate with the Web container
Public String getId ()
Returns the identification number during a session
A simple example of saving information to session
Sessionlogin.html
Copy Code code as follows:
<meta name= "keywords" content= "keyword1,keyword2,keyword3"/>
<meta name= "description" content= "This are my page"/>
<meta name= "Content-type" content= "text/html; Charset=utf-8 "/>
<!--<link rel= "stylesheet" type= "Text/css" href= "./styles.css" >--></pre>
<form action= "Servlet/saveinfo" method= "POST" >
User name:
<input type= "text" name= "username"/> <input "Submit" type=
Password:
<input type= "Password" name= "userpasswd"/>
</form>
<pre>
</pre>
</div>
<div>
Copy Code code as follows:
Package chap03;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
public class Saveinfo extends HttpServlet {
/**
* constructor of the object.
*/
Public Saveinfo () {
Super ();
}
/**
* Destruction of the servlet.
*/
public void Destroy () {
Super.destroy (); Just puts "destroy" string in log
Put your code here
}
/**
* The Doget method of the servlet.
*
* This is called when a form has it tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response The response send by the server to the client
* @throws servletexception If an error occurred
* @throws IOException If an error occurred
*/
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
If the user enters the user name, it is placed in the session
if (Request.getparameter ("username")!=null);
{
HttpSession session = Request.getsession ();
Session.setattribute ("username", Request.getparameter ("username"));
}
Response.setcontenttype ("TEXT/HTML;CHARSET=GBK");
PrintWriter out = Response.getwriter ();
OUT.PRINTLN ("Session has been created");
Out.println ("
");
Out.println ("Jump to other <a> page </a>");
}
/**
* The DoPost method of the servlet.
*
* This is called when a form, has its tag value, equals to post.
*
* @param request the request send by the client to the server
* @param response The response send by the server to the client
* @throws servletexception If an error occurred
* @throws IOException If an error occurred
*/
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (Request,response);
}
/**
* Initialization of the servlet.
*
* @throws Servletexception If an error occurs
*/
public void Init () throws Servletexception {
Put your code here
}
}</pre>
</div>
<div>
Copy Code code as follows:
Package chap03;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
public class GetSession extends HttpServlet {
/**
* constructor of the object.
*/
Public getsession () {
Super ();
}
/**
* Destruction of the servlet.
*/
public void Destroy () {
Super.destroy (); Just puts "destroy" string in log
Put your code here
}
/**
* The Doget method of the servlet.
*
* This is called when a form has it tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response The response send by the server to the client
* @throws servletexception If an error occurred
* @throws IOException If an error occurred
*/
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("TEXT/HTML;CHARSET=GBK");
PrintWriter out = Response.getwriter ();
String username = "";
This is not to create a session but to take a session that has been created
HttpSession session = Request.getsession ();
If it has been fetched, the description is already logged in
if (session!=null)
{
Username = (String) session.getattribute ("username");
Out.println ("Get created session");
Out.println ("
");
OUT.PRINTLN ("Login name:" +username);
}
Else
{
Response.sendredirect (".. /sessionlogin.html ");
}
}
/**
* The DoPost method of the servlet.
*
* This is called when a form, has its tag value, equals to post.
*
* @param request the request send by the client to the server
* @param response The response send by the server to the client
* @throws servletexception If an error occurred
* @throws IOException If an error occurred
*/
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (Request,response);
}
/**
* Initialization of the servlet.
*
* @throws Servletexception If an error occurs
*/
public void Init () throws Servletexception {
Put your code here
}
}</pre>
</div>
<div></div>
<div>