• JavaWeb_利用Servlet处理注册登录请求


    利用Servlet处理注册登录请求

      程序结构

      

    <%@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" method="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.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>
    personCenter.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" method="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
    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;
        }
        
        
        
    }
    User.java
    package com.Gary.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.Gary.model.User;
    import com.Gary.util.DBUtil;
    
    
    @WebServlet("/login_do")
    public class LoginServlet extends HttpServlet {
       
        
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            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;
                //获取到application对象
                ServletContext application = this.getServletContext();
                //第一次登录用户时if不瞒住,number=0
                if(application.getAttribute("userNumber")!=null){
                    number = (Integer)application.getAttribute("userNumber");
                }
                
                //登录过一个用户后在原基础number上进行++
                number++;
    
                application.setAttribute("userNumber",number);
                
                //通过seesion的setAttribute传递数据
                request.getSession().setAttribute("user", user);
                //登录请求的转发,将数据传递给personCenter.jsp页面
                request.getRequestDispatcher("personCenter.jsp").forward(request,response);
            }
        }
    
    }
    LoginServlet.java
    package com.Gary.servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.Gary.util.DBUtil;
    
    
    @WebServlet("/register_do")
    public class RegisterServlet extends HttpServlet {
    
        
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            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);
            }
            
        }
    
    }
    RegisterServlet.java
    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;
        }
    }
    DBUtil.java

      通过RegisterServlet.java和LoginServlet.java来处理注册登录请求

    用户注册

      通过户数据模型驱动User,java和DBUtild.java中对数据进行本地处理

      register.jsp发送请求表单信息到register_do(RegisterServlet.java路径:@WebServlet("/register_do"))

      登录同理注册

    <form action="register_do" method="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>

       RegisterServlet.java通过doPost()方法处理注册信息

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            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);
            }
            
        }

      

    (如需转载学习,请标明出处)
  • 相关阅读:
    C语言、指针的指针和野指针的问题
    常见证书格式和转换
    cation,validation,qualification有何区别
    Cygwin + OpenSSH FOR Windows的安装配置
    python static variable
    45个与众不同的非常棒网页设计案例
    65个精心设计的富有灵感的电子商务网站案例
    60个抢眼的企业网站设计案例
    26个有用的创建视觉图片网站的jQuery插件
    Android 图像用户界面免费的PSD设计文件
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/10296096.html
Copyright © 2020-2023  润新知