📄 materialtypeservlet.java
字号:
package imis_mate.servlet;
import imis_mate.bean.*;
import imis_mate.DAO.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cmis.common.Encoding;
public class MaterialTypeServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public MaterialTypeServlet() {
super();
}
/**
* 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("update")) {
//更新信息
this.update(request, response);
} else if(pattern.equals("select")) {
//查询信息
this.select(request,response);
} else if(pattern.equals("delete")) {
//删除信息
this.delete(request,response);
} else if(pattern.equals("newPage")) {
//新建页面(跳转到添加信息页面)
this.newPage(request,response);
}
}
private void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 向表中插入数据
boolean flag = false;
String url = FAIL_URL;
MaterialTypeBean mateType = new MaterialTypeBean();
mateType.setMaterialTypeNo(request.getParameter("materialTypeNo"));
mateType.setMaterialTypeName(request.getParameter("materialTypeName"));
mateType.setMaterialPaTypeNo(request.getParameter("materialPaTypeNo"));
mateType.setMaUnit(request.getParameter("maUnit"));
mateType.setInMin(request.getParameter("inMin"));
mateType.setInMax(request.getParameter("inMax"));
mateType.setReMark(request.getParameter("reMark"));
MaterialTypeDAO mateDAO = DAOFactory.getInstance().getMaterialTypeDAO();
try {
MaterialTypeBean mateTypeTEMP = mateDAO.selectMaterialType(mateType.getMaterialTypeNo());
if(mateTypeTEMP == null) {
flag = mateDAO.insertMaterialType(mateType);
} else {
request.setAttribute("MaterialType",mateType);
request.setAttribute("MaterialTypeNoExisted","物品类型编号已存在");
//request.getRequestDispatcher(INSERT_URL).forward(request,response);
url = INSERT_URL;
}
} catch (Exception e) {
e.printStackTrace();
}
if(flag == true) {
request.setAttribute("success", "信息添加成功");
request.getRequestDispatcher(SUCCESS_URL).forward(request, response);
} else {
request.setAttribute("fail", "信息添加失败");
request.getRequestDispatcher(url).forward(request,response);
}
}
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//向表中更新数据
boolean flag = false;
//String url = "../imis_mate/fail.jsp";
MaterialTypeBean mateType = new MaterialTypeBean();
mateType.setMaterialTypeNo(request.getParameter("materialTypeNo"));
mateType.setMaterialTypeName(request.getParameter("materialTypeName"));
mateType.setMaterialPaTypeNo(request.getParameter("materialPaTypeNo"));
mateType.setMaUnit(request.getParameter("maUnit"));
mateType.setInMax(request.getParameter("inMax"));
mateType.setInMin(request.getParameter("inMin"));
mateType.setReMark(request.getParameter("reMark"));
MaterialTypeDAO mateDAO = DAOFactory.getInstance().getMaterialTypeDAO();
try {
flag = mateDAO.updateMaterialType(mateType);
} catch (Exception e) {
e.printStackTrace();
}
if(flag == true) {
request.setAttribute("success", "物品类型更新成功");
request.getRequestDispatcher(SUCCESS_URL).forward(request, response);
} else {
request.setAttribute("fail","物品类型更新失败");
request.getRequestDispatcher(FAIL_URL).forward(request,response);
}
}
private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 查询表中数据
MaterialTypeBean materialType = null;
String materialTypeNo = request.getParameter("MaterialTypeNo");
MaterialTypeDAO mateDAO = DAOFactory.getInstance().getMaterialTypeDAO();
try {
materialType = mateDAO.selectMaterialType(materialTypeNo);
} catch (Exception e) {
e.printStackTrace();
}
if(materialType != null) {
request.setAttribute("MaterialType",materialType);
} else {
request.setAttribute("materialNoNull","物品类型编号不存在.");
}
if(request.getParameter("page").equals("update")) {
request.getRequestDispatcher(UPDATE_URL).forward(request, response);
}
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 删除表中数据
boolean flag = false;
boolean isSon = true;
String url = SUCCESS_URL;
String materialTypeNo = request.getParameter("materialTypeNo");
MaterialTypeBean mateBean = new MaterialTypeBean();
MaterialTypeDAO mateDAO = DAOFactory.getInstance().getMaterialTypeDAO();
try {
mateBean = mateDAO.selectMaterialType(materialTypeNo);
isSon = mateDAO.selectMaterialPaTypeNo(mateBean.getMaterialSQ());
if(!isSon) {
flag = mateDAO.deleteMaterialType(materialTypeNo);
}
} catch (Exception e) {
e.printStackTrace();
}
if(flag == true) {
request.setAttribute("success","成功删除信息");
//request.getRequestDispatcher(SUCCESS_URL).forward(request, response);
url = SUCCESS_URL;
} else {
if(!isSon){
request.setAttribute("fail","信息删除失败");
} else {
request.setAttribute("fail","信息删除失败,该物品类型下有子类型.");
}
url = FAIL_URL;
//request.getRequestDispatcher(FAIL_URL).forward(request,response);
}
request.getRequestDispatcher(url).forward(request,response);
}
private void newPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//新建页面(跳转到添加信息页面)
request.getRequestDispatcher(INSERT_URL).forward(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
private static final String SUCCESS_URL = "../imis_mate/successful.jsp"; //提交成功页面
private static final String FAIL_URL = "../imis_mate/fail.jsp"; //提交失败页面
private static final String INSERT_URL = "../imis_mate/MaterialTypeTable_insert.jsp"; //插入数据页面
private static final String UPDATE_URL = "../imis_mate/MaterialTypeTable_update.jsp";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -