📄 materialcontroller.java
字号:
package controller.material;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.DealMaterial;
import beans.Material;
public class MaterialController extends HttpServlet {
/**
* Constructor of the object.
*/
public MaterialController() {
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 {
response.setContentType("text/html");
request.setCharacterEncoding("GBK");
String action = request.getParameter("action");
HttpSession mySession = request.getSession();
int type = 0;
if(mySession.getAttribute("type")!= null){
type = Integer.parseInt(mySession.getAttribute("type").toString());
}
if(type!=1){
mySession.setAttribute("errorMsg", "不具有权限操作主料");
response.sendRedirect("../main.jsp");
return;
}
if(action == null){
action = "query";
}
if(action.equals("add")){
doAddMaterial(request,response);
return;
}
if(action.equals("delete")){
doDeleteMaterial(request,response);
return;
}
if(action.equals("query")){
doQueryMaterial(request,response);
return;
}
}
private void doQueryMaterial(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
DealMaterial temp = new DealMaterial();
String errorMsg = temp.getMaterial();
Vector materials = temp.getVector();
HttpSession mySession = request.getSession();
mySession.setAttribute("errorMsg", errorMsg);
request.setAttribute("allMaterials", materials);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/material/showMaterials.jsp");
rd.forward(request, response);
return;
}
private void doDeleteMaterial(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
// TODO Auto-generated method stub
String delete = (String)request.getParameter("materialID");
HttpSession mySession = request.getSession();
if(delete==null){
mySession.setAttribute("errorMsg", "操作错误");
response.sendRedirect("../main.jsp");
return;
}
Material deleteMaterial = new Material();
deleteMaterial.setMaterialID(delete);
deleteMaterial.getMaterialData(delete);
String errorMsg = deleteMaterial.delete();
if(errorMsg == null){
errorMsg = "删除成功";
mySession.setAttribute("errorMsg", errorMsg);
}else{
mySession.setAttribute("errorMsg", errorMsg);
}
request.setAttribute("material", deleteMaterial);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/material/show.jsp");
rd.forward(request, response);
return;
}
private void doAddMaterial(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
// TODO Auto-generated method stub
String add = (String)request.getParameter("materialName");
HttpSession mySession = request.getSession();
if(add==null){
mySession.setAttribute("errorMsg", "操作错误");
response.sendRedirect("../main.jsp");
return;
}
Material addMaterial = new Material();
addMaterial.setName(add);
String errorMsg = addMaterial.addNewMaterial();
if(errorMsg == null){
errorMsg = "添加成功";
mySession.setAttribute("errorMsg", errorMsg);
}else{
mySession.setAttribute("errorMsg", errorMsg);
}
request.setAttribute("material", addMaterial);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin/material/show.jsp");
rd.forward(request, response);
return;
}
/**
* 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 {
response.setContentType("text/html");
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -