?? elecinfo.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);
}
}
/**
*
* 這個(gè)方法向表“ElecInfo”中插入數(shù)據(jù)
*
* @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);
}
}
/**
*
* 這個(gè)方法向表“ElecInfo”中查找數(shù)據(jù)
*
* @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);
}
/**
*
* 這個(gè)方法向表“ElecInfo”中得到信息,這個(gè)方法會被修改和刪除頁面調(diào)用,根據(jù)參數(shù)“page”來判斷
* 是哪個(gè)頁面。
*
* @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);
}
}
/**
*
* 這個(gè)方法向表“ElecInfo”中修改數(shù)據(jù)
*
* @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);
}
}
}
/**
*
* 這個(gè)方法向表“ElecInfo”中刪除數(shù)據(jù)
*
* @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
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -