JavaWeb_(session和application)用户登录注册模板_进阶版

我自横刀向天笑,去留肝胆两昆仑。这篇文章主要讲述JavaWeb_(session和application)用户登录注册模板_进阶版相关的知识,希望能为你提供帮助。
 
 
用户登录注册模板_基础版传送门
 
用户登录注册模板进阶版
添加了获得获得当前登录用户信息及存储登录人数
用户登录后,在首页、注册页、登录页显示登录用户信息和存储登录人数信息

JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片

 
目录结构
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片

 
 
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
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="https://www.songbingjia.com/android/login.jsp"> 登录< /a> < a href="https://www.songbingjia.com/android/register.jsp"> 注册< /a> < /body> < /html>

index.jsp 
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
< %@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="https://www.songbingjia.com/android/登录"/> < /form> < /body> < /html>

login.jsp 
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
< %@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>

login_do.jsp 
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
< %@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="https://www.songbingjia.com/android/男" /> 女< input type="radio" name="sex" value="https://www.songbingjia.com/android/女"/> < br/> < input type="submit" value="https://www.songbingjia.com/android/注册"/> < /form> < /body> < /html>

register.jsp【JavaWeb_(session和application)用户登录注册模板_进阶版】 
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
< %@ 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>

register_do.jsp 
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
JavaWeb_(session和application)用户登录注册模板_进阶版

文章图片
< %@ page import="com.Gary.model.User" %> < %@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> < !

    推荐阅读