菜品信息的数据去哪里了?

菜品信息的数据去哪里了?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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回答
好帮手慕柯南 2019-05-13 18:44:11

同学你好!同学在在添加之后

http://img1.sycdn.imooc.com//climg/5cd949b10001ea4d06540326.jpg

  1. 看同学的代码在ServletContext中存了一份

  2. 将 id和foodName存在了Seesion中

  3. 又将foodList在Seesion中存了一份

建议同学将全局的数据放入ServletContext中,要返回给页面的数据放入Seesion中,比如所有的菜品放入ServletContext中,根据条件查询出来的放入Seesion中返回给页面

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 讓倪飛翔 #1
    是不是可以把2去掉,直接写3,也就是直接将foodList在Seesion中保存?
    2019-05-13 18:56:04
  • 好帮手慕柯南 回复 提问者 讓倪飛翔 #2
    同学可以把2去掉,建议同学将ServletContext的数据也更新一下呢,感觉同学后面一直在用它。祝学习愉快~
    2019-05-13 19:00:22
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码