纯JSP实现用户登录注册,记事本

时间:2022-04-29
本文章向大家介绍纯JSP实现用户登录注册,记事本,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

没有美化,没有格式,没有样式

1.JSP登陆注册

将用户注册的信息保存在application对象中,用于登录时的验证。

首页如下:

如果未登录,在  session 中找不到 currentUser 的值,则直接跳转到其他页面。

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!-- 用于验证是否登录,如果没有登陆则不能访问该页面,并跳转到登录页面 -->
<% 
    Object obj = session.getAttribute("currentUser");
    if (obj == null) {
        response.sendRedirect("login.jsp");
    }
%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <h1>这是一个首页 !</h1>
</body>
</html>

注册页面如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="operation.jsp" method="post">
        <input type="hidden" name="sub_type" value="reg" />
        <input type="text" name="username" placeholder="输入用户名" />
        <input type="password" name="password" placeholder="输入密码" />
        <input type="password" name="password1" placeholder="确认密码" />
        <input type="submit" value="注册" /><a href="login.jsp">返回登陆</a>
    </form>
</body>
</html>

登录页面如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="operation.jsp" method="post">
        <input type="hidden" name="sub_type" value="log" />
        <input type="text" name="username" placeholder="输入用户名" />
        <input type="password" name="password" placeholder="输入密码" />
        <input type="submit" value="登录" /><a href="register.jsp">注册新用户</a>
    </form>
</body>
</html>

用户类,用于存放用户信息:

仅有用户名和代码两个属性。

package test;

public class User {
    private String username;
    private String password;

    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;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + "]";
    }

}

逻辑控制页面如下:

首先根据给登录页面和注册页面设置的隐藏的 sub_type 属性判断是登录还是注册,然后进行相应的判断。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="test.User"%>
<%    
    //获取请求的信息
    String sub_type = request.getParameter("sub_type");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String password1 = request.getParameter("password1");
    
    //判断是登录页面还是注册页面
    if("reg".equals(sub_type)) {
        if (!password.equals(password1)) {
            out.print("两次输入的密码不一致 !");
        } else {
            Object obj = application.getAttribute(username);
            if (obj != null) {        //用传来的用户名找用户,如果不为空则用户已存在
                out.print("用户名已经存在 !");
            } else {
                User user = new User();        //实例化一个用户并设置信息
                user.setUsername(username);
                user.setPassword(password);
                application.setAttribute(username, user);
                out.print("注册成功 !");
            }
        }
    }
    if("log".equals(sub_type)) {
        Object obj = application.getAttribute(username);
        if(obj!=null) {
            User u = (User)obj;//将获取到的对象强转型,然后获取信息进行判断
            if(password.equals(u.getPassword())) {
                session.setAttribute("currentUser", u);
                response.sendRedirect("index.jsp");
            } else {
                out.print("密码不对啊 !");
            }
            
        } else {
            out.print("用户名不存在 !");
        }
    }
    out.print("<br>");
    out.print("<a href='login.jsp'>跳转登陆</a>");
%>

2.记事本:

在用户登录注册的基础上进行修改,登陆后的用户可以在记事本留言,其他用户登录后可以看到,按照时间倒序排序,可以看到用户名,留言信息,留言时间。

添加一个留言类, Says类,有 用户,留言时间,留言内容三个属性。

主页如下,除了判断登录用户,设置 utf-8 之外,还有注意输出的时候先倒序,遍历输出之后再次倒序。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="test.Says,test.User,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
    
    Object obj1 = session.getAttribute("currentUser");
    if (obj1 == null) {
        response.sendRedirect("denglu.jsp");
    }
%>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <form action="chuli.jsp" method="post">
        <input type="hidden" name="sub_type" value="tex" />
        <textarea rows="4" cols="12" name="text"></textarea>
        <input type="submit" value="提交"> 
    </form>
    <%

        Object obj=application.getAttribute("s");

        if(obj!=null){
            ArrayList<Says> list=(ArrayList)obj;
            
            Collections.reverse(list);//倒序,以保证后写入的内容排在上边
            
            for(Says say : list){
                out.print(say.getUname()+"&nbsp;&nbsp;"+"|"+"&nbsp;&nbsp;");    
                out.print(say.getDate()+"<br>");
                out.print(say.getSays()+"<br>");
                
            }
            Collections.reverse(list);//再次倒序,否则输出的后会出问题
        }
        

    %>
</body>
</html>

注册:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="chuli.jsp" method="post">
        <input type="hidden" name="sub_type" value="reg" />
        <input type="text" name="username" placeholder="输入用户名" />
        <input type="password" name="password" placeholder="输入密码" />
        <input type="password" name="password1" placeholder="确认密码" />
        <input type="submit" value="注册" /><a href="denglu.jsp">返回登陆</a>
    </form>
</body>
</html>

登录:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="chuli.jsp" method="post">
        <input type="hidden" name="sub_type" value="log" />
        <input type="text" name="username" placeholder="输入用户名" />
        <input type="password" name="password" placeholder="输入密码" />
        <input type="submit" value="登录" /><a href="zhuce.jsp">注册新用户</a>
    </form>
</body>
</html>

用户类:

package test;

public class User {
    private String username;
    private String password;

    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;
    }

}

留言类:

package test;

import java.util.Date;

public class Says {
    private String uname;//暂无用
    private String date;
    private String says;
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getSays() {
        return says;
    }
    public void setSays(String says) {
        this.says = says;
    }
    @Override
    public String toString() {
        return "Says [uname=" + uname + ", date=" + date + ", says=" + says + "]";
    }
}

逻辑处理:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="test.Says,test.User,java.util.*"%>
    <%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<%

    String sub_type = request.getParameter("sub_type");
    
    String password = request.getParameter("password");
    String password1 = request.getParameter("password1");
    Says says=new Says();
    
    
    if("reg".equals(sub_type)) {
        
        String username = request.getParameter("username");
        System.out.println(username);
        if (!password.equals(password1)) {
            out.print("两次输入的密码不一致 !");
        } else {
            Object obj = application.getAttribute(username);
            if (obj != null) {
                out.print("用户名已经存在 !");
            } else {
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                application.setAttribute(username, user);
                out.print("注册成功 !");
            }
        }
        out.print("<br>");
        out.print("<a href='denglu.jsp'>跳转登陆</a>");
    }
    if("log".equals(sub_type)) {
        String username = request.getParameter("username");
        System.out.println(username);
        application.setAttribute("name",username);
        Object obj = application.getAttribute(username);
        if(obj!=null) {
            User u = (User)obj;
            if(password.equals(u.getPassword())) {
                session.setAttribute("currentUser", u);
                //String name=u.getUsername();
                //application.setAttribute("name",name);
                response.sendRedirect("main.jsp");
            } else {
                out.print("密码不对啊 !");
            }
            
        } else {
            out.print("用户名不存在 !");
        }
        out.print("<br>");
        out.print("<a href='denglu.jsp'>跳转登陆</a>");
    }
    if("tex".equals(sub_type)){
        String say = request.getParameter("text");
        
        Date d=new Date();
        Object sobj=application.getAttribute("name");//获取用户名
        String sname=(String)sobj;
        
        System.out.println(sname);
        says.setUname(sname);
        says.setSays(say);
        says.setDate(d.toLocaleString());
        Object obj=application.getAttribute("s");
        
        if(obj==null){
            List list =new ArrayList();
            list.add(says);
            application.setAttribute("s",list);
        }else{
            List<Says> list=(List)obj;
            list.add(says);
            application.setAttribute("s",list);
        }
        response.sendRedirect("main.jsp");
    }
    
%>