package com.action; import java.io.IOException; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.DB; import com.orm.Tuser; public class user_servlet extends HttpServlet { @Override public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException { String type=req.getParameter("type"); if(type.endsWith("userReg")) { userReg(req, res); } if(type.endsWith("userLogout")) { userLogout(req, res); } if(type.endsWith("userEdit")) { userEdit(req, res); } if(type.endsWith("userMana")) { userMana(req, res); } if(type.endsWith("userDel")) { userDel(req, res); } if(type.endsWith("lutanGuanliyuanAdd")) { lutanGuanliyuanAdd(req, res); } if(type.endsWith("lutanGuanliyuan")) { lutanGuanliyuan(req, res); } } public void userReg(HttpServletRequest req,HttpServletResponse res) { String loginname=req.getParameter("loginname"); String loginpw=req.getParameter("loginpw"); String xuehao=req.getParameter("xuehao"); String name=req.getParameter("name"); String sex=req.getParameter("sex"); String age=req.getParameter("age"); int type=1; String id=null; String del="no"; String sql="insert into t_user(loginname,loginpw,xuehao,name,sex,age,type,del) values(?,?,?,?,?,?,?,?)"; Object[] params={loginname,loginpw,xuehao,name,sex,age,type,del}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); req.setAttribute("msg", "注册成功"); String targetURL = "/common/add_success.jsp"; dispatch(targetURL, req, res); } public void userLogout(HttpServletRequest req,HttpServletResponse res) { req.getSession().setAttribute("user", null); req.getSession().setAttribute("userType", null); String targetURL = "/qiantai/default.jsp"; dispatch(targetURL, req, res); } public void userEdit(HttpServletRequest req,HttpServletResponse res) { int id=Integer.parseInt(req.getParameter("id")); String loginname=req.getParameter("loginname"); String loginpw=req.getParameter("loginpw"); String name=req.getParameter("name"); String sex=req.getParameter("sex"); String age=req.getParameter("age"); String sql="update t_user set loginpw=?,name=?,sex=?,age=? where id=?"; Object[] params={loginpw,name,sex,age,id}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); req.setAttribute("msg", "修改成功,重新登录后生效"); String targetURL = "/common/add_success.jsp"; dispatch(targetURL, req, res); } public void userDel(HttpServletRequest req,HttpServletResponse res) { int id=Integer.parseInt(req.getParameter("id")); String sql="update t_user set del='yes' where id=?"; Object[] params={id}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); req.setAttribute("message", "操作成功"); req.setAttribute("path", "user?type=userMana"); String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res); } public void userMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { List userList=new ArrayList(); String sql="select * from t_user where del='no' and type=1"; Object[] params={}; DB mydb=new DB(); try { mydb.doPstm(sql, params); ResultSet rs=mydb.getRs(); while(rs.next()) { Tuser user=new Tuser(); user.setId(rs.getInt("id")); user.setLoginname(rs.getString("loginname")); user.setLoginpw(rs.getString("loginpw")); user.setXuehao(rs.getString("xuehao")); user.setName(rs.getString("name")); user.setSex(rs.getString("sex")); user.setAge(rs.getString("age")); user.setType(rs.getInt("type")); userList.add(user); } rs.close(); } catch(Exception e) { e.printStackTrace(); } mydb.closed(); req.setAttribute("userList", userList); req.getRequestDispatcher("admin/user/userMana.jsp").forward(req, res); } public void lutanGuanliyuanAdd(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { String loginname=req.getParameter("loginname"); String loginpw=req.getParameter("loginpw"); String xuehao=""; String name=""; String sex=""; String age=""; int type=100; String del="no"; String id=null; String sql="insert into t_user(loginname,loginpw,xuehao,name,sex,age,type,del) values(?,?,?,?,?,?,?,?)"; Object[] params={loginname,loginpw,xuehao,name,sex,age,type,del}; DB mydb=new DB(); mydb.doPstm(sql, params); mydb.closed(); req.setAttribute("message", "操作成功"); req.setAttribute("path", "user?type=lutanGuanliyuan"); String targetURL = "/common/success.jsp"; dispatch(targetURL, req, res); } public void lutanGuanliyuan(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { List lutanGuanliyuanList=new ArrayList(); String sql="select * from t_user where type=100"; Object[] params={}; DB mydb=new DB(); try { mydb.doPstm(sql, params); ResultSet rs=mydb.getRs(); while(rs.next()) { Tuser user=new Tuser(); user.setId(rs.getInt("id")); user.setLoginname(rs.getString("loginname")); user.setLoginpw(rs.getString("loginpw")); user.setType(rs.getInt("type")); lutanGuanliyuanList.add(user); } rs.close(); } catch(Exception e) { e.printStackTrace(); } mydb.closed(); req.setAttribute("lutanGuanliyuanList", lutanGuanliyuanList); req.getRequestDispatcher("admin/user/lutanGuanliyuan.jsp").forward(req, res); } public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response) { RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI); try { dispatch.forward(request, response); return; } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public void init(ServletConfig config) throws ServletException { super.init(config); } @Override public void destroy() { } }