⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elecinfo.java

📁 JSP移动商品管理平台源代码.........
💻 JAVA
字号:
package imis_elec;

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;


public class ElecInfo extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String pattern = request.getParameter("pattern");
		
		if(pattern.equals("insert")) {
			this.insert(request, response);
		}else if(pattern.equals("search")) {
			this.search(request, response);
		}else if(pattern.equals("getElecInfo")) {
			this.getElecInfo(request, response);
		}else if(pattern.equals("modify")) {
			this.modify(request, response);
		}else if(pattern.equals("delete")) {
			this.delete(request, response);
		}

	}
	
	/**
	 *
	 * 这个方法向表“ElecInfo”中插入数据
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void insert(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException{
		
		boolean flag = false;
		String url = "../imis_elec/fail.jsp";
		
		ElecInfoTO elecInfoTO = new ElecInfoTO();
		elecInfoTO.setUserId(request.getParameter("UserId").trim());
		elecInfoTO.setUserName(request.getParameter("UserName").trim());
		elecInfoTO.setElecType(request.getParameter("ElecType").trim());
		elecInfoTO.setUsedBy(request.getParameter("UsedBy").trim());
		elecInfoTO.setAddr(request.getParameter("Addr").trim());
		elecInfoTO.setDepa(request.getParameter("Depa").trim());
		elecInfoTO.setElecGre(request.getParameter("ElecGre").trim());
		elecInfoTO.setRunCap(request.getParameter("RunCap").trim());
		elecInfoTO.setRemark(request.getParameter("Remark").trim());
		elecInfoTO.setBuyDep(request.getParameter("buyDep").trim());
		
		
		ElecInfoDAOImpl elecInfoDAO = (ElecInfoDAOImpl) DAOFactory.getInstance().getElecInfoDAO();
		
		try {
			ElecInfoTO tempTO = elecInfoDAO.getElecInfo(elecInfoTO.getUserId());
			if(tempTO == null) {
				flag = elecInfoDAO.insertElecInfo(elecInfoTO);
			} else {
				url = "../imis_elec/elecInfo_insert.jsp";
				request.setAttribute("ElecInfo", elecInfoTO);
				request.setAttribute("UserIdExisted", "户号已存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}			
		
		if(flag == true) {
			request.setAttribute("success", "用户信息录入成功!");
			request.getRequestDispatcher("../imis_elec/successful.jsp").forward(request, response);
		} else {
			request.setAttribute("fail", "用户信息录入失败!");
			request.getRequestDispatcher(url).forward(request, response);
		}
	}
	
	/**
	 *
	 * 这个方法向表“ElecInfo”中查找数据
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */	
	public void search(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException{
		
		ArrayList list = new ArrayList();
		
		ElecInfoTO elecInfoTO = new ElecInfoTO();
		elecInfoTO.setUserId(request.getParameter("UserId").trim());
		elecInfoTO.setUserName(request.getParameter("UserName").trim());
		elecInfoTO.setElecType("");
		elecInfoTO.setUsedBy(request.getParameter("UsedBy").trim());
		elecInfoTO.setAddr("");
		elecInfoTO.setDepa("");
		elecInfoTO.setElecGre("");
		elecInfoTO.setRunCap("");
		elecInfoTO.setRemark("");
		elecInfoTO.setBuyDep(request.getParameter("buyDep").trim());
		
		
		//System.out.println(request.getParameter("buyDep").trim());
		
		ElecInfoDAOImpl elecInfoDAO = (ElecInfoDAOImpl) DAOFactory.getInstance().getElecInfoDAO();
		try {
			list = elecInfoDAO.searchElecInfos(elecInfoTO);
		} catch (Exception e) {
			e.printStackTrace();
		}			
		
		request.getSession().setAttribute("ElecInfoList", list);
		request.getRequestDispatcher("../imis_elec/elecInfoBySearch.jsp").forward(request, response);
	}
	
	/**
	 *
	 * 这个方法向表“ElecInfo”中得到信息,这个方法会被修改和删除页面调用,根据参数“page”来判断
	 * 是哪个页面。
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */	
	public void getElecInfo(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException{
				
		ElecInfoTO elecInfoTO = null;
		String userID = request.getParameter("UserId");
		
		ElecInfoDAOImpl elecInfoDAO = (ElecInfoDAOImpl) DAOFactory.getInstance().getElecInfoDAO();
		try {
			elecInfoTO = elecInfoDAO.getElecInfo(userID);
		} catch (Exception e) {
			e.printStackTrace();
		}			
		
		if(elecInfoTO != null) {
			request.setAttribute("ElecInfo", elecInfoTO);
		} else {
			request.setAttribute("UserIdUnexisted", "户号不存在!");
		}
		
		if(request.getParameter("page").equals("modify")) {
			request.getRequestDispatcher("../imis_elec/elecInfoModify.jsp").forward(request, response);
		} else if(request.getParameter("page").equals("delete")) {
			request.getRequestDispatcher("../imis_elec/elecInfoDelete.jsp").forward(request, response);
		}
	}
	
	/**
	 *
	 * 这个方法向表“ElecInfo”中修改数据
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */	
	public void modify(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException{
				
		boolean flag = false;
		boolean useIdExist = false;
		
		ElecInfoTO elecInfoTO = new ElecInfoTO();
		elecInfoTO.setUserId(request.getParameter("UserId").trim());
		elecInfoTO.setUserName(request.getParameter("UserName").trim());
		elecInfoTO.setElecType(request.getParameter("ElecType").trim());
		elecInfoTO.setUsedBy(request.getParameter("UsedBy").trim());
		elecInfoTO.setAddr(request.getParameter("Addr").trim());
		elecInfoTO.setDepa(request.getParameter("Depa").trim());
		elecInfoTO.setElecGre(request.getParameter("ElecGre").trim());
		elecInfoTO.setRunCap(request.getParameter("RunCap").trim());
		elecInfoTO.setRemark(request.getParameter("Remark").trim());
		elecInfoTO.setBuyDep(request.getParameter("BuyDep").trim());
		
		
		ElecInfoDAOImpl elecInfoDAO = (ElecInfoDAOImpl) DAOFactory.getInstance().getElecInfoDAO();
		try {
			if(request.getParameter("oldUserId").equals(elecInfoTO.getUserId())) {
				flag = elecInfoDAO.updateElecInfo(elecInfoTO);
			} else if(elecInfoDAO.getElecInfo(elecInfoTO.getUserId()) == null) {
				flag = elecInfoDAO.deleteElecInfo(request.getParameter("oldUserId"));
				flag = elecInfoDAO.insertElecInfo(elecInfoTO);
			} else {
				useIdExist = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}			
		
		if(flag == true) {
			request.setAttribute("success", "您已成功修改信息!");
			request.getRequestDispatcher("../imis_elec/successful.jsp").forward(request, response);
		} else {
			if(useIdExist) {
				request.setAttribute("UserIdExisted", "户号已存在");
				request.getRequestDispatcher("../imis_elec/elecInfoModify.jsp").forward(request, response);
			} else {
				request.setAttribute("fail", "您的修改失败了!");
				request.getRequestDispatcher("../imis_elec/fail.jsp").forward(request, response);
			}
		}

	}
	
	/**
	 *
	 * 这个方法向表“ElecInfo”中删除数据
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */	
	public void delete(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException{
				
		boolean flag = false;
		
		String userId = request.getParameter("UserId");
		
		ElecInfoDAOImpl elecInfoDAO = (ElecInfoDAOImpl) DAOFactory.getInstance().getElecInfoDAO();
		try {
			flag = elecInfoDAO.deleteElecInfo(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}			
		
		if(flag == true) {
			request.setAttribute("success", "您已成功删除信息!");
			request.getRequestDispatcher("../imis_elec/successful.jsp").forward(request, response);
		} else {
			request.setAttribute("fail", "您的删除失败了!");
			request.getRequestDispatcher("../imis_elec/fail.jsp").forward(request, response);
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -