菜品信息的数据去哪里了?
package homework2.servlet; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import homework2.domain.Food; import homework2.utils.UploadUtils; /** * 菜品添加的Servlet */ @WebServlet("/FoodAddServlet") public class FoodAddServlet extends HttpServlet { List<Food> foodList = new ArrayList<Food>(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 数据的接收 // 文件上传基本操作: try { //定义一个Map集合用于保存接收到的数据: Map<String, String> map = new HashMap<String, String>(); // 1、创建一个磁盘文件项工厂对象。 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); // 2、创建一个核心解析类。 ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); // 3、解析request请求,返回的是List集合,其中存放的是FileItem对象。 List<FileItem> list = servletFileUpload.parseRequest(request); // 4、遍历集合,获得每个FileItem,判断表单项还是文件上传项。 String foodImage = null; for(FileItem fileItem:list) { // 判断是表单项还是文件上传项 if(fileItem.isFormField()) { // 普通表单项:接收表单项参数的值: // 获得表单的name的属性 String name = fileItem.getFieldName(); // 获得表单项的值 String value = fileItem.getString("UTF-8"); map.put(name, value); System.out.println(name+" "+value); } else { // 文件上传项:文件上传功能 // 获得文件上传的名称: String fileName = fileItem.getName(); if(fileName != null && !"".equals(fileName)) { // 通过工具类获得唯一文件名: String uuidFileName = UploadUtils.getUUIDFileName(fileName); // 获得文件上传的数据: InputStream is = fileItem.getInputStream(); // 获得文件上传的路径: String path = this.getServletContext().getRealPath("/upload"); // 将输入流对接到输出流就可以了 foodImage = path+"\\"+uuidFileName; OutputStream os = new FileOutputStream(foodImage); int len = 0; byte[] b = new byte[1024]; while((len = is.read(b))!=-1) { os.write(b, 0, len); } is.close(); os.close(); } } } // // 获得ServletContext对象 // List<Food> foodList = (List<Food>)this.getServletContext().getAttribute("list"); // // 校验用户名: // for(Food f:foodList) { // if(f.getFoodName().equals(map.get("foodName"))) { // request.setAttribute("msg", "此菜品已存在"); // request.getRequestDispatcher("/server.jsp").forward(request,response); // return ; // } // } // 封装数据到food当中: Food food = new Food(); food.setId(Integer.parseInt(map.get("id"))); food.setFoodName(map.get("foodName")); food.setTaste(map.get("taste")); // food.setFoodImage(map.get("foodImage")); food.setFoodImage(foodImage); food.setPrice(Double.parseDouble(map.get("price"))); food.setDescription(map.get("description")); // 将添加的菜品信息存入到List集合中 foodList.add(food); for(Food f:foodList) { System.out.println(f); } this.getServletContext().setAttribute("list", foodList); // 菜品添加成功,跳转到菜品查询页面: // 存入菜品信息: request.getSession().setAttribute("id", food.getId()); request.getSession().setAttribute("foodName", food.getFoodName()); // request.getSession().setAttribute("taste", food.getTaste()); // request.getSession().setAttribute("foodImage", food.getFoodImage()); // request.getSession().setAttribute("price", food.getPrice()); // request.getSession().setAttribute("description", food.getDescription()); request.getSession().setAttribute("foodList", foodList); response.sendRedirect(request.getContextPath()+"/showFoodList.jsp"); } catch (FileUploadException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
这个是我添加菜品信息的servlet,我想问我添加的菜品存在了哪里?
因为现在需要做菜品修改的部分。我修改的数据和之前已添加的数据,总是对应不起来?听老师帮忙告之。谢谢。
1
收起
正在回答 回答被采纳积分+1
1回答
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星