package com.demo.servlet;
import com.demo.service.UserService;
import com.demo.service.impl.UserServiceImpl;
import com.demo.util.Util;
import com.demo.vo.User;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
//@WebServlet("/AuthServlet")
public class AuthServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//过滤编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String action = Util.decode(request, "action");
if ("login".equalsIgnoreCase(action)) {//登录
String username = Util.decode(request, "username");
String password = Util.decode(request, "password");
String validationCode = Util.decode(request, "validationCode");
if (validationCode != null && !validationCode.equals(request.getSession().getAttribute("validationCode"))) {//验证码不通过
request.getSession().setAttribute("alert_msg", "错误:验证码不正确!");
request.getRequestDispatcher("login.jsp").forward(request, response);
return;
}
UserService userService = new UserServiceImpl();
Map<String, Object> params = new HashMap();
params.put("searchColumn", "username");//使用`username`字段进行模糊查询
params.put("keyword", username);
List<User> list = (List<User>) userService.list(params).get("list");
for (User user : list) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {//找到这个管理员了
request.getSession().setAttribute("loginUser", user);
request.getRequestDispatcher("menu.jsp").forward(request, response);
return;
}
}
request.getSession().setAttribute("alert_msg", "错误:用户名或密码错误!");
request.getRequestDispatcher("login.jsp").forward(request, response);
} else if ("register".equalsIgnoreCase(action)) {//注册
String username = Util.decode(request, "username");
String password = Util.decode(request, "password");
System.out.println("username=" + username);
System.out.println("password=" + password);
UserService userService = new UserServiceImpl();
Map<String, Object> params = new HashMap();
params.put("searchColumn", "username");//使用`username`字段进行模糊查询
params.put("keyword", username);
params.put("startIndex", 0);
params.put("pageSize", Long.MAX_VALUE);
List<User> list = (List<User>) userService.list(params).get("list");
for (User user : list) {
if (user.getUsername().equals(username) /*&& user.getPassword().equals(password)*/) {//说明该用户名已存在,必须换个用户名才能注册
request.getSession().setAttribute("alert_msg", "错误:用户名已存在!");
request.getRequestDispatcher("register.jsp").forward(request, response);
return;
}
}
User vo = new User();
vo.setUsername(username);
vo.setPassword(password);
//vo.setUserType("普通用户");//需要设置一个默认值
userService.add(vo);
request.getSession().setAttribute("alert_msg", "注册成功!用户名:[" + username + "]");
request.getRequestDispatcher("login.jsp").forward(request, response);
} else if ("logout".equalsIgnoreCase(action)) {//登出
HttpSession session = request.getSession();
User user = (User) session.getAttribute("loginUser");
if (user != null) {
session.removeAttribute("loginUser");
}
response.sendRedirect("login.jsp");
} else if ("validationCode".equalsIgnoreCase(action)) {
String codeChars = "0123456789";// 图形验证码的字符集合,系统将随机从这个字符串中选择一些字符作为验证码
// 获得验证码集合的长度
int charsLength = codeChars.length();
// 下面三条记录是关闭客户端浏览器的缓冲区
// 这三条语句都可以关闭浏览器的缓冲区,但是由于浏览器的版本不同,对这三条语句的支持也不同
// 因此,为了保险起见,建议同时使用这三条语句来关闭浏览器的缓冲区
response.setHeader("ragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 设置图形验证码的长和宽(图形的大小)
int width = 90, height = 20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();// 获得用于输出文字的Graphics对象
Random random = new Random();
g.setColor(getRandomColor(180, 250));// 随机设置要填充的颜色
g.fillRect(0, 0, width, height);// 填充图形背景
// 设置初始字体
g.setFont(new Font("Times New Roman", Font.ITALIC, height));
g.setColor(getRandomColor(120, 180));// 随机设置字体颜色
// 用于保存最后随机生成的验证码
StringBuilder validationCode = new StringBuilder();
// 验证码的随机字体
String[] fontNames = {"Times New Roman", "Book antiqua", "Arial"};
for (int i = 0; i < 4; i++) {
// 随机设置当前验证码的字符的字体
g.setFont(new Font(fontNames[random.nextInt(3)], Font.ITALIC, height));
// 随机获得当前验证码的字符
char codeChar = codeChars.charAt(random.nextInt(charsLength));
validationCode.append(codeChar);
// 随机设置当前验证码字符的颜色
g.setColor(getRandomColor(10, 100));
// 在图形上输出验证码字符,x和y都是随机生成的
g.drawString(String.valueOf(codeChar), 16 * i + random.nextInt(7), height - random.nextInt(6));
}
HttpSession session = request.getSession();
session.setMaxInactiveInterval(5 * 60);
// 将验证码保存在session对象中,key为validation_code
session.setAttribute("validationCode", validationCode.toString());
g.dispose();// 关闭Graphics对象
OutputStream os = response.getOutputStream();
ImageIO.write(image, "JPEG", os);// 以JPEG格式向客户端发送图形验证码
} else if ("resetPassword".equalsIgnoreCase(action)) {
String msg;
User loginUser = (User) request.getSession().getAttribute("loginUser");
String oldPassword = Util.decode(request, "oldPassword");
if (!loginUser.getPassword().equals(oldPassword)) {
msg = "原密码错误!";
} else {
String newPassword = Util.decode(request, "newPassword");
loginUser.setPassword(newPassword);
UserService userService = new UserServiceImpl();
userService.update(loginUser);
msg = "修改成
没有合适的资源?快使用搜索试试~ 我知道了~
毕设-Javaweb智能维修管理系统
共281个文件
class:84个
jsp:66个
java:28个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 118 浏览量
2022-04-02
16:25:03
上传
评论 2
收藏 8.61MB 7Z 举报
温馨提示
环境:开发工具:idea,数据库:MySQL5.7 jdk1.8 架构:Javaweb,前端jsp 主要功能 用户:用户管理、维修管理、设备管理等; 管理员:用户管理、维修管理、设备管理、公告管理等
资源推荐
资源详情
资源评论
收起资源包目录
毕设-Javaweb智能维修管理系统 (281个子文件)
AuthServlet.class 7KB
AuthServlet.class 7KB
AuthServlet.class 7KB
WeixiuDAOImpl.class 6KB
WeixiuDAOImpl.class 6KB
WeixiuDAOImpl.class 6KB
ShebeiDAOImpl.class 6KB
ShebeiDAOImpl.class 6KB
ShebeiDAOImpl.class 6KB
UserDAOImpl.class 6KB
UserDAOImpl.class 6KB
UserDAOImpl.class 6KB
NoticeDAOImpl.class 6KB
NoticeDAOImpl.class 6KB
NoticeDAOImpl.class 6KB
WeixiuServlet.class 5KB
WeixiuServlet.class 5KB
WeixiuServlet.class 5KB
ShebeiServlet.class 5KB
ShebeiServlet.class 5KB
ShebeiServlet.class 5KB
UserServlet.class 5KB
UserServlet.class 5KB
UserServlet.class 5KB
NoticeServlet.class 5KB
NoticeServlet.class 5KB
NoticeServlet.class 5KB
Util.class 5KB
Util.class 5KB
Util.class 5KB
PageBean.class 3KB
PageBean.class 3KB
PageBean.class 3KB
LoginFilter.class 2KB
LoginFilter.class 2KB
LoginFilter.class 2KB
Weixiu.class 2KB
Weixiu.class 2KB
Weixiu.class 2KB
Shebei.class 2KB
Shebei.class 2KB
Shebei.class 2KB
User.class 2KB
User.class 2KB
User.class 2KB
ShebeiServiceImpl.class 1KB
NoticeServiceImpl.class 1KB
WeixiuServiceImpl.class 1KB
ShebeiServiceImpl.class 1KB
WeixiuServiceImpl.class 1KB
NoticeServiceImpl.class 1KB
NoticeServiceImpl.class 1KB
ShebeiServiceImpl.class 1KB
WeixiuServiceImpl.class 1KB
UserServiceImpl.class 1KB
UserServiceImpl.class 1KB
UserServiceImpl.class 1KB
Notice.class 1KB
Notice.class 1KB
Notice.class 1KB
WeixiuService.class 447B
ShebeiService.class 447B
NoticeService.class 447B
WeixiuService.class 447B
NoticeService.class 447B
ShebeiService.class 447B
ShebeiService.class 447B
NoticeService.class 447B
WeixiuService.class 447B
UserService.class 439B
UserService.class 439B
UserService.class 439B
ShebeiDAO.class 435B
WeixiuDAO.class 435B
NoticeDAO.class 435B
WeixiuDAO.class 435B
ShebeiDAO.class 435B
NoticeDAO.class 435B
ShebeiDAO.class 435B
WeixiuDAO.class 435B
NoticeDAO.class 435B
UserDAO.class 427B
UserDAO.class 427B
UserDAO.class 427B
bootstrap.css 179KB
bootstrap.css 179KB
bootstrap.css 179KB
bootstrap.min.css 148KB
bootstrap.min.css 148KB
bootstrap.min.css 148KB
myCss.css 111KB
myCss.css 111KB
myCss.css 111KB
bootstrap-theme.css 26KB
bootstrap-theme.css 26KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
bootstrap-theme.min.css 23KB
bootstrap-theme.min.css 23KB
glyphicons-halflings-regular.eot 20KB
共 281 条
- 1
- 2
- 3
资源评论
计算机毕业设计,
- 粉丝: 541
- 资源: 110
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功