📄 dishcontroller.java
字号:
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
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.DealAllDishSort;
import util.DealDish;
import util.DealMaterial;
import beans.Dish;
import beans.Material;
public class DishController extends HttpServlet {
/**
* Constructor of the object.
*/
public DishController() {
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");
HttpSession mySession = request.getSession();
String action = (String)request.getParameter("action");
int type = 0;
if(mySession.getAttribute("type")!= null){
type = Integer.parseInt(mySession.getAttribute("type").toString());
}
if(action == null){
action = "";
}
if(action.equals("query")){
if(type == 1){
doQueryDish(request,response);
response.sendRedirect("../admin/highQuery.jsp");
}else{
doLowQueryDish(request,response);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/lowQuery.jsp");
rd.forward(request, response);
return;
}
return;
}
if(type == 1 || type == 2){
if(type == 1&& action.equals("addDish")){
doAddDish(request,response);
return;
}
if(type == 1&& action.equals("modifyDish")){
doModifyDish(request,response);
return;
}
if(type == 1&& action.equals("deleteDish")){
doDeleteDish(request,response);
return;
}
if(type == 1&& action.equals("addMaterial")){
doAddMaterial(request,response);
return;
}
if(type == 1&&action.equals("modifyMaterial")){
doModifyMaterial(request,response);
return;
}
if(type == 1&&action.equals("materialOK")){
doMaterialOK(request,response);
return;
}
if(type == 1&&action.equals("delete")){
doDeleteDish(request,response);
return;
}
if(type == 1&&action.equals("ok")){
doOkDish(request,response);
return;
}
if(type == 1&&action.equals("cancelModifyDish")){
doCancelModifyDish(request,response);
return;
}
if(type == 1&&action.equals("modifyDishOK")){
doModifyDishOK(request,response);
return;
}
if(type == 1&&action.equals("deleteDishOK")){
doDeleteDishOK(request,response);
return;
}
}
mySession.setAttribute("errorMsg", "信息错误,重新登陆");
response.sendRedirect("../main.jsp");
}
private void doLowQueryDish(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
String dishSort = request.getParameter("dishSorts");
DealDish temp = new DealDish();
if(dishSort == null){
dishSort ="all";
}
String mains = request.getParameter("mains");
if(mains == null){
mains = "all";
}
if(dishSort.equals("all")&&mains.equals("all")){
String errorMsg = temp.getDishes();
}else if(dishSort.equals("all")&&(!mains.equals("all"))){
String errorMsg = temp.getDisherByMain(mains);
}else if((!dishSort.equals("all"))&&mains.equals("all")){
String errorMsg = temp.getDishesBySort(dishSort);
}else{
String errorMsg = temp.getDishes(dishSort, mains);
}
Vector dishes = new Vector();
int i = 0;
while(temp.isIn(i)){
dishes.add(temp.getDish(i));
i++;
}
Hashtable dishsInfor;
//生成showType 数组
dishsInfor = new Hashtable();
Vector showType = new Vector();
DealAllDishSort dealSort = new DealAllDishSort();
dealSort.generateDish();
showType = dealSort.getDishesSort();
Vector materialsName = new Vector();
DealMaterial ma = new DealMaterial();
String errorMsg = ma.getMaterial();
i = 0;
while(ma.hasMore(i)){
materialsName.add(ma.getMaterial(i).getName());
i++;
}
dishsInfor.put("showType", showType);
dishsInfor.put("materialsName", materialsName);
dishsInfor.put("dishes", dishes);
request.setAttribute("dishesInfor", dishsInfor);
}
private void doQueryDish(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
//生成dishes数组
String dishSort = request.getParameter("dishSorts");
DealDish temp = new DealDish();
if(dishSort == null){
dishSort ="all";
}
String mains = request.getParameter("mains");
if(mains == null){
mains = "all";
}
if(dishSort.equals("all")&&mains.equals("all")){
String errorMsg = temp.getDishes();
}else if(dishSort.equals("all")&&(!mains.equals("all"))){
String errorMsg = temp.getDisherByMain(mains);
}else if((!dishSort.equals("all"))&&mains.equals("all")){
String errorMsg = temp.getDishesBySort(dishSort);
}else{
String errorMsg = temp.getDishes(dishSort, mains);
}
Vector dishes = new Vector();
int i = 0;
while(temp.isIn(i)){
dishes.add(temp.getDish(i));
i++;
}
HttpSession mySession = request.getSession();
Hashtable dishsInfor;
//生成showType 数组
dishsInfor = new Hashtable();
Vector showType = new Vector();
DealAllDishSort dealSort = new DealAllDishSort();
dealSort.generateDish();
showType = dealSort.getDishesSort();
Vector materialsName = new Vector();
DealMaterial ma = new DealMaterial();
String errorMsg = ma.getMaterial();
i = 0;
while(ma.hasMore(i)){
materialsName.add(ma.getMaterial(i).getName());
i++;
}
dishsInfor.put("showType", showType);
dishsInfor.put("materialsName", materialsName);
dishsInfor.put("dishes", dishes);
mySession.setAttribute("dishesInfor", dishsInfor);
}
//编写成功但未测试
private void doDeleteDishOK(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
Hashtable dishInfor = (Hashtable)mySession.getAttribute("dishInfor");
if(dishInfor !=null){
String action = (String)dishInfor.get("action");
if(action != null && action.equals(("modify"))){
mySession.setAttribute("errorMsg", "不能修改新菜。请完成修改这个菜");
response.sendRedirect("../admin/showDishInfor.jsp");
return;
}
}
dishInfor = new Hashtable();
String dishID = request.getParameter("dishID");
if(dishID == null){
mySession.setAttribute("errorMsg", "没有给出正确的菜的ID");
response.sendRedirect("../admin/caipu.jsp");
return;
}
Dish temp = new Dish();
temp.getDetailInfo(dishID);
String errorMsg = temp.deleteDish();
if(errorMsg == null||errorMsg.equals("")){
mySession.setAttribute("errorMsg", "删除成功");
dishInfor.put("action", "delete");
dishInfor.put("dish", temp);
mySession.setAttribute("dishInfor", dishInfor);
response.sendRedirect("../admin/showDishInfor.jsp");
}else{
mySession.setAttribute("errorMsg", errorMsg);
response.sendRedirect("../admin/deleteDish.jsp");
}
}
//编写成功
//但未测试
private void doModifyDishOK(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
Hashtable dishInfor = (Hashtable)mySession.getAttribute("dishInfor");
if(dishInfor !=null){
String action = (String)dishInfor.get("action");
if(action != null && action.equals(("modify"))){
mySession.setAttribute("errorMsg", "不能添加新菜。请完成修改这个菜");
response.sendRedirect("../admin/showDishInfor.jsp");
return;
}
}
dishInfor = new Hashtable();
String dishID = request.getParameter("dishID");
if(dishID == null){
mySession.setAttribute("errorMsg", "没有给出正确的菜的ID");
response.sendRedirect("../admin/caipu.jsp");
return;
}
Dish temp = new Dish();
String errorMsg = temp.getDetailInfo(dishID);
mySession.setAttribute("errorMsg", errorMsg);
if(errorMsg != null){
response.sendRedirect("../admin/modifyDish.jsp");
return;
}
dishInfor.put("action", "modify");
dishInfor.put("dish", temp);
mySession.setAttribute("dishInfor", dishInfor);
response.sendRedirect("../admin/showDishInfor.jsp");
}
//当点击添加菜的功能是执行这个方法
//相当完善
private void doAddDish(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
HttpSession mySession = request.getSession();
Hashtable dishInfor = (Hashtable)mySession.getAttribute("dishInfor");
if(dishInfor == null){
dishInfor = new Hashtable();
dishInfor.put("action", "new");
mySession.setAttribute("dishInfor", dishInfor);
mySession.setAttribute("errorMsg","");
response.sendRedirect("../admin/showDishInfor.jsp");
return;
}else{
String action = (String)dishInfor.get("action");
if(action!=null){
if(action.equals("modify")){
mySession.setAttribute("errorMsg", "不能添加新菜。请完成修改这个菜");
response.sendRedirect("../admin/showDishInfor.jsp");
return;
}else{
dishInfor.put("action", "new");
mySession.setAttribute("dishInfor", dishInfor);
mySession.setAttribute("errorMsg","");
response.sendRedirect("../admin/showDishInfor.jsp");
return;
}
}else{
dishInfor.put("action", "new");
mySession.setAttribute("dishInfor", dishInfor);
mySession.setAttribute("errorMsg","");
response.sendRedirect("../admin/showDishInfor.jsp");
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -