package com.li.web;
import com.google.gson.Gson;
import com.li.pojo.User;
import com.li.service.UserService;
import com.li.service.impl.UserServiceImpl;
import com.li.utils.WebUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;
public class UserServlet extends BaseServlet {
private UserService userService = new UserServiceImpl();
protected void ajaxExistusername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
boolean existsUsername = userService.exitUsername(username);
Map<String,Object> resultMap=new HashMap<>();
resultMap.put("existsUsername",existsUsername);
Gson gson = new Gson();
String json = gson.toJson(resultMap);
resp.getWriter().write(json);
}
protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().invalidate();
resp.sendRedirect(req.getContextPath());
}
protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
User loginUSer = userService.login(new User(username, password, null));
if (loginUSer == null) {
req.setAttribute("msg", "用户或者密码错误");
req.setAttribute("username", username);
System.out.println("登录失败");
req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
} else {
//将登录信息保存到session中
req.getSession().setAttribute("user",loginUSer);
req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req, resp);
}
}
protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取验证码
String token =(String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
//删除验证码防止重复提交
req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
//获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
String email = req.getParameter("email");
String code = req.getParameter("code");
User user= WebUtils.copyParamToBean(req.getParameterMap(),new User());
//检查验证码要求验证码为abcde
if (token !=null && token.equalsIgnoreCase(code)) {
//检查用户名可用
if (userService.exitUsername(username)) {
System.out.println("用户名存在");
req.setAttribute("msg", "用户名已存在");
req.setAttribute("username", username);
req.setAttribute("email", email);
req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
} else {
//可用保存到数据库
userService.registUser(new User(username, password, email));
//跳转注册成功页面
req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req, resp);
}
} else {
//跳回注册页面
req.setAttribute("msg", "验证码错误");
req.setAttribute("username", username);
req.setAttribute("email", email);
System.out.println("验证码错误");
req.getRequestDispatcher("/pages/user/regist.jsp").forward(req, resp);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
JavaEE项目学习-网上书城系统
共83个文件
java:42个
jsp:20个
jar:12个
需积分: 20 3 下载量 184 浏览量
2022-10-18
13:29:13
上传
评论 2
收藏 7.2MB 7Z 举报
温馨提示
使用 HTML,jQuery实现前端简单业务逻辑, Druid 数据库连接池和统一实现事务的提交和回滚。DAO 层使用 DBUtils 操作数据库,Web 层使用自定义类BaseWeb 类通过反射使用相应业务请求, ajax调用服务判断用户名是否存在。
资源推荐
资源详情
资源评论
收起资源包目录
Book.7z (83个子文件)
Book
Book.iml 4KB
src
jdbc.properties 163B
com
li
dao
OrderItemDao.java 229B
UserDao.java 488B
OrderDao.java 202B
BookDao.java 564B
impl
BaseDAO.java 2KB
UserDaoImpl.java 885B
BookDaoImpl.java 2KB
OrderItemDaoImpl.java 502B
OrderDaoImpl.java 454B
filter
ManagerFilter.java 913B
TransactionFilter.java 823B
test
CartTest.java 2KB
UserServletTest.java 886B
OrderDaoTest.java 549B
BookDaoTest.java 2KB
OrderServiceTest.java 742B
BookServiceTest.java 1KB
OrderItemDaoTest.java 576B
UserServiceTest.java 832B
JDBCUtilsTest.java 541B
UserDaoTest.java 976B
pojo
User.java 1KB
Order.java 2KB
Cart.java 2KB
Page.java 2KB
CartItem.java 2KB
OrderItem.java 2KB
Book.java 2KB
web
OrderServlet.java 1KB
BookServlet.java 3KB
UserServlet.java 4KB
BaseServlet.java 1KB
CartServlet.java 4KB
ClientBookServlet.java 2KB
service
OrderService.java 149B
UserService.java 439B
BookService.java 589B
impl
BookServiceImpl.java 2KB
UserServiceImpl.java 711B
OrderServiceImpl.java 2KB
utils
JDBCUtils.java 3KB
WebUtils.java 954B
web
WEB-INF
tags
web.xml 3KB
lib
taglibs-standard-spec-1.2.1.jar 38KB
taglibs-standard-impl-1.2.1.jar 196KB
mysql-connector-java-8.0.23.jar 2.3MB
commons-logging-1.1.1.jar 59KB
commons-beanutils-1.8.0.jar 226KB
gson-2.2.4.jar 186KB
commons-dbutils-1.3.jar 40KB
commons-fileupload-1.2.1.jar 56KB
druid-1.1.9-sources.jar 1.68MB
druid-1.1.9.jar 2.59MB
mykaptcha-2.3.2.jar 438KB
commons-io-1.4.jar 161KB
static
css
style.css 4KB
img
default.jpg 25KB
pwd-icons-new.png 2KB
logo.gif 11KB
code.bmp 9KB
script
jquery-1.7.2.js 247KB
pages
client
index.jsp 5KB
manager
book_manager.jsp 2KB
manager.jsp 654B
book_edit.jsp 2KB
order_manager.jsp 1KB
cart
checkout.jsp 681B
cart.jsp 4KB
user
login_success.jsp 675B
login.jsp 2KB
regist_success.jsp 760B
regist.jsp 4KB
error
error404.jsp 338B
error500.jsp 344B
common
login_success_menu.jsp 497B
footer.jsp 309B
manger_menu.jsp 430B
page_nav.jsp 3KB
head.jsp 649B
order
order.jsp 1KB
index.jsp 142B
共 83 条
- 1
资源评论
都会一直开心
- 粉丝: 43
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功