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