JavaWeb_(session和application)用户登录注册模板_进阶版
用户登录注册模板_基础版 传送门
用户登录注册模板进阶版
添加了获得获得当前登录用户信息及存储登录人数
用户登录后,在首页、注册页、登录页显示登录用户信息和存储登录人数信息
目录结构
<%@page import="com.Gary.model.User"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% Object o = session.getAttribute("user"); if(o!=null){ User user = (User)o; out.println("当前登录用户:"+user.getUsername()+"<br/>"); } %> <% Object o2 = application.getAttribute("userNumber"); if(o2!=null){ out.println("当前登录用户"+o2+"个<br/>"); } %> <a href="login.jsp">登录</a> <a href="register.jsp">注册</a> </body> </html>
<%@page import="com.Gary.model.User"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% Object o = session.getAttribute("user"); if(o!=null){ User user = (User)o; out.println("当前登录用户:"+user.getUsername()+"<br/>"); } %> <% Object msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> 登录<hr> <form action="login_do.jsp" methon="post"> 用户名:<input type="text" name="username" /><br/> 密码 :<input type="password" name="password" /><br/> <input type="submit" value="登录"/> </form> </body> </html>
<%@page import="com.Gary.util.DBUtil" %> <%@page import="com.Gary.model.User" %> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% String username = request.getParameter("username"); String password = request.getParameter("password"); User user = DBUtil.verifyAccount(username,password); if(user==null){ // out.println("登录失败,用户名或密码错误"); request.setAttribute("message","登录失败,用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request,response); }else{ // out.println("登录成功"); int number = 0; //第一次登录用户时if不瞒住,number=0 if(application.getAttribute("userNumber")!=null){ number = (Integer)application.getAttribute("userNumber"); } //登录过一个用户后在原基础number上进行++ number++; application.setAttribute("userNumber",number); //通过seesion的setAttribute传递数据 session.setAttribute("user", user); //登录请求的转发,将数据传递给personCenter.jsp页面 request.getRequestDispatcher("personCenter.jsp").forward(request,response); } %> </body> </html>
<%@page import="com.Gary.model.User"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% Object o = session.getAttribute("user"); if(o!=null){ User user = (User)o; out.println("当前登录用户:"+user.getUsername()+"<br/>"); } %> <% Object msg = request.getAttribute("message"); if(msg!=null) out.println(msg); %> <form action="register_do.jsp" methon="post"> 用户名:<input type="text" name="username" /><br/> 密码: <input type="password" name="password" /><br/> 年龄: <input type="text" name="age" /><br/> 性别:男<input type="radio" name="sex" value="男" />女<input type="radio" name="sex" value="女"/><br/> <input type="submit" value="注册"/> </form> </body> </html>
<%@ page import="com.Gary.util.DBUtil" %> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% String username = request.getParameter("username"); String password = request.getParameter("password"); int age = Integer.parseInt(request.getParameter("age")); String sex = request.getParameter("sex"); boolean isSuccess = DBUtil.addUser(username,password,age,sex); //使用请求转发 if(isSuccess){ request.setAttribute("message", "注册成功,请登录"); //通过getRequestDispatcher传递路径将请求转发给login.jsp request.getRequestDispatcher("login.jsp").forward(request,response); }else{ request.setAttribute("message", "注册失败,用户名重复"); //通过getRequestDispatcher传递路径将请求转发给login.jsp request.getRequestDispatcher("register.jsp").forward(request,response); } // if(isSuccess){ // out.println(username); // out.println("<font color='green'>注册成功</font>"); // }else{ // out.println(username); // out.println("<font color='red'>注册失败</font>"); // } %> </body> </html>
<%@ page import="com.Gary.model.User" %> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <% //通过session的getAttribute进行访问 User user = (User)session.getAttribute("user"); %> 用户名:<%=user.getUsername()%><br/> 年龄:<%=user.getAge()%><br/> 性别:<%=user.getSex()%><br/> </body> </html>
package com.Gary.model; public class User { private String username; private String password; private int age; private String sex; public User(String username, String password, int age, String sex) { super(); this.username = username; this.password = password; this.age = age; this.sex = sex; } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
package com.Gary.util; import java.util.HashMap; import java.util.Map; import com.Gary.model.User; public class DBUtil { private static Map<String ,User>db = new HashMap<String,User>(); public static boolean addUser(String username,String password,int age,String sex) { //TODO if(db.containsKey(username)) { return false; }else { User user=new User(username,password,age,sex); db.put(username, user); return true; } } //账号校验 public static User verifyAccount(String username,String password) { if(db.containsKey(username)) { User user = db.get(username); if(user.getPassword().equals(password)) { return user; }else { return null; } } return null; } }
获得当前用户登录
注册时通过session的setAttribute传递数据
<% String username = request.getParameter("username"); String password = request.getParameter("password"); User user = DBUtil.verifyAccount(username,password); if(user==null){ // out.println("登录失败,用户名或密码错误"); request.setAttribute("message","登录失败,用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request,response); }else{ // out.println("登录成功"); //通过seesion的setAttribute传递数据 session.setAttribute("user", user); //登录请求的转发,将数据传递给personCenter.jsp页面 request.getRequestDispatcher("personCenter.jsp").forward(request,response); } %>
personCenter中通过Session对存储user键值进行访问
<% //通过session的getAttribute进行访问 User user = (User)session.getAttribute("user"); %>
index.jsp、login.jsp、register.jsp中添加session会话获得当前登录用户
<% Object o = session.getAttribute("user"); if(o!=null){ User user = (User)o; out.println("当前登录用户:"+user.getUsername()+"<br/>"); } %>
存储登录人数
当用户web启动的时候会创建application内置对象,当用户关闭web时销毁application内置对象
application存放的数据通常是所有客户端所共享的数据
当用户注册时,login_do.jsp中定义一个number变量用来存储登录人数,每次有用户进行登录时,使number数量+1
<% String username = request.getParameter("username"); String password = request.getParameter("password"); User user = DBUtil.verifyAccount(username,password); if(user==null){ // out.println("登录失败,用户名或密码错误"); request.setAttribute("message","登录失败,用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request,response); }else{ // out.println("登录成功"); int number = 0; //第一次登录用户时if不瞒住,number=0 if(application.getAttribute("userNumber")!=null){ number = (Integer)application.getAttribute("userNumber"); } //登录过一个用户后在原基础number上进行++ number++; application.setAttribute("userNumber",number); //通过seesion的setAttribute传递数据 session.setAttribute("user", user); //登录请求的转发,将数据传递给personCenter.jsp页面 request.getRequestDispatcher("personCenter.jsp").forward(request,response); } %>
在index.jsp中添加application应用
<% Object o2 = application.getAttribute("userNumber"); if(o2!=null){ out.println("当前登录用户"+o2+"个<br/>"); } %>
(如需转载学习,请标明出处)