HttpSession for WEB storage objects

Source: Internet
Author: User

HttpSession quick warm-up
HttpSession indicates a session, which is used to maintain the status information of the client user.
When a user opens a browser and accesses a website, the server will allocate a space for the browser in the server's memory. The space is exclusive by the browser, which is the session space, by default, data in this space is stored for 30 minutes. For a browser, different pages can share data in the session space.
You can use the public int getMaxInactiveInterval () method to view the validity period of the newly created session object.
You can specify the session validity period in the following two ways.
1. Add the following configuration in the web. xml configuration file:
<Session-config>
<Session-timeout> 20 </session-timeout>
</Session-config>
2. Create a session object and call the public void setMaxInactiveInterval (int time) method to set the session validity period.
 
When a browser accesses a website, the server assigns a unique session id to the browser to distinguish different browsers (clients ).
 
The Cookie mechanism is the default session management policy. If the cookie function of the web browser is disabled, the session id cannot be obtained.
 
A simple session-based user login status information management Applet
 
1. Create a common web project.
2. Create a package named com. neusoft. httpsession In the src folder, and then create a servlet named HttpSessionTest under the package. The content is as follows:
------------------------------------
Package com. neusoft. httpsession;
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 HttpSessionTest extends HttpServlet {
 
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
This. doPost (request, response );
 
}
 
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Request. setCharacterEncoding ("gb2312 ");
Response. setCharacterEncoding ("gb2312 ");
PrintWriter out = response. getWriter ();
String userName = request. getParameter ("userName ");
String userPassWord = request. getParameter ("userPassWord ");
// No login request or direct request to this page through the login page
If (userName = "" & userPassWord = "")
| (UserName = null & userPassWord = null )){
HttpSession session = request. getSession (true );
// Obtain the user name from the session object
String name = (String) session. getAttribute ("name ");
// If the name is not empty, the user has logged on.
If (name! = Null ){
Out. println (name + "already logged on! ");
} Else {
// If the user name is blank, the logon page is displayed.
Response. sendRedirect ("/Test/index. jsp ");
}
} Else if (userName = "" | userPassWord = ""){
// Jump to the logon page if the user name or password is not entered
Response. sendRedirect ("/Test/index. jsp ");
} Else {// creates a session when a logon request is sent, retrieves the name from the session, and displays
HttpSession session = request. getSession (true );
Session. setAttribute ("name", userName );
String name = (String) session. getAttribute ("name ");
Out. println (name + "already logged on! ");
}
Out. flush ();
Out. close ();
}
}
------------------------------------
The servlet configuration information in the web. xml configuration file is as follows:
------------------------------------
<Servlet>
<Servlet-name> HttpSessionTest </servlet-name>
<Servlet-class> com. neusoft. httpsession. HttpSessionTest </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> HttpSessionTest </servlet-name>
<Url-pattern>/servlet/HttpSessionTest </url-pattern>
</Servlet-mapping>
------------------------------------
The Form Content on the logon page is as follows:
<Form action = "/Test/servlet/HttpSessionTest" method = "post">
<Label> name: </label>
<Input type = "text" name = "userName"> <br>
<Label> password: </label>
<Input type = "password" name = "userPassWord"> <br>
<Input type = "submit" value = "OK">
<Input type = "reset" value = "reset">
</Form>
------------------------------------
3. re-release the project. Enter http: // localhost: 8080/Test/in the browser to bring up the logon window. Enter the user name and password (no verification is performed here) and the servlet processing page will be displayed, and "** logged on
Recorded! "Information, username or password is empty or all are empty. Go to the logon page again.
 
If the client has not been logged on, access http: // localhost: 8080/Test/servlet/HttpSessionTest directly. The server obtains the value of "name" from the session and the value is null, indicates that the user has not logged on or has a session.
The logon page is displayed.
 
If the client has logged on and the session object has not expired, the browser has not been closed. If the client directly accesses http: // localhost: 8080/Test/servlet/HttpSessionTest, the server will
Take the value of "name". This value is not blank. It indicates that the user has logged on and the session has not expired, and the logon information is displayed.
 

Author: "extreme scholar"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.