老师我正在做添加文件功能
老师我正在做添加文件功能 可是添加菜品页面点击了添加按钮之后出现了404错误 文件工厂的upload功能也没有再工作台体现 是addFood。jsp的问题吗 foodaddservlet。java package 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 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; /** * Servlet implementation class addServlet */ public class FoodAddServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory(); ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory); List<FileItem> list=servletFileUpload.parseRequest(request); for (FileItem fileItem : list) { if(fileItem.isFormField()) { String name=fileItem.getFieldName(); String value=fileItem.getString("UTF-8"); System.out.println(name+" "+value); }else { } } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
initservlet。java
package servlet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import com.imooc.domain.Food;
/**
* Servlet implementation class InitServlet
*/
public class InitServlet extends HttpServlet {
public void init() throws ServletException {
List<Food> list=new ArrayList<Food>();
this.getServletContext().setAttribute("list", list);
}
}
food。java
package com.imooc.domain;
public class Food {
public String id;
public String foodName;
public String taste;
public String foodImage;
public String price;
public String description;
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 getFoodImage() {
return foodImage;
}
public void setFoodImage(String foodImage) {
this.foodImage = foodImage;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
正在回答
你好同学,你的xml配置写的不对,servlet-class是你配置servlet所在的路径,并且需要配置servlet-mapping,来添加映射路径的地址,请参考如下代码进行修改,祝学习愉快~
<?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_4_0.xsd" version="4.0"> <display-name>foodmng</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>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> <servlet> <description></description> <display-name>FoodAddServlet</display-name> <servlet-name>FoodAddServlet</servlet-name> <servlet-class>servlet.FoodAddServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>FoodAddServlet</servlet-name> <url-pattern>/FoodAddServlet</url-pattern> </servlet-mapping> </web-app>
页面404的是找不到路径的错误,下图所标记的Message是无法被访问的路径,也就是/foodmng/FoodAddServlet,解决方案:建议你检查一下web.xml中是否配置了FoodAddServlet的访问路径,如果问题没有解决,建议你粘贴一下web.xml中的代码,祝学习愉快~
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星