根据菜品ID修改的问题,报null
后台报了空,很奇怪为什么取不到数据?我的菜品添加,菜品查询,菜品名称查询都可以实现的
package homework2.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 homework2.domain.Food; /** * 菜品修改(根据菜品ID进行修改)的Servlet */ @WebServlet("/FoodUpdateServlet") public class FoodUpdateServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 接收数据: String id = request.getParameter("id"); System.out.println(id); String foodname = request.getParameter("foodName"); System.out.println(foodname); String taste = request.getParameter("taste"); System.out.println(taste); // String foodimage = request.getParameter("foodImage"); // String price = request.getParameter("price"); // String description = request.getParameter("description"); // 从ServletContext域中获得保存菜品名称信息的集合: List<Food> list = (List<Food>) this.getServletContext().getAttribute("list"); for(Food food:list) { // 判断菜品ID是否正确 if(id.equals(food.getId())) { // 查询成功: request.getSession().setAttribute("foodName", foodname); System.out.println(foodname); request.getSession().setAttribute("taste", taste); System.out.println(taste); request.getSession().setAttribute("list", list); System.out.println(list); response.sendRedirect("/homework2/showFoodList.jsp"); return; } } // 查询失败: request.setAttribute("msg", "菜品名称错误"); request.getRequestDispatcher("/selectFoodByName.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<%@page import="homework2.domain.Food"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>菜品修改(根据菜品ID进行修改)</title> <style type="text/css"> </style> </head> <body> <% String foodId=""; if(session.getAttribute("foodId")!=null){ foodId = (String)session.getAttribute("foodId"); } String msg = ""; if(request.getAttribute("msg")!=null){ msg = (String)request.getAttribute("msg"); } %> <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>菜 名</td> <td><input type="text" name="foodName"></td> </tr> <tr> <td>口 味</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>价 格</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>
为什么数据居然是空,我很奇怪
0
收起
正在回答 回答被采纳积分+1
1回答
好帮手慕柯南
2019-05-07 11:52:38
同学你好!
首先我们看到报空指针的行数是第34行,
根据报错的信息定位到34行,原来是因为循环时抛出了一场,最终定位报空指针的原因是因为list为NULL,建议同学循环前判断一下list是否为NULL
2.修改时获取不到页面输入的值,是因为我们在form表单中,添加了enctype="multipart/form-data",这样会使数据变为二进制流,所以通过request.getParameter("");是无法获取到值的,老师的视频中讲过,要通过FileItem来获取值,同学如果不清楚可以再去看一下老师文件上传的讲解。
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星