SoFunction
Updated on 2025-03-07

Introduction to session and usage examples in servlet

HttpServletRequest has two overloaded getSession() methods, one accepts a value of the type of boolean, and the other does not have any parameters. The getSession() method has the same function as the getSession(true) method, that is, if the corresponding client has generated a session, it will return the old session. Otherwise, this method will generate a session ID and be bound to the corresponding client. If getSession(false) means that if the corresponding client already has a corresponding session, it will return the old session, otherwise it will not generate a new session. You can use the isNow() method on the HttpSession object to determine whether this session is newly created.

Common methods of HttpSession

public void setAttribute(String name,Object value)
Bind the value object to the session with name name

public object getAttribute(String name)
Get the attribute value of name, if the attribute does not exist, return null

public void removeAttribute(String name)
Delete the name attribute from the session. If it does not exist, it will not be executed and an error will not be thrown.

public Enumeration getAttributeNames()
Returns the enumeration value related to the session

public void invalidate()
Invalidate the session and delete the attribute object at the same time

public Boolean isNew()
Used to detect whether the current client is a new session

public long getCreationTime()
Return to session creation time

public long getLastAccessedTime()
Returns the time when the web container received the last request from the client during session time

public int getMaxInactiveInterval()
Returns the maximum time requested by a client during the session to seconds

public void setMaxInactiveInterval(int seconds)
Maximum time allowed for client requests

ServletContext getServletContext()
Returns the context environment of the current session, the ServletContext object can enable 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 session


Copy the codeThe code is as follows:

<meta name="keywords" content="keyword1,keyword2,keyword3" />
<meta name="description" content="this is my page" />
<meta name="content-type" content="text/html; charset=UTF-8" />

 <!--    <link rel="stylesheet" type="text/css" href="./">--></pre>
<form action="servlet/saveinfo" method="post">
username:
 <input type="text" name="username" /> <input type="submit" />

password:
 <input type="password" name="userpasswd" />

 </form>
<pre>

</pre>
</div>
<div>

Copy the codeThe code is as follows:

package chap03;

import ;
import ;

import ;
import ;
import ;
import ;
import ;

public class saveinfo extends HttpServlet {

/**
 * Constructor of the object.
 */
 public saveinfo() {
 super();
 }

/**
 * Destruction of the servlet.

 */
 public void destroy() {
 (); // Just puts "destroy" string in log
 // Put your code here
 }

/**
 * The doGet method of the servlet.

 *
 * This method is called when a form has its 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 has entered the user name, put it in the session
 if(("username")!=null);
 {
 HttpSession session = ();
 ("username",("username"));
 }
 ("text/html;charset=GBK");
 PrintWriter out = ();
("session has been created");
 ("
");
("Skip to other <a>pages</a>");

 }

/**
 * The doPost method of the servlet.

 *
 * This method is called when a form has its tag value method 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 the codeThe code is as follows:

package chap03;

import ;
import ;

import ;
import ;
import ;
import ;
import ;
public class getsession extends HttpServlet {

/**
 * Constructor of the object.
 */
 public getsession() {
 super();
 }

/**
 * Destruction of the servlet.

 */
 public void destroy() {
 (); // Just puts "destroy" string in log
 // Put your code here
 }

/**
 * The doGet method of the servlet.

 *
 * This method is called when a form has its 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 {

("text/html;charset=GBK");
 PrintWriter out = ();

 String username = "";
//This is not creating a session, but getting the already created session
 HttpSession session = ();
//If it has been retrieved, it means that it has been logged in
 if(session!=null)
 {
 username = (String)("username");
("Get the created Session");
 ("
");
("Login name:"+username);
 }
 else
 {
 ("../");
 }
 }

/**
 * The doPost method of the servlet.

 *
 * This method is called when a form has its tag value method 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>