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() 
	{
		
	}
}
最近下载更多
你爹正在加载中  LV4 2023年7月19日
ZDM133  LV2 2023年5月22日
wangdongtai  LV31 2022年5月20日
ll23344y  LV2 2022年2月15日
夜星1111  LV1 2022年1月6日
hshhshh  LV1 2021年12月26日
Start1  LV15 2021年11月25日
ssssha  LV1 2021年7月6日
ericxu1116  LV24 2021年6月28日
Rovann  LV1 2021年6月20日
最近浏览更多
zayuchuan 1月3日
暂无贡献等级
asddwh  LV12 2023年12月26日
hanzhuohong  LV1 2023年12月25日
komorebi123987  LV5 2023年12月9日
小芳同学  LV1 2023年12月5日
去码头整点薯条  LV3 2023年11月15日
wbx666  LV1 2023年9月25日
你爹正在加载中  LV4 2023年7月19日
29693748  LV1 2023年7月6日
yewang666  LV1 2023年6月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友