package serv;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tools.PageBean;
import vo.UserVO;
import dao.UserDAO;
//ZH额LIYOU LEI PEIZHI WENJIAN LI MEIYOU 所以404
//你还需要去配置下web.xml
public class UserServlet extends HttpServlet {
private UserDAO dao;
private final String SHOW_ALL_URL="UserServlet?action=showall";
public void init() throws ServletException {
dao = new UserDAO();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
// 接收动作标识变量
String action = request.getParameter("action");
System.out.println(action);
if (action == null || action.trim().equals("")) {
out.print("请传入参数");
return;
}
if (action.equals("reg")) {
// 1 取值
String uname = request.getParameter("uname");
String pswd = request.getParameter("pswd");
// 2 组装vo
UserVO vo = new UserVO();
vo.setUname(uname);
vo.setPswd(pswd);
// 3 创建DAO
// UserDAO dao = new UserDAO();
// 4 执行方法
int r = dao.userReg(vo);
// 5 判断结果
switch (r) {
case -2:
out.print("yonghumingyijingcunzai");
break;
case 0:
case -1:
out.print("failed");
break;
default:
out.print("success");
}
}
if (action.equals("login")) {
// 1 取值
String uname = request.getParameter("uname");
String pswd = request.getParameter("pswd");
// 2 组装vo
UserVO vo = new UserVO();
// 此时 vo的userid=0
vo.setUname(uname);
vo.setPswd(pswd);
// 3 创建UserDAO
// UserDAO dao = new UserDAO();
// 4 执行方法
UserVO vovo = dao.userLogin(vo);
// 5 判断结果
if (vovo.getUserid() != 0) {
out.print("success");
} else {
out.print("failed");
}
}
if (action.equals("showall")) {
// UserDAO dao = new UserDAO();
ArrayList<UserVO> list = dao.userShowALL();
// 在request中存储一个list对象,名字是userall
request.setAttribute("userall", list);
request.getRequestDispatcher("showall.jsp").forward(request,
response);
}
if(action.equals("showallpage"))
{
String pagen=request.getParameter("pagenum");
int pagenum=0;
if(pagen==null||pagen.trim().equals("")){
pagenum=1;
}else
{
pagenum=Integer.parseInt(pagen);
}
PageBean pb=new PageBean(pagenum);
request.setAttribute("pb", pb);
request.getRequestDispatcher("showallpage.jsp").forward(request,response);
}
if (action.equals("del")) {
// 1 取得ID
String userid = request.getParameter("userid");
// System.out.println(userid);
// 2 DAO
// UserDAO dao = new UserDAO();
// 3 执行
int r = dao.userDelByID(userid);
// 4 结果
if (r == 1) {
out.print(this.msg("删除成功"));
out.print(this.gogo(SHOW_ALL_URL));
// 重定向到显示全部功能
// response.sendRedirect("UserServlet?action=showall");
} else {
out.print(this.msg("删除失败"));
out.print(this.gogo(SHOW_ALL_URL));
// out.print("f");
}
}
if (action.equals("showone")) {
// 1 取值
String userid = request.getParameter("userid");
// 2 创建对象
// UserDAO dao = new UserDAO();
// 3 调用方法
UserVO vo = dao.userFindByID(userid);
// 4 request存储
request.setAttribute("one", vo);
// 5 请求转发到修改表单页面
request.getRequestDispatcher("update.jsp").forward(request,
response);
}
if (action.equals("update")) {
// 1 取值 组装vo对象
UserVO vo = new UserVO();
vo.setUserid(Integer.parseInt(request.getParameter("userid")));
vo.setUname(request.getParameter("uname"));
vo.setPswd(request.getParameter("pswd"));
// 2 DAO对象
// UserDAO dao = new UserDAO();
// 2.5 判断用户名是否冲突
if (dao.unameIsExist(vo.getUname()) > 0) {
out.print("用户名已经存在");
return;
}
// 3 执行方法
int r = dao.userUpdate(vo);
// 4 处理结果
if (r > 0) {
response.sendRedirect("UserServlet?action=showall");
} else {
out.print("f");
}
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public String msg(String msg)
{
String s="";
s+="<script type='text/javascript'>";
s+="alert('"+msg+"');";
s+="</script>";
return s;
}
public String gogo(String url)
{
String s="";
s+="<script type='text/javascript'>";
s+="location.href='"+url+"';";
s+="</script>";
return s;
}
}