package cn.javabs.login.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import cn.javabs.login.entity.User;
import cn.javabs.login.service.UserService;
import cn.javabs.login.service.impl.UserServiceImpl;
/**
*
* @author Mryang StringUtilsz中的isNotbBlock:判断某字符串是否不为空且长度不为0且不由空白符构成
*
*/
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 引入UserService
UserService userService = new UserServiceImpl();
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
// 如果传入的参数是login
if ("login".equals(method)) {
login(request, response);
// 如果传入的参数是regist
} else if ("regist".equals(method)) {
regist(request, response);
// 如果传入的参数是logout
} else if ("logout".equals(method)) {
logout(request, response);
// 如果传入的参数是tologin
} else if ("tologin".equals(method)) {
tologin(request, response);
// 如果传入的参数是toregist
} else if ("toregist".equals(method)) {
toregist(request, response);
// 如果传入的参数是对应不上
} else {
request.setAttribute("msg", "该功能正在开发...");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
}
/**
* 用户登录
*
* @param request
* @param response
*/
private void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 1. 获取前台jsp表单传递的参数
*/
String username = request.getParameter("username");
String password = request.getParameter("password");
// 如果测试传入的参数。可以打开72行的注释
// System.out.println("用户名:" + username + "<br/>" + "密码" + password);
/*
* 2.根据参数进行判断
*/
// 2.1 如果用户名和密码都不为空
if (StringUtils.isNotBlank(username)
&& StringUtils.isNotBlank(password)) {
// 2.1.1 传递到service层登录方法
User user = userService.userLogin(username, password);
// 2.1.2 判断用户信息是否查询到
if (user != null) {
// 查询到,存入session
request.getSession().setAttribute("USER_SESSION", user);
// 登录成功,转向主页
request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(
request, response);
} else {
// 未查询到或者用户名和密码不匹配,将失败的信息存储到msg中
request.setAttribute("msg", "用户名或者密码不对!");
// 登录失败,转向错误信息提示页面,根据存入的msg取出相对应的内容
request.getRequestDispatcher("/message.jsp").forward(request,
response);//
}
// 2.2 如果用户名或者密码为空
} else {
request.setAttribute("msg", "用户名或者密码不许为空!");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(
request, response);//
}
}
/**
* 用户注册
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void regist(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User user = new User();
// 1. 获取前台jsp页面的参数
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String email = request.getParameter("email");
// 2. 将获取到的参数封装到user对象中去
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
user.setSex(sex);
if (StringUtils.isNotBlank(user.getUsername())
&& StringUtils.isNotBlank(user.getPassword())) {
int rows = userService.userRegister(user);
if (rows > 0) {
response.setHeader("refresh",
"1;url=UserServlet?method=tologin");
} else {
request.setAttribute("msg", "注册失败!");
request.getRequestDispatcher("/message").forward(request,
response);
}
}else{
request.setAttribute("msg", "用户名或者密码不许为空");
request.getRequestDispatcher("/message").forward(request,
response);
}
}
/**
* 转向到登录页面
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void tologin(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,
response);//
}
/**
* 转向到注册页面
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void toregist(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/jsp/regist.jsp").forward(
request, response);//
}
/**
* 用户注销
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void logout(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.getSession().invalidate();
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(
request, response);//
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Login.zip (52个子文件)
config
jdbc.properties 156B
WebContent
css
sky-forms-ie8.css 2KB
reset.css 1KB
sky-forms.css 31KB
demo.css 590B
login.css 1KB
img
bg-cyan.jpg 236KB
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 1KB
jsp
main.jsp 605B
login.jsp 3KB
regist.jsp 6KB
lib
com.springsource.org.apache.commons.logging-1.1.1.jar 60KB
mysql-connector-java-5.1.7-bin.jar 693KB
commons-pool2-2.6.0.jar 128KB
commons-lang3-3.8.1.jar 490KB
commons-dbutils-1.7.jar 95KB
commons-dbcp2-2.5.0.jar 193KB
js
jquery.min.js 81KB
jquery.placeholder.min.js 2KB
canvas-particle.js 5KB
sky-forms-ie8.js 425B
jquery-1.9.1.min.js 90KB
jquery.validate.min.js 26KB
login.js 858B
index.jsp 408B
message.jsp 358B
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.wst.common.component 544B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 503B
org.eclipse.jdt.core.prefs 364B
src
cn
javabs
login
dao
UserDao.java 762B
impl
UserDaoImpl.java 1KB
sql
login.sql 406B
controller
UserServlet.java 6KB
service
UserService.java 551B
impl
UserServiceImpl.java 850B
entity
User.java 933B
util
DBCPUtils.java 1KB
.project 1KB
.classpath 887B
build
classes
jdbc.properties 156B
cn
javabs
login
dao
UserDao.class 313B
impl
UserDaoImpl.class 2KB
sql
login.sql 406B
controller
UserServlet.class 4KB
service
UserService.class 275B
impl
UserServiceImpl.class 1KB
entity
User.class 1KB
util
DBCPUtils.class 2KB
共 52 条
- 1
资源评论
- usappt2020-06-10最近自己撸代码,好多东西都忘光了,重新来学习
杨校
- 粉丝: 3430
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功