当前位置 : 主页 > 编程语言 > c++ >

将操作同种实体类的servlet封装在一起

来源:互联网 收集:自由互联 发布时间:2021-07-03
UsersServlet package com.web.Servlet;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.List;import javax.servlet.ServletException;import javax.servlet.annotation.W
UsersServlet
package com.web.Servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

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 com.web.Bean.Users;
import com.web.Biz.IUsersBiz;
import com.web.Biz.impl.RolesBiz;
import com.web.Biz.impl.UsersBiz;
import com.web.Tools.PNTools;

@WebServlet("/UsersServlet")
public class UsersServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IUsersBiz biz = new UsersBiz();
	Class c = UsersServlet.class;
	public UsersServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		// 解决编码问题
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String type = request.getParameter("type");
		for (Method mt : c.getDeclaredMethods()) {
			if (mt.getName().equalsIgnoreCase(type)) {
				System.out.println(mt.getName());
				try {
					mt.invoke(this, request,response);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
		
	}

	public void deleteServlet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获得传递的参数
		int id = Integer.parseInt(request.getParameter("id"));
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		// 进行删除操作并验证是否删除成功
		if (biz.DeleteUsers(id) >= 1) {
			response.sendRedirect("UsersServlet?type=GetUsersAll&pageIndex=" + pageIndex + "&pageSize=" + pageSize + "");
		}
	}

	public void getUsersAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 从其他组件获得参数
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = Integer
				.parseInt(request.getParameter("pageSize") == null ? "10" : request.getParameter("pageSize"));
		List
 
   list = biz.GetUsersPage(pageIndex, pageSize);
		// 创建和获得分页导航的参数
		int count = biz.GetUsersCount();
		int endPageIndex = (int) Math.ceil((double) count / pageSize);
		String pnString = PNTools.paging(count, pageSize, pageIndex, "UsersServlet?type=GetUsersAll");
		// 将参数放进request并转发至manager.jsp
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("pnString", pnString);
		request.setAttribute("endPageIndex", endPageIndex);
		request.setAttribute("list", list);
		request.setAttribute("pageSize", pageSize);
		request.getRequestDispatcher("manager.jsp").forward(request, response);
	}

	public void loginServlet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 接受从登录界面传来的参数
		String id = request.getParameter("id");
		String pwd = request.getParameter("pwd");
		// 基本验证
		if (id != null && !"".equals(id) && pwd != null && !"".equals(pwd)) {
			// 内容验证 (过滤特殊字符,脏数据) 此处省略部分代码
			int sid = Integer.parseInt(id);
			Users info = biz.Login(sid, pwd);
			if (info != null) {
				// 将登录成功的用户设入session,用以验证合法用户
				request.getSession().setAttribute("info", info);
				response.sendRedirect("UsersServlet?type=GetUsersAll&pageIndex=1&pageSize=10");
			} else {
				request.setAttribute("msg", "用户名或者密码错误!");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		} else {
			request.setAttribute("msg", "用户名或者密码不能为空!");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}

	public void modifyServlet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获得传递的参数
		String id = request.getParameter("id");
		String pwd = request.getParameter("pwd");
		String name = request.getParameter("name");
		String roleid = request.getParameter("roleid");
		// 基本验证
		if (id != null && !"".equals(id) && pwd != null && !"".equals(pwd) && name != null && !"".equals(name)) {
			int sid = Integer.parseInt(id);
			int rid = Integer.parseInt(roleid);
			// 进行修改操作并验证是否成功
			if (biz.ModifyUsers(sid, pwd, name, rid) >= 1) {
				request.setAttribute("msg", "");
				getUsersAll(request, response);
			} else {
				request.setAttribute("msg", "");
				getUsersAll(request, response);
			}
		}
	}

	public void registerServlet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获得传递的参数
		String id = request.getParameter("id");
		String pwd = request.getParameter("pwd");
		String name = request.getParameter("name");
		String roleid = request.getParameter("roleid") == "" ? "1" : request.getParameter("roleid");
		// 基本验证
		if (id != null && !"".equals(id) && pwd != null && !"".equals(pwd) && name != null && !"".equals(name)) {
			int sid = Integer.parseInt(id);
			int rid = Integer.parseInt(roleid);
			// 进行注册操作并验证是否成功
			if (biz.Register(sid, pwd, name, rid) >= 1) {
				request.setAttribute("msg", "");
				request.getRequestDispatcher("/register.jsp").forward(request, response);
			} else {
				request.setAttribute("msg", "");
				request.getRequestDispatcher("/register.jsp").forward(request, response);
			}
		} else {
			request.setAttribute("msg", "用户名或者密码等等不能为空!");
			request.getRequestDispatcher("/register.jsp").forward(request, response);
		}

	}

	public void toUpdateUsers(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		int id = Integer.parseInt(request.getParameter("id"));
		Users info = biz.getUserById(id);
		request.setAttribute("list", new RolesBiz().getRolesAll());
		request.setAttribute("info", info);
		request.getRequestDispatcher("modify.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
 
网友评论