老师我正在做添加文件功能

老师我正在做添加文件功能

老师我正在做添加文件功能 可是添加菜品页面点击了添加按钮之后出现了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;

}


}

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

正在回答

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

2回答

你好同学,你的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>


  • 慕沐3512059 提问者 #1
    老师我什么情况下要配置web文件呢 要是选择了2.5版本是不是就不用配置了啊
    2018-11-26 15:14:34
  • chrismorgen 回复 提问者 慕沐3512059 #2
    不是的,只要是使用servlet就需要配置servlet的请求路径,后期也会用到@WebServlet注解,使用起来会更方便一些哦,祝学习愉快~
    2018-11-26 17:11:35
  • 慕沐3512059 提问者 回复 chrismorgen #3
    好的老师 谢谢您啦
    2018-11-26 17:22:11
chrismorgen 2018-11-26 11:01:41

页面404的是找不到路径的错误,下图所标记的Message是无法被访问的路径,也就是/foodmng/FoodAddServlet,解决方案:建议你检查一下web.xml中是否配置了FoodAddServlet的访问路径,如果问题没有解决,建议你粘贴一下web.xml中的代码,祝学习愉快~

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

  • 提问者 慕沐3512059 #1
    老师我不会在web。xml里面配置地址 是不是在创建工程的时候配置2.5版本就可以彻底解决这个问题呢 web。xml <?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>com.imooc.servlet.InitServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> </web-app>
    2018-11-26 14:24:34
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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