由添加菜品界面跳转到显示界面出现404错误
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String basePath=request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+request.getContextPath()+"/"; %> <!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> <form action="<%=basePath%>/FoodAddServlet" 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>
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.tomcat.util.http.fileupload.RequestContext; import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory; import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload; import com.imooc.dish.Food; import com.imooc.dish.FoodDaoImpl; import com.imooc.utils.UploadUtils; /** * Servlet implementation class addFoodServlet */ @WebServlet("/addFoodServlet") public class FoodAddServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ 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集合,List集合中存放的是FileItem对象 List<org.apache.tomcat.util.http.fileupload.FileItem> list = servletFileUpload.parseRequest((RequestContext) request); //4. 遍历集合,获得每个FileItem,判断是表单项还是文件上传项 String url = null; for(org.apache.tomcat.util.http.fileupload.FileItem fileItem : list) { if(fileItem.isFormField()) { // 普通表单项:接收表单项参数的值 String name = fileItem.getFieldName(); // 获得表单项name属性的值 String value = fileItem.getString("UTF-8");// 获得表单项的值 //System.out.println(name + " " + value); 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("/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); //封装数据到Food中 Food food = new Food(); food.setFoodId(map.get("id")); food.setFoodName(map.get("foodName")); food.setTaste(map.get("taste")); food.setPrice(map.get("price")); food.setDescribe(map.get("description")); FoodDaoImpl fdi = new FoodDaoImpl(); fdi.addFood(food); //记录成功,跳转到显示菜品页面 request.getSession().setAttribute("foodId", food.getFoodId()); request.getSession().setAttribute("foodName", food.getFoodName()); request.getSession().setAttribute("taste", food.getTaste()); request.getSession().setAttribute("price", food.getPrice()); request.getSession().setAttribute("description", food.getDescribe()); response.sendRedirect(((HttpServletRequest) request).getContextPath()+"/showFoodList.jsp"); }catch (Exception e) { e.printStackTrace(); } } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String basePath=request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+request.getContextPath()+"/"; %> <!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> <% String foodId = ""; String foodName = ""; String price = ""; String taste = ""; String description = ""; if(session.getAttribute("foodId")!=null){ foodId = (String)session.getAttribute("foodId"); } if(session.getAttribute("foodName")!=null){ foodName = (String)session.getAttribute("foodName"); } if(session.getAttribute("taste")!=null){ taste = (String)session.getAttribute("taste"); } if(session.getAttribute("price")!=null){ price = (String)session.getAttribute("price"); } if(session.getAttribute("description")!=null){ description = (String)session.getAttribute("description"); } %> <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> <tr> <td value="<%=foodId %>"></td> <td value="<%=foodName %>"></td> <td value="<%=taste %>"></td> <td value=""></td> <td value="<%=price %>"></td> <td value="<%=description %>"></td> </tr> </tbody> </table> </center> </body> </html>
2
收起
正在回答
3回答
你好!运行你的代码出现如下错误
检查发现,在添加商品的jsp页面中,使用的提交方式是post,而Servlet中只有doGet方法,这样就不一致了。因为method为post时,会提交给doPost()方法执行。可以将Servlet中的doGet方法改成doPost
改后运行,发生了类型转换异常
是Servlet中如下选中的语句引起的
语句改成如下形式:
包导入的也不对,要导入org.apache.commons.fileupload下的类
在showFoodList.jsp页面中,td标签没有value属性,需要按如下形式完成。
另外,可以不用每个属性都存放在一个session中,可以将Goods对象存储在session中,在jsp页面使用el表达式进行表示,同学可以再看看之前关于jstl显示内容的视频。
如果同学还有问题,建议将控制台报错贴出来,便于老师帮你解决问题。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星