package com.qf.Servlet;
import com.alibaba.druid.util.StringUtils;
import com.qf.bean.User;
import com.qf.service.UserService;
import com.qf.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;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
@WebServlet(name = "UserServlet",urlPatterns = "/user")
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//1.接受参数
String str = request.getParameter("method");
try {
Class cla = this.getClass();
Method method1 = cla.getDeclaredMethod(str, HttpServletRequest.class, HttpServletResponse.class);
method1.invoke(this, request, response);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("name");
String password = request.getParameter("password");
if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
request.setAttribute("msg1", "用户名或密码不能为空");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
UserService userService = new UserServiceImpl();
User user = userService.findByNameAndPassword(name, password);
if (user == null) {
request.setAttribute("msg", "用户名或密码错误");
//response.sendRedirect(contextPath+"/pages/user/login.jsp");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
// System.out.println(user.getState());
request.getSession().setAttribute("userstate", user.getState());
request.getSession().setAttribute("username", user.getName());
System.out.println(user.getName());
request.getRequestDispatcher("admin_index.jsp").forward(request, response);
}
public void findall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String index = request.getParameter("index");
Object power = request.getSession().getAttribute("userstate");
Integer userPower=Integer.parseInt(power.toString());
if (userPower==0){
request.setAttribute("msg", "抱歉,您的权限不足");
request.getRequestDispatcher("state.jsp").forward(request, response);
}
String uname = null;
if (username != null) {
uname = username;
}
int pageindex = 1;
if (index != null) {
pageindex = Integer.parseInt(index);
}
int pages = 3;
UserService userService=new UserServiceImpl();
int totalcount=userService.totalcounts(uname);
int totalPage = totalcount % pages > 0 ? totalcount / pages + 1 : totalcount / pages;
List<User> list=userService.findAll(uname,pageindex,pages);
request.setAttribute("uname", uname);
request.setAttribute("list", list);
System.out.println(list.toString());
request.setAttribute("pageindex", pageindex);
request.setAttribute("count", totalcount);//总条数
request.setAttribute("pages", totalPage);//总页数
request.getRequestDispatcher("userAdmin.jsp").forward(request, response);
}
public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String repwd = request.getParameter("repwd");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String mobile = request.getParameter("mobile");
String address = request.getParameter("address");
String auth = request.getParameter("auth");
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || StringUtils.isEmpty(sex)|| StringUtils.isEmpty(age)|| StringUtils.isEmpty(mobile)|| StringUtils.isEmpty(address)|| StringUtils.isEmpty(auth)) {
request.setAttribute("msg1", "全部为必填");
request.getRequestDispatcher("userAdd.jsp").forward(request, response);
}
if (password.equals(repwd)==false){
request.setAttribute("msg3", "两次密码必须一致");
request.getRequestDispatcher("userAdd.jsp").forward(request, response);
}
User user=new User(null, username, password, sex, Integer.parseInt(age), Integer.parseInt(mobile),address,Integer.parseInt(auth) );
UserService userService=new UserServiceImpl();
userService.adds(user);
response.sendRedirect("/user?method=findall");
}
public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserService userService =new UserServiceImpl();
String did = request.getParameter("did");
String state = request.getParameter("state");
int id=Integer.parseInt(did);
int states=Integer.parseInt(state);
if (states==1){
User user=new User(id,null,null , null, null, null, null, 0);
userService.updatestates(user);
response.sendRedirect("/user?method=findall");
}else if (states==0){
User user=new User(id,null,null , null, null, null, null, 1);
userService.updatestates(user);
response.sendRedirect("/user?method=findall");
}
}
public void back(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession().removeAttribute("username");
request.getSession().invalidate();
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}