项目学习 —— 注册与登录(3)

一. 登录成功以后,首页将不再显示登录和注册按钮,将显示欢迎某用户登录和注销

1.将UserServlet改为如下: 

package server.web;

import server.pojo.User;
import server.service.UserService;
import server.service.impl.UserServiceImpl;
import server.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取客户端的请求
        User user = WebUtils.copyParamToBean(req.getParameterMap(),new User());

        //2.用户登录
        User loginUser = userService.login(user);
        if(loginUser == null){
            //登录失败,跳回登录页面
            req.setAttribute("msg","用户名或密码错误!");
            req.setAttribute("username",user.getUsername());
            req.getRequestDispatcher("pages/user/login.jsp").forward(req,resp);
        }else{
            //登录成功,跳到首页index.jsp
            req.getSession().setAttribute("user",loginUser);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }
    }

    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取客户端的请求
        User user = WebUtils.copyParamToBean(req.getParameterMap(),new User());

        //2.用户注册
        if(userService.existUsername(user.getUsername())){
            req.setAttribute("msg","用户名已存在");
            req.setAttribute("username",user.getUsername());
            req.setAttribute("email",user.getEmail());
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);  //跳转到注册页面
        }else{
            userService.registUser(user);
            req.getRequestDispatcher("index.jsp").forward(req,resp);  //跳转到注册成功页面
        }
    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //销毁Session
        req.getSession().invalidate();
        //重定向到首页
        resp.sendRedirect("index.jsp");
    }
}

 2. 将index.jsp页面改为如下

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core_1_1" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    首页
    <%    //获取当前工程的路径
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
    %>
    



    <%-- 如果用户还没有登录,显示登录和注册菜单--%>
    
        
        
    
    <%-- 如果用户已经登录,显示欢迎用户和注销--%>
    
        欢迎${sessionScope.user.username}
        
    

二. 添加验证码

1. 导入谷歌验证码kaptcha的jar包

2. 在web.xml中配置以下信息




    
        KaptchaServlet
        com.google.code.kaptcha.servlet.KaptchaServlet
    

    
        KaptchaServlet
        /kaptcha.jpg
    

3. 修改regist.jsp页面,添加验证码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    注册
    <%    //获取当前工程的路径
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
    %>
    


    
用户名:
用户密码:
确认密码:
电子邮箱:
验证码:

${empty requestScope.msg ? "" : requestScope.msg}

4. 将UserServlet改为如下:

package server.web;

import server.pojo.User;
import server.service.UserService;
import server.service.impl.UserServiceImpl;
import server.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取客户端的请求
        User user = WebUtils.copyParamToBean(req.getParameterMap(),new User());

        //2.用户登录
        User loginUser = userService.login(user);
        if(loginUser == null){
            //登录失败,跳回登录页面
            req.setAttribute("msg","用户名或密码错误!");
            req.setAttribute("username",user.getUsername());
            req.getRequestDispatcher("pages/user/login.jsp").forward(req,resp);
        }else{
            //登录成功,跳到首页index.jsp
            req.getSession().setAttribute("user",loginUser);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }
    }

    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取Session中的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //删除Session中的验证码
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);

        String code = req.getParameter("code");

        //1.获取客户端的请求
        User user = WebUtils.copyParamToBean(req.getParameterMap(),new User());

        //2.用户注册
        if(token != null && token.equalsIgnoreCase(code)){   //检查验证码是否正确
            if(userService.existUsername(user.getUsername())){   //注册不成功
                req.setAttribute("msg","用户名已存在");
                req.setAttribute("username",user.getUsername());
                req.setAttribute("email",user.getEmail());
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);  //跳转到注册页面
            }else{
                userService.registUser(user);    //注册成功
                req.getRequestDispatcher("index.jsp").forward(req,resp);  //跳转到首页
            }
        }else {
            // 把回显信息,保存到Request域中
            req.setAttribute("msg", "验证码错误!");
            req.setAttribute("username", user.getUsername());
            req.setAttribute("email", user.getEmail());
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
        }
    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //销毁Session
        req.getSession().invalidate();
        //重定向到首页
        resp.sendRedirect("index.jsp");
    }
}

你可能感兴趣的:(项目(1),java,jsp)