本帖最后由 paulliu 于 2019-3-20 13:58 编辑
package cn.itcat.web;
import cn.itcat.domain.User;
import cn.itcat.service.impl.UserServiceimpl;
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 java.io.IOException;
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//设置编码格式
req.setCharacterEncoding("utf-8");
//获取验证码
String vcode = req.getParameter("verifycode");
//获取系统生成的验证码
String checkcode_server =(String) req.getSession().getAttribute("CHECKCODE_SERVER");
//获取用户名与密码
String username = req.getParameter("username");
String password = req.getParameter("password");
User user = new User();
user.setUsername(username);
user.setPassword(password);
//判断用户名和密码是否为空
boolean b = username.length()>0 &&password.length()>.0;
if (b==false){
//跳转到登录页面,提示用户名或密码不能为空
req.setAttribute("inputnull_lerro","用户名和密码不能为空");
req.getRequestDispatcher("login.jsp").forward(req,res);
}else {
//判断验证码是否正确
if (checkcode_server != null && vcode != null && checkcode_server.equalsIgnoreCase(vcode) && b == true) {
//如果验证码正确,执行查询方法
User loginuser = new UserServiceimpl().login(user);
if (loginuser != null) {
//跳转到登录成功页面
req.setAttribute("loginuser", loginuser);
req.getRequestDispatcher("/index.jsp").forward(req, res);
} else {
//跳转到登录失败提示:用户名或者密码错误
req.setAttribute("login_erro", "用户名或密码输入错误");
req.getRequestDispatcher("/login.jsp").forward(req, res);
}
} else {
//跳转到登录页面验证码输入错误,请重输
req.setAttribute("checkcode_erro", "验证码输入错误");
req.getRequestDispatcher("/login.jsp").forward(req, res);
}
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
this.doPost(req, res);
}
}
|
|