老师,页面跳转后table没有显示出来?

老师,页面跳转后table没有显示出来?

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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%
 String basePath=request.getContextPath();
 request.setAttribute("basePath",basePath);
%>
<base href="<%=basePath%>/AddServlet">
<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");
   }
  %>
  <h3><font color="red"><%=msg %></font></h3>
  <form action="<%=basePath%>/AddServlet" 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>
 
 
 
 
package foodServlet;
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 foodMenu.Food;
import utils.UploadUtils;
/**
 * Servlet implementation class FoodServlet
 */
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  //数据接收
  //文件上传
  //1.创建磁盘文件工厂对象 2.创建核心解析类 3.解析request请求,返回List集合存放每一个表单项生成的FileItem对象
  try {
   //用于保存每次表单项传入的数据,否则会被下一次填写的覆盖,name和value的作用用于区分单复选框
   Map<String,String> map=new HashMap<String,String>();
    
   DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
   ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
   List<FileItem> list=servletFileUpload.parseRequest(request);
    
   //定义List集合,保存四种口味
   List<String> tasteList=new ArrayList<String>();
   //4.遍历,每一个FileItem代表每一个项
   String url=null;
   for(FileItem fileItem:list) {
    //判断是普通项还是文件上传项isFormField();
    if(fileItem.isFormField()) {
     //普通项
     String name=fileItem.getFieldName();//获取name属性值  getFieldName()方法
     String value=fileItem.getString("UTF-8");//获得value值  getString("UTF-8")
      
     System.out.println(name+"  "+value);
     //复选框
     if("taste".equals(name)) {
      String tasteValue=fileItem.getString("UTF-8");
      tasteList.add(tasteValue);
      tasteValue=tasteList.toString().substring(1,tasteList.toString().length()-1);
      System.out.println(name+"  "+tasteList);
      map.put(name, tasteValue);
     }else {
      map.put(name, value);
     }
      
    }else {
     //文件上传项:
     //获得文件上传名称getName();
     String fileName=fileItem.getName();
     if(fileName!=null&&!"".equals(fileName)) {
      //通过工具类获得唯一文件名:包名.调用方法
      String uuidFileName=UploadUtils.getUUIDFileName(fileName);
      //获得文件上传数据getInputStream();
      InputStream is=fileItem.getInputStream();
      //还要将文件弄到某个目录下面去,获取路径getServletContext().getRealPath();
      String path=this.getServletContext().getRealPath("/upload");
       
      //最后将输入流对接到输出流就可以了
      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();
     }
      
    }
   }
   System.out.println(map);
   //将菜品放入初始化InitServlet中预置好的ServletContext中
   List<Food> foodList=(List<Food>)this.getServletContext().getAttribute("list");
   //校验菜名
   for(Food food:foodList) {
    if(food.getName().equals(map.get("name"))) {
    request.setAttribute("msg""菜品已存在");
    request.getRequestDispatcher("/addFood.jsp").forward(request, response);
    return;
    }
   }
    
   //封装数据到food中
   Food food=new Food();
   food.setName(map.get("name"));
   food.setId(map.get("id"));
   food.setTaste(map.get("taste"));
   food.setAddress(map.get("address"));
   food.setPrice(map.get("price"));
   food.setDescribe(map.get("describe"));
   food.setPath(url);
    
   foodList.add(food);
   for(Food f:foodList) {
    System.out.println(f);
   }
   this.getServletContext().setAttribute("list", foodList);
   //菜品添加完成,重定向 跳转 到菜品查询页面   
   //获取工程名request.getContextPath()
   request.getSession().setAttribute("name",food.getName());
   request.getSession().setAttribute("id",food.getId());
   request.getSession().setAttribute("taste",food.getTaste());
   request.getSession().setAttribute("address",food.getPath());
   request.getSession().setAttribute("price",food.getPrice());
   request.getSession().setAttribute("describe",food.getDescribe());
   request.getSession().setAttribute("path",food.getPath());
   response.sendRedirect(request.getContextPath()+"/showFoodList.jsp");
    
  catch (FileUploadException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
   
   
   
 }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
  doGet(request, response);
 }
}
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%
 String basePath=request.getContextPath();
 request.setAttribute("basePath",basePath);
%>
<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 name="";
   String id="";
   String taste="";
   String address="";
   String price="";
   String describe="";
   String path="";
  if(session.getAttribute("name")!=null){
   name=(String)session.getAttribute("name");
   id=(String)session.getAttribute("id");
   taste=(String)session.getAttribute("taste");
   address=(String)session.getAttribute("address");
   price=(String)session.getAttribute("price");
   describe=(String)session.getAttribute("describe");
   path=(String)session.getAttribute("path");
   
  %>
  <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>
     
    <tr>
     <td><%=id %></td>
     <td><%=name %></td>
     <td><%=taste %></td>
     <td><img src=<%=path %>></td>
     <td><%=price%></td>
     <td><%=describe %></td>
    </tr>    
   </tbody>
  </table>
  <%
   
  %>
 </center>
</body>
</html>
  1. 老师那几个html怎么拼接起来?是按资料里面那个Frame做但没看懂。

  2. 页面跳转后table没有显示出来耶怎么办

  3. showFoodList.jsp里面有一段 可以简化吗,就是不放进session里面,直接getName( )这样子。String name="";
       String id="";
       String taste="";
       String address="";
       String price="";
       String describe="";
       String path="";
      if(session.getAttribute("name")!=null){
       name=(String)session.getAttribute("name");
       id=(String)session.getAttribute("id");
       taste=(String)session.getAttribute("taste");
       address=(String)session.getAttribute("address");
       price=(String)session.getAttribute("price");
       describe=(String)session.getAttribute("describe");
       path=(String)session.getAttribute("path");

正在回答

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

4回答

同学你好。首先非常抱歉没有能精准定位同学的问题,是我太草率了。下面涉及的图片是我拿同学的代码和老师的代码拼接修改得到的运行结果,所以行号会有所不同:


1、table显示不出来的原因是提交表单的时候是foodName所以map中只有foodName的项,没有name,所以每次存入的food中的name都为null:

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

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

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

而展示页面上,因为name为null,就不会进入if中,就不会展示table了:

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


2、输出两个taste 香辣,是因为分别在两个断点处输出,当name为taste时才会进入if,而tasteValue和前面的value是同一个值:

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


3、这里并没有死循环,不过确实循环了许多次。为了统计循环了多少次定义了一个count,并且将断点打在后面,可以看到我这张图片用了74次:

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


4、至于Session。确实是同学说的那样。是我糊涂了。再次向同学道歉。在同一个站点下,不关闭当前窗口的情况下,无论是重定向还是转发都不会丢失。除非是session的生存期到。但因为重定向可以直接跳转到另外的服务器,这时session就会丢失。

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

祝学习愉快~

提问者 bbb绑绑绑 2019-05-11 17:51:43

图2http://img1.sycdn.imooc.com//climg/5cd69a8f0001054b19201080.jpg

图1http://img1.sycdn.imooc.com//climg/5cd69a8f0001bcd319201080.jpg

另外按照老师吩咐将第130行的//response.sendRedirect(request.getContextPath()+"/showFoodList.jsp");改成request.getRequestDispatcher("/showFoodList.jsp").forward(request, response);好像也没有反应呢?


芝芝兰兰 2019-05-11 11:31:52

同学你好。同学说的地方老师使用的是ServletContext:

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

ServletContext是Servlet上下文。服务器启动的时候就会为每个web项目创建一个ServletContext对象,服务器停止时才释放。ServletContext不会因为刷新页面凭空消失。也不会因为重定向而消失。

但是ServletContext存在时间很长,频繁的写入读出会占用内存。所以这里还是建议同学使用转发哦。

如果解答了同学的疑问,望采纳~

祝学习愉快~


  • 提问者 bbb绑绑绑 #1
    1.老师是八分40秒时候的视频,将数据存到session里然后再写了重定向,还是不太清楚视频里的可以显示,麻烦老师再耐心解释一遍。 2.图片“图2”中为什么输出两个taste 香辣?第57行输出的一个第61行输出一个 3.图片“图3”中调试的时候85行和86行好像死循环了?
    2019-05-11 17:48:06
芝芝兰兰 2019-05-10 17:01:38

同学你好。下面来一条条解答你的疑惑:


1、Frame 标签定义了放置在每个框架中的 HTML 文档。但注意WEB-INF下的页面直接使用frame标签无法引入。WEB-INF下面的内容都是只能由服务器级别才能访问,客户端浏览器并不能访问。如果要在页面中访问另一个在WEB-INF下的页面。无论发起方在何处,都是需要通过Servlet来请求才能访问到。建议同学把公共页面放置在WEB-INF外哦。


在下面的这个例子中,设置了一个两列的框架集。第一列被设置为占据浏览器窗口的 25%,第二列被设置为占据浏览器窗口的 50%,第三列被设置为占据浏览器窗口的25%。HTML 文档 "frame_a.htm" 被置于第一个列中,而 HTML 文档 "frame_b.htm" 被置于第二个列中,HTML 文档 "frame_c.htm" 被置于第三个列中:

1
2
3
4
5
<frameset cols="25%,50%,25%">
   <frame src="frame_a.htm">
   <frame src="frame_b.htm">
   <frame src="frame_c.htm">
</frameset>


2、table没有显示出来是因为同学使用了重定向进行跳转,重定向之后,request中存储的全部信息都会丢失,包括session。所以这里需要使用转发request.getRequestDispatcher("/showFoodList.jsp").forward(request, response);


3、这里可以直接将food对象放入session中。然后使用<%=food.getName()%>


如果还有疑问,可以继续提问。如果解答了同学的疑问,望采纳~

祝学习愉快~


  • 提问者 bbb绑绑绑 #1
    咦,老师那教程中的视频“步骤二 · 3-6 文件上传及注册后页面跳转”8分钟的时候用的是重定向呢,那为什么可以视频里的可以显示呢?
    2019-05-10 23:08:05
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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