09-real-time online count statistics (JSP built-in object)

Source: Internet
Author: User

Enter. jsp

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %> <HTML> <body> <! -- Create a user login form and submit it to the doenter page --> <form action = "doenter. JSP "method =" Post "> guest name: <input type = "text" name = "guestname"/> <br/> <input type = "Submit" value = ""/> </form> </body> 

Doenter. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ page import =" entity. user "%> <% // retrieve the visitor name string guestname = request from the request. getparameter ("guestname"); // If the guest name is null or empty, log on again if (guestname = NULL | guestname. equals ("") {response. sendredirect ("enter. JSP ");} else {// create user Object User user = new user (); // Save the guest name user in the user object. setusername (guestname); // Save the Session of the user object in the session. setattribute ("guest", user); response. sendredirect ("online. JSP ") ;}%>

Online. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ page import =" entity. user "%> <% @ page import =" entity. constents "%> <body> <% entity. user user = NULL; // determines whether there is any guest attribute if (Session. getattribute ("guest") = NULL) {response. sendredirect ("enter. JSP ");} else {user = (entity. user) session. getattribute ("guest"); %> hello, <% = user. getUserName () %> <br/> the number of online users is: <% = constents. online_user_count %> <br/> <a href = "doout. JSP "> exit </a> <% }%> </body>

Doout. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% // use session to invalidate the session. invalidate (); response. sendredirect ("enter. JSP "); %>

Constents. Java

Package entity; public class constents {// static integer variable, used to save the number of online users public static int online_user_count = 0; // number of online users}

User. Java

 

Package entity; import javax. servlet. HTTP. httpsessionbindingevent; import javax. servlet. HTTP. httpsessionbindinglistener; public class user implements httpsessionbindinglistener {private int ID; private string username; private string password; private string email; Public int GETID () {return ID ;} public void setid (int id) {This. id = ID;} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;} Public String getemail () {return email;} public void setemail (string email) {This. email = Email;} public void valuebound (httpsessionbindingevent event) {// The session is automatically called. online_user_count ++;} public void valueunbound (httpsessionbindingevent event) {// deletes automatically called constents from the session. online_user_count --;}}

Google

 

IE

 

Related Article

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.