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

Posted Cynical丶Gary

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
index.jsp

 

<%@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>
login.jsp

 

<%@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

 

<%@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>
register.jsp

 

<%@ 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

 

<%@ page import="com.Gary.model.User" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!JAVAWEB学习笔记16_session&cookie

java_第一年_JavaWeb

JavaWeb_(Hibernate框架)Hibernate中对象的三种状态

Javaweb设置session过期时间

javaWeb中的session

JavaWeb之Cookie和Session技术