从上一个AddFood.jsp调转到这里就不好使了。这是AddFood.jsp:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><base href="<%=basePath%>"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>添加菜品</title></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> <% System.out.println("1"); %></body></html>这是FoodAddServlet:package com.imooc;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.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;public class FoodAddServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Map<String,String> map = new HashMap<String,String>(); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); List<FileItem> list = servletFileUpload.parseRequest(request); String url = null; for (FileItem fileItem : list) { if(fileItem.isFormField()){ String name = fileItem.getFieldName(); 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); List<Food> foodList = (List<Food>) this.getServletContext().getAttribute("list"); for(Food u :foodList){ if(u.getFoodName().equals(map.get("username"))){ request.setAttribute("msg", " "); request.getRequestDispatcher("/regist.jsp").forward(request, response); return ; } } Food f = new Food(); f.setFoodId(map.get("id")); f.setFoodName(map.get("foodName")); f.setFoodTaste(map.get("taste")); f.setFoodPath(url); f.setFoodPrice(map.get("price")); f.setFoodDescribe("description"); foodList.add(f); for (Food u : foodList) { System.out.println(u); } this.getServletContext().setAttribute("list", foodList); request.getSession().setAttribute("username", f.getFoodName()); response.sendRedirect(request.getContextPath()+"/showFoodList.jsp"); } catch (FileUploadException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}而且后台显示FoodAddServlet并没有运行,不知道怎么回事?
添加回答
举报
0/150
提交
取消