package com.bjsxt.servlet;
import com.bjsxt.entity.User;
import com.bjsxt.service.UserService;
import com.bjsxt.service.impl.UserServiceImpl;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class UserServlet extends BaseServlet {
// @Override
// protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// //解决POST表单的中文乱码问题
// request.setCharacterEncoding("utf-8");
// //接收method属性的值
// String methodName = request.getParameter("method");
//
// //根据method属性的值调用相应的方法
// if("login".equals(methodName)){
// this.login(request,response);
// }else if("register".equals(methodName)){
// this.register(request,response);
// }else if("logout".equals(methodName)){
// this.logout(request,response);
// }
//
// }
public void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取表单的数据
String userId = request.getParameter("userId");
if(userId == null){
userId = "";
}
String strAge = request.getParameter("minAge");
int minAge = 0;
try{
minAge = Integer.parseInt(strAge); //"12" "abc"
}catch(NumberFormatException e){
e.printStackTrace();
}
//调用业务层完成查询操作
UserService userService = new UserServiceImpl();
//List<User> userList = userService.findAll();
List<User> userList = userService.find(userId,minAge);
//List<User> userList = null;
//List<User> userList = new ArrayList<User>();
//跳转到show.jsp显示数据
request.setAttribute("userId",userId);
request.setAttribute("minAge",strAge);
request.setAttribute("ulist",userList);
request.getRequestDispatcher("/admin/show.jsp").forward(request,response);
}
public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//结束当前的session
request.getSession().invalidate();
//跳转回登录页面
response.sendRedirect(request.getContextPath()+"/admin/login.jsp");
}
public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//request.setCharacterEncoding("utf-8");
//1.接收来自视图层的表单数据
String userId = request.getParameter("userId");
String realName = request.getParameter("realName");
String pwd = request.getParameter("pwd");
String rePwd = request.getParameter("repwd");
int age = Integer.parseInt(request.getParameter("age"));// "23"
String [] hobbyArr = request.getParameterValues("hobby");
String strDate = request.getParameter("enterDate");//"1999-12-23"
Date enterDate = Date.valueOf(strDate);
//util.Date SimpleDateFormat
//判断两次密码是否相同
if(pwd == null || !pwd.equals(rePwd)){
request.setAttribute("error","两次密码必须相同");
request.getRequestDispatcher("/admin/register.jsp").forward(request,response);
return;
}
//2.调用业务层完成注册操作并返回结果
User user = new User(userId,realName,pwd,age, Arrays.toString(hobbyArr),enterDate);
UserService userService = new UserServiceImpl();
int n = userService.register(user);
//3.根据结果进行页面跳转
if(n>0){
response.sendRedirect(request.getContextPath()+"/admin/login.jsp");
}else{
request.setAttribute("error","注册失败");
request.getRequestDispatcher("/admin/register.jsp").forward(request,response);
}
}
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决POST表单的中文乱码问题
//request.setCharacterEncoding("utf-8");
//获取用户名和密码 request 内建对象 请求
String username = request.getParameter("username");
String password = request.getParameter("password");
String rememberme = request.getParameter("rememberme");
//调用下一层判断登录是否成功,并返回结果
//进行服务器端的表单验证
if(username ==null || "".equals(username)){
request.setAttribute("error","用户名不能为空JSP");
request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
return;
}
if (username.length()<=6){
request.setAttribute("error","用户名长度必须大于6JSP");
request.getRequestDispatcher("/admin/login.jsp").forward(request,response);//后面语句还会执行
return; //后面的语句不再执行
}
// boolean flag = false;//默认失败
// if(username.indexOf("sxt")>=0 || username.contains("尚学堂")){
// flag = true;
// }
User user = null;//默认登录失败
// UserDao userDao = new UserDaoImpl();
// user = userDao.find(username,password);
UserService userService = new UserServiceImpl();
user = userService.login(username,password);
//userService.addOrder("shoppingCart");
//输出结果
if(user != null){
//登录成功才记住我
//1.办理会员卡
String username2 = URLEncoder.encode(username,"utf-8");
Cookie cookie1 = new Cookie("uname",username2);
Cookie cookie2 = new Cookie("password",password);
//2.指定会员卡的作用范围,默认范围是当前目录 /servlet/LoginServlet /admin/login.jsp
//cookie1.setPath("/"); //当前服务器
cookie1.setPath("/myservlet2/"); //当前项目
cookie2.setPath("/myservlet2");
//3.指定会员卡的作用时间
if("yes".equals(rememberme)){
cookie1.setMaxAge(60*60*24*10); //默认的时间浏览器不关闭的时间;-1 表示一直有效
cookie2.setMaxAge(60*60*24*10);
}else{
cookie1.setMaxAge(0);
cookie2.setMaxAge(0);
}
//4.将会员卡带回家
response.addCookie(cookie1);
response.addCookie(cookie2);
//成功跳转到成功页面
//out.println("登录成功");
// /servlet/LoginServlet
// /servlet/success.jsp
// request.getRequestDispatcher("/admin/success.jsp").forward(request,response);
HttpSession session = request.getSession();
// session.setAttribute("username",username);
session.setAttribute("user",user);
//response.sendRedirect("/myservlet2/admin/success.jsp");
//response.sendRedirect("https://www.bjsxt.com:443/news/11377.html");
//response.sendRedirect("http://localhost:8080/myservlet2/admin/success.jsp");
//response.sendRedirect("/myservlet2/admin/success.jsp");
//response.sendRedirect("/myservlet2/admin/success.jsp");
//response.sendRedirect(request.getContextPath()+"/admin/success.jsp");
//http://192.168.58.250:8080/myservlet2/servlet/LoginServlet
//http://192.168.58.250:8080/myservlet2/admin/success.jsp
//登录成功后,网站的访问人数+1
//1.获取当前的访问人数
ServletContext context = this.getServletContext();
Integer count2 = (Integer) context.getAttribute("count");
//2.人数+1
if(count2 == null){ //第一个用户
count2 = 1;
}else{
count2++;
}
//3.再存放到application作用域中
context.setAttribute("count",count2);
//http://192.168.58.250:8080/myservlet2/servlet/admin/success.jsp
response.sendRedirect("../admin/success.jsp");
}else{
//失败跳转回登录页面
//out.println("登录失败");
request.setAttribute("error","用户名或者密码错误");
// RequestDispatcher rd = request.getRequestDispatcher("/admin/login.jsp");
// rd.forward(request,response);
//RequestDispatcher rd = request.getRequestDispatcher("http://localhost:8080/myservlet2/admin/login.jsp");
//RequestDispatcher rd = request.getRequestDispatcher("/admin/login.jsp");
//http://192.168.58.250:8080/myservlet2/servlet/admin/login.jsp
RequestDispatcher rd = request.getRequestDispatcher("../admin/login.jsp");
rd.forward(request,response);
}
}
}