session Introduction and usage examples in servlet
- 2021-01-06 00:42:28
- OfStack
HttpServletRequest has two overloaded getSession () method, 1 to accept 1 boolean type of value, the other one without any arguments, getSession () method and getSession function 1 sample (true) method, is that if the corresponding client has produced a session, then it will return the old session, otherwise, this method will produce a session ID and corresponding client binding in 1 case, and if getSession (false) said if the corresponding client has a corresponding session, Then return the old session, otherwise no new session will be generated. You can use the isNow() method on the HttpSession object to determine whether the session is new or not
Common methods of HttpSession
public void setAttribute(String name,Object value)
Bind the value object to the session with the name name
public object getAttribute(String name)
Gets the value of the property for name, or returns null if the property does not exist
public void removeAttribute(String name)
Removing the name attribute from the session will not execute if it is not present and will not throw an error.
public Enumeration getAttributeNames()
Returns the enumerated values associated with the session
public void invalidate()
Invalidates the session while deleting the property object
public Boolean isNew()
Used to check whether the current customer is a new session
public long getCreationTime()
Returns the session creation time
public long getLastAccessedTime()
Returns the time during session time when the web container received the last request from the customer
public int getMaxInactiveInterval()
Returns the maximum duration of a customer request during the session as seconds
public void setMaxInactiveInterval(int seconds)
Allows a client to request the maximum amount of time
ServletContext getServletContext()
Returns the context of the current session. The ServletContext object enables Servlet to communicate with the web container
public String getId()
Returns the identification number for the duration of the session
A simple example of saving information to session
sessionlogin.html
<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="./styles.css">--></pre>
<form action="servlet/saveinfo" method="post">
The user name :
<input type="text" name="username" /> <input type="submit" />
password :
<input type="password" name="userpasswd" />
</form>
<pre>
</pre>
</div>
<div>
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 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 a user name I'll put it in session In the
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 You have created ");
out.println("
");
out.println(" Jump to something else <a> page </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>
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 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 {
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
String username = "";
// This is not creating session I'm going to take what I've already created session
HttpSession session = request.getSession();
// If it has been fetched, it is logged in
if(session!=null)
{
username = (String)session.getAttribute("username");
out.println(" Get the created Session");
out.println("
");
out.println(" Login name :"+username);
}
else
{
response.sendRedirect("../sessionlogin.html");
}
}
/**
* 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>