请问一下查询页面出现这问题是怎么回事?

请问一下查询页面出现这问题是怎么回事?

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.imooc.food;
public class Food {
 private String id;
 private String foodName;
 private String taste;
 private String path;
 private String price;
 private String describe;
 public Food() {
   
 }
 public Food(String id, String foodName, String taste, String path, String price, String describe) {
  super();
  this.id = id;
  this.foodName = foodName;
  this.taste = taste;
  this.path = path;
  this.price = price;
  this.describe = describe;
 }
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getFoodName() {
  return foodName;
 }
 public void setFoodName(String foodName) {
  this.foodName = foodName;
 }
 public String getTaste() {
  return taste;
 }
 public void setTaste(String taste) {
  this.taste = taste;
 }
 public String getPath() {
  return path;
 }
 public void setPath(String path) {
  this.path = path;
 }
 public String getPrice() {
  return price;
 }
 public void setPrice(String price) {
  this.price = price;
 }
 public String getDescribe() {
  return describe;
 }
 public void setDescribe(String describe) {
  this.describe = describe;
 }
 @Override
 public String toString() {
  return "Food [id=" + id + ", foodName=" + foodName + ", taste=" + taste + ", path=" + path + ", price=" + price
    ", describe=" + describe + ", toString()=" super.toString() + "]";
 }
  
  
}
 
 
 
 
 
package com.imooc.food;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class FoodDaoImpl {
 private static final List<Food> db=new ArrayList<Food>();
 private String msgString;
 public static List<Food> getDb(){
  return db;
 }
  
 public String getMsgString() {
  return msgString;
 }
 public void setMsgString(String msgString) {
  this.msgString = msgString;
 }
 
    //添加菜品
 public void addFood(Food food) {
  db.add(food);
 }
 //查询所有菜品信息
 public List<Food> getAllFood(){
  if(db.isEmpty()) {
   System.out.println("菜单是空的");
   return null;
  }else {
   Iterator it=db.iterator();
   return db;
  }
   
   
 }
 //根据菜品名称查询菜品信息
 public Food getFoodByName(String foodName) {
  Food food=new Food();
  for(int i=0;i<db.size();i++) {
   if(db.get(i).getFoodName().equals(foodName)) {
    food=db.get(i);
    break;
   }
  }
  return food;
   
 }
 //根据菜品id查询菜品信息
 public Food getFoodById(String id) {
  Food food=new Food();
  for(int i=0;i<db.size();i++) {
   if(db.get(i).getId().equals(id)) {
    food=db.get(i);
    break;
   }
  }
  return food;
   
 }
 //菜品修改
 public void updateFood(Food newFood) {
  for(int i=0;i<db.size();i++) {
   if(db.get(i).getId().equals(newFood.getId())) {
    db.remove(i);
    db.add(i, newFood);
    System.out.println("菜品已更新完成!");
    break;
   }
  }
 }
 //根据菜品ID进行删除
 public void deleteFoodById(String id) {
  if(db.isEmpty()) {
   System.out.println("菜品是空的");
   return;
  }else {
   for(int i=0;i<db.size();i++) {
    if(db.get(i).getId().equals(id)) {
     db.remove(i);
     break;
    }
   }
  }
 }
}
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package com.imooc.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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 com.imooc.food.Food;
import com.imooc.food.FoodDaoImpl;
import com.imooc.utils.UploadUtils;
/**
 * Servlet implementation class FoodAddServlet
 */
@WebServlet("/FoodAddServlet")
public class FoodAddServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FoodAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  FoodDaoImpl FoodDaoImpl1=(FoodDaoImpl) this.getServletContext().getAttribute("FoodDaoImpl");
  List<Food> foodList=(List<Food>) this.getServletContext().getAttribute("foodList");
  if(foodList.isEmpty()) {
   System.out.println("从servletContext中获得的集合是空的");
  }
   try {
             //定义一个Map集合用于保存接收到的数据(即addFood.jsp提交的)
             Map<String, String> map = new HashMap<String, String>();
           //1、创建一个磁盘文件项工厂对象
             DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
             //2、创建一个核心的解析类
             ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
             //3、解析request请求,返回的是List集合,List集合中存放的是FileItem对象    
            List<FileItem> list = servletFileUpload.parseRequest(request);
            //4、遍历list,查看传递过来的数据是什么
             String url = null;
             for(FileItem fileItem:list) {
                 if(fileItem.isFormField()) {
                    //普通表单项:单选框、多选框、按钮
                     //接收表单项参数的值
                     String name = fileItem.getFieldName();//获得表单项的name属性的值
                     String value = fileItem.getString("UTF-8");
                     //存放在map中
                     map.put(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("/foodImage");
                        //将输入流对接到输出流
                        url=path+"\\"+uuidFileName;
                        OutputStream os=new FileOutputStream(url);
                        int len=0;
                        byte[] b=new byte[1024];
                        while((len=is.read(b))!=-1) {
                            os.write(b, 0, len);
                        }
                        is.close();
                        os.close();
                        }
                 }
             }
              
             if(foodList==null
                 System.out.println("从ContextServlet中获得的菜单是空的");
              
             if(foodList!=null) {
                 //检验网页上添加的菜名是否已存在
                 for(Food food:foodList) {
                     if(food.getId().equals(map.get("id"))) {
                         request.setAttribute("msg""菜名已存在");
                         //菜名已存在,跳转到添加界面
                         request.getRequestDispatcher("/addFood.jsp").forward(request, response);
                         return;
            
                 }
             }
          
             //新添加的菜名可上传,封装相关数据
             Food food = new Food();
             food.setId(map.get("id"));
             food.setFoodName(map.get("foodName"));
             food.setTaste(map.get("taste"));
             food.setPath(url);
             food.setPrice((map.get("price")));
             food.setDescribe(map.get("description"));
             if( FoodDaoImpl1==null) {
                FoodDaoImpl1=new FoodDaoImpl();  
             }
             //添加到全局对象FoodDaoImpl1中
             FoodDaoImpl1.addFood(food);
             this.getServletContext().setAttribute("FoodDaoImpl1", FoodDaoImpl1);
             //跳转到展示食物界面
             request.getRequestDispatcher("/showFoodList.jsp").forward(request, response);
             //response.sendRedirect(request.getContextPath()+"/showFoodList.jsp");
         }
              
                 catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 }
}
 
 
 
package com.imooc.servlet;
import java.io.IOException;
import java.util.List;
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 com.imooc.food.Food;
import com.imooc.food.FoodDaoImpl;
/**
 * Servlet implementation class FoodDeleteServlet
 */
@WebServlet("/FoodDeleteServlet")
public class FoodDeleteServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FoodDeleteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  FoodDaoImpl FoodDaoImpl1=(FoodDaoImpl)this.getServletContext().getAttribute("FoodDaoImpl1");
        List<Food> foodList=FoodDaoImpl1.getDb();
        String deleteFoodId = request.getParameter("id");
        String msg=FoodDaoImpl1.getMsgString();
        boolean flag=false;
        for(Food food:foodList) {
            if(food.getId().equals(deleteFoodId))
                flag=true;
        }
        if(flag==false) {
            msg="输入的id无效,请重新输入";
            request.setAttribute("msg",msg);
        }
        if(flag==true) {
        FoodDaoImpl1.deleteFoodById(deleteFoodId);
        this.getServletContext().setAttribute("FoodDaoImpl1", FoodDaoImpl1);
        msg="根据id删除菜品已完成";
        request.setAttribute("msg",msg);
        request.getRequestDispatcher("/showFoodList.jsp").forward(request, response);
        }
 }
}
 
 
 
package com.imooc.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
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 com.imooc.food.Food;
import com.imooc.food.FoodDaoImpl;
import com.imooc.utils.UploadUtils;
/**
 * Servlet implementation class FoodUpdateServlet
 */
@WebServlet("/FoodUpdateServlet")
public class FoodUpdateServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FoodUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("UTF-8");
        FoodDaoImpl FoodDaoImpl1=(FoodDaoImpl) this.getServletContext().getAttribute("FoodDaoImpl1");
        List<Food> foodList=FoodDaoImpl1.getDb();
        if(foodList.isEmpty()) {
            System.out.println("从ServletContext中获取的list是空的");
        }
        Map<String, String> map = new HashMap<String, String>();
         
        try {
            //1.创建磁盘工厂项文件
            DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
            //2.创建一个核心解析类
            ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
            //3.解析requst请求,返回的是List集合,List集合中存放的是FileItem对象
            List<FileItem> list = servletFileUpload.parseRequest(request);
            //4.遍历List集合,判断FileItem类型
             String url = null;
             for(FileItem fileItem:list) {
                 if(fileItem.isFormField()) {
                     String name=fileItem.getName();
                     String value=fileItem.getString("UTF-8");
                     map.put(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("/foodImage");
                         url=path+"\\"+uuidFileName;
                         OutputStream os=new FileOutputStream(url);
                         int len=0;
                         byte[] b=new byte[1024];
                         while((len=is.read(b))!=-1) {
                             os.write(b, 0, len);
                         }
                         is.close();
                         os.close();
                     }
                 }
             }
//              检验菜名
                Iterator<Food> iterator = foodList.iterator();
                boolean flag = false;
                Food testFood = null;
                while(iterator.hasNext()) {
                    testFood = iterator.next();
                    if(testFood.getId().equals(map.get("id"))) {
                        flag=true;
                        break;
                    }
                }
                if(flag==false) {
                    String msg = "您要修改的id:"+ map.get("id") + "菜单里没有";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher("/updateFood.jsp").forward(request, response);
                }else {
                    Food food = new Food();
                    food.setId(map.get("id"));
                    food.setFoodName(map.get("foodName"));
                    food.setTaste(map.get("taste"));
                    food.setPath(url);
                    food.setPrice((map.get("price")));
                    food.setDescribe(map.get("description"));
                    FoodDaoImpl1.updateFood(food);
                    System.out.println("更新后的food为"+food);
                    this.getServletContext().setAttribute(" FoodDaoImpl1",  FoodDaoImpl1);
                    request.getRequestDispatcher("/showFoodList.jsp").forward(request, response);
                   // response.sendRedirect(request.getContextPath()+"/showFoodList.jsp");
                }
        catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 }
}
 
 
 
package com.imooc.servlet;
 
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import com.imooc.food.Food;
import com.imooc.food.FoodDaoImpl;
/**
 * Servlet implementation class InitServlet
 */
public class InitServlet extends HttpServlet {
  
 @Override
 public void init() throws ServletException {
  //创建FoodDaoImpl对象
  FoodDaoImpl FoodDaoImpl1=new FoodDaoImpl();
  // 创建一个List集合用于保存用户注册的信息
  List<Food> foodList=FoodDaoImpl1.getDb();
  //将List保存到ServletContext作用域中
  this.getServletContext().setAttribute("list", foodList);
  this.getServletContext().setAttribute("foodList", foodList);
 }
  
  
}
 
 
 
 
package com.imooc.servlet;
import java.io.IOException;
import java.util.List;
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 com.imooc.food.Food;
import com.imooc.food.FoodDaoImpl;
/**
 * Servlet implementation class SelectServlet
 */
@WebServlet("/SelectServlet")
public class SelectServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SelectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("UTF-8");
        FoodDaoImpl FoodDaoImpl1=(FoodDaoImpl) this.getServletContext().getAttribute("FoodDaoImpl1");
        List<Food> list=FoodDaoImpl1.getDb();
        String foodName = request.getParameter("foodName");
        Food foodSearch=FoodDaoImpl1.getFoodByName(foodName);
        boolean flag=false;
        if(foodSearch!=null) {
            flag=true;
        }
        if(flag==false) {
               String msg = "对不起,您要查找的"+foodName+"我们家餐馆没有";
               request.setAttribute("msg", msg);
               request.getRequestDispatcher("/selectFoodByName.jsp").forward(request, response);
        }else {
            request.getSession().setAttribute("foodSearch", foodSearch);
            request.getRequestDispatcher("/showFoodList.jsp").forward(request, response);
        }
 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.imooc.utils;
import java.util.UUID;
public class UploadUtils {
 /**
  * 生成唯一的文件名 
  */
  public static String getUUIDFileName(String fileName) {
   //将文件名的前面部分进行截取:xx.jpg  -->  .jpg
   int idx=fileName.lastIndexOf(".");
   String extention=fileName.substring(idx);
   String uuidFileName=UUID.randomUUID().toString().replace("-""")+extention;
   return uuidFileName;
  }
}
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath(); //path是project的名字
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>添加菜品</title>
<style type="text/css">
</style>
</head>
<body>
 <center>
  <h1>菜品添加</h1>
  <%
        String msg="";
        if(request.getAttribute("msg")!=null){
            msg=(String)request.getAttribute("msg");
        }
         %>
         <h2 style="color: red;font-weight: bolder;">${requestScope.msg }</h2>
  <form action="<%=basePath%>/FoodAddServlet" method="post" enctype="multipart/form-data">
   <table border="1px" width="400px" cellspacing="0px" cellpadding="0px">
    <tr>
     <td>菜品&nbsp;ID</td>
     <td><input type="text" name="id"></td>
    </tr>
    <tr>
     <td>菜&nbsp;&nbsp;名</td>
     <td><input type="text" name="foodName"></td>
    </tr>
    <tr>
     <td>口&nbsp;&nbsp;味</td>
     <td>
      <input type="radio" name="taste" value="香辣">香辣
      <input type="radio" name="taste" value="微辣">微辣
      <input type="radio" name="taste" value="麻辣">麻辣
      <input type="radio" name="taste" value="不辣">不辣
     </td>
    </tr>
    <tr>
     <td>菜品图片</td>
     <td><input type="file" name="foodImage"></td>
    </tr>
    <tr>
     <td>价&nbsp;&nbsp;格</td>
     <td><input type="text" name="price"></td>
    </tr>
    <tr>
     <td>菜品描述</td>
     <td>
      <textarea name="description"></textarea>
     </td>
    </tr>
    <tr   style="text-align:center;width:20px">
     <td colspan="2">
      <input type="submit" value="添加">
      <input type="reset" value="重置">
     </td>
    </tr>
   </table>
  </form>
 </center>
</body>
</html>
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath(); //path是project的名字
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>菜品删除(根据ID删)</title>
<style type="text/css">
</style>
</head>
<body>
 <center>
  <h1>菜品删除(根据ID删除)</h1>
  <h2 style="color: red;font-weight: bolder;">${requestScope.msg }</h2>
  <form action="<%=basePath%>/FoodDeleteServlet" method="post">
   <table width="400px" border="1px" cellspacing="0px" cellpadding="0px">
    <tr>
     <td>菜品ID</td>
     <td><input type="text" name="id"></td>
    </tr>
    <tr>
     <td colspan="2" style="text-align:center"><input type="submit" value="删除"></td>
    </tr>
   </table>
  </form>
 </center>
</body>
</html>
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Document</title>
</head>
<body>
 <p><a href="addFood.jsp" target="main">菜品添加</a></p>
 <p><a href="selectFood.jsp" target="main">菜品查询</a></p>
 <p><a href="updateFood.jsp" target="main">菜品修改</a></p>
 <p><a href="deleteById.jsp" target="main">菜品删除</a></p>
</body>
</html>
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath(); //path是project的名字
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>菜品查询选项页面</title>
<style type="text/css">
</style>
</head>
<body>
 <center>
  <p><a href="/showFoodList.jsp">查询所有菜品信息</a></p>
  <p><a href="/selectFoodByName.jsp">菜名查询</a></p>
 </center>
</body>
</html>
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath(); //path是project的名字
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>菜名查询</title>
<style type="text/css">
</style>
</head>
<body>
 <center>
  <h1>菜名查询</h1>
  <%
            String msg = "";
            if(request.getAttribute("msg")!=null){
                msg = (String) request.getAttribute("msg");
            }
        %>
        <h2 style="color: red;font-weight: bolder;"><%=msg %></h2>
  <form action="<%=basePath%>/SelectServlet" method="post">
   <input type="hidden" name="type" value="2">
   <table width="400px" border="1px" cellspacing="0px" cellpadding="0px">
    <tr>
     <td>菜名</td>
     <td><input type="text" name="foodName"></td>
    </tr>
    <tr>
     <td colspan="2" style="text-align:center"><input type="submit" value="查询"></td>
    </tr>
   </table>
  </form>
 </center>
</body>
</html>
 
 
<%@page import="com.imooc.food.Food"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<%
    String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>菜品管理系统</title>
</head>
<frameset rows="20%,*">
 <frame src="./top.jsp"></frame>
 <frameset cols="10%,*">
  <frame src="./left.jsp"></frame>
  <frame name="main"></frame>
 </frameset>
</frameset>
</html>
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.imooc.food.Food"%>
<%@page import="com.imooc.food.FoodDaoImpl"%>
<%@page import="java.util.List"  %>
<!DOCTYPE html>
<html>
<head>
<%
    String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>菜品信息展示</title>
<style type="text/css">
</style>
</head>
<body>
 <center>
  <h1>菜品查询</h1>
  <table border="1px" cellspacing="0px" cellpadding="0px" width="800px">
   <thead>
    <tr>
     <th>菜品ID</th>
     <th>菜名</th>
     <th>口味</th>
     <th>菜品图片</th>
     <th>价格</th>
     <th>菜品描述</th>
    </tr>
   </thead>
   <tbody>
     <%
     FoodDaoImpl impl=new FoodDaoImpl();
     List<Food> list=impl.getAllFood();
     for(Food food:list){
      String name=food.getPath();
      System.out.print(name);
      int idx=food.getPath().lastIndexOf("\\");
      String img=food.getPath().substring(idx+1);
      
     %>
    <tr>
     <td><%=food.getId() %></td>
     <td><%=food.getFoodName() %></td>
     <td><%=food.getTaste() %></td>
     <td><img src="/menu_food/foodImage/<%=img %>"></td>
     <td><%=food.getPrice() %></td>
     <td><%=food.getDescribe() %></td>
    </tr>
    <%
     }
    %>
   </tbody>
  </table>
 </center>
</body>
</html>
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>菜品后台管理系统</title>
</head>
<body>
 <center>
  <h1>菜品后台管理系统</h1>
 </center>
</body>
</html>
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath(); //path是project的名字
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>菜品修改(根据菜品ID进行修改)</title>
<style type="text/css">
</style>
</head>
<body>
 <center>
  <h1>根据菜品ID修改</h1>
  <form action="<%=basePath%>/FoodUpdateServlet" method="post" enctype="multipart/form-data">
   <table border="1px" width="400px" cellspacing="0px" cellpadding="0px">
    <tr>
     <td>修改ID</td>
     <td><input type="text" name="id"></td>
    </tr>
    <tr>
     <td>菜&nbsp;&nbsp;名</td>
     <td><input type="text" name="foodName"></td>
    </tr>
    <tr>
     <td>口&nbsp;&nbsp;味</td>
     <td>
      <input type="radio" name="taste" value="香辣">香辣
      <input type="radio" name="taste" value="微辣">微辣
      <input type="radio" name="taste" value="麻辣">麻辣
      <input type="radio" name="taste" value="不辣">不辣
     </td>
    </tr>
    <tr>
     <td>菜品图片</td>
     <td><input type="file" name="foodImage"></td>
    </tr>
    <tr>
     <td>价&nbsp;&nbsp;格</td>
     <td><input type="text" name="price"></td>
    </tr>
    <tr>
     <td>菜品描述</td>
     <td>
      <textarea name="description"></textarea>
     </td>
    </tr>
    <tr   style="text-align:center;width:20px">
     <td colspan="2">
      <input type="submit" value="修改">
      <input type="reset" value="重置">
     </td>
    </tr>
   </table>
  </form>
 </center>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>menu_food</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>InitServlet</display-name>
    <servlet-name>InitServlet</servlet-name>
    <servlet-class>com.imooc.servlet.InitServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>InitServlet</servlet-name>
    <url-pattern>/InitServlet</url-pattern>
  </servlet-mapping>
   
</web-app>

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

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

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


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
好帮手慕阿满 2019-06-23 12:12:13

同学你好,同学是指修改菜品吗?在left.jsp中,点击修改菜品,应该跳转到updateFoodList.jsp中,然后在updateFoodList.jsp中和添加菜品的页面基本一样,输入要修改的菜品id,以及修改后的菜品信息,在FoodUpdateServlet类中,获取要修改的菜品id,并判断是否存在,存在则修改,不存在则给出提示。如果同学在修改的过程中有问题,建议同学详细描述一下具体问题。

祝:学习愉快~

好帮手慕柯南 2019-06-22 10:41:58

同学你好!

  1. 同学将查询出来的信息,放在了foodSearch中

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

    但是同学显示的依然是所有的菜品,并没有显示所查询的数据

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

    修改建议:查询是返回一个标识flag,如果flag=1,则显示查询出来的数据,同学可参考:

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

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

  2. 修改失败的原因是因为获取普通项时应该使用getFieldName,但是同学使用了getName

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

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

  • 提问者 weixin_慕哥8001276 #1
    老师,问题已解决,问一下这个菜品修改是什么意思?修改不了,请老师明示!谢谢
    2019-06-22 21:52:46
好帮手慕柯南 2019-06-21 15:42:19

同学你好!

  1. 既然查询时会把多个查询出来,那么说明同学根据菜品名称去集合中查找菜品时出现问题了呢,同学可以看一下查找的逻辑是否有误呢,以及servlet返回的是否是查询出来的信息。

  2. 修改,同学需要在集合中查到要修改的菜品,然后再修改呢

祝学习愉快~

  • 提问者 weixin_慕哥8001276 #1
    按照老师的方法去找了相关问题,但还是没找出问题,希望老师能帮我找出需要修改的地方进行修改,谢谢!
    2019-06-21 21:14:44
好帮手慕柯南 2019-06-21 11:46:51

同学你好!将路径前面的 /去掉就可以了呢

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

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

  • 提问者 weixin_慕哥8001276 #1
    能实现查询全部菜品,但是按菜名查询实现不了,当我添加了两个菜品时,查询其中一个菜名时,另一个也会显示出来,所以是什么问题呢?
    2019-06-21 15:27:01
  • 提问者 weixin_慕哥8001276 #2
    还有就是菜品修改也修改不了,是什么问题呢?
    2019-06-21 15:28:10
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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