servlet解析报错 排除路径错误
先贴上jsp代码
<form action="<%=basePath %>/FoodAddServlet" method="post" enctype="multipart/form-data">
再贴上servlet java的代码
public class FoodAddServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // 用于完成数据的接受,使用map数据结构来保存key-value HashMap<String, String> map= new HashMap<>(); //文件上传的基本操作 1.需要创作磁盘文件项工厂对象 DiskFileItemFactory diskfileitemfactory= new DiskFileItemFactory(); //2.创建一个核心解析类 ServletFileUpload servletfileupload = new ServletFileUpload (diskfileitemfactory); //3解析request请求,返回值是list集合,存放file item, 代表页面中的每一个解析项 List<FileItem> list=servletfileupload.parseRequest(request); //4. 遍历集合 获得file item, 判断其为表单项还是文件上传项 for (FileItem fileitem:list) { if(fileitem.isFormField()) { //普通表单项,文本框,接受表单参数 String Name=fileitem.getFieldName();//获得表单项的name属性 String Value=fileitem.getString("UTF-8");//获得表单项的值 System.out.println(Name+" "+Value); }else { } } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
我觉得路径应该没有问题,但是测试print一直没有输出
再贴上web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>reg_login1</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> <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>com.imooc.servlet.FoodAddServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FoodAddServlet</servlet-name> <url-pattern>/FoodAddServlet</url-pattern> </servlet-mapping> </web-app>
现在贴上错误信息
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Error instantiating servlet class [com.imooc.servlet.FoodAddServlet]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Error instantiating servlet class [com.imooc.servlet.FoodAddServlet]
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:412)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1385)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:844)
Root Cause
java.lang.ClassNotFoundException: com.imooc.servlet.FoodAddServlet
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1292)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1121)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:412)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1385)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:844)
Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/9.0.8
正在回答
建议同学参考我的配置图进行一下配置:source的输出路径,就是系统默认的:
发布目录选择的Tomcat:
编译以后,在Tomcat的发布目录下,是有FoodAddServlet.class文件的;
建议同学将Source的编译输出路径改一下,然后再重新编译一下;
重新编译:Project——clean——Clean projects selected below
选中要编译的项目,点击OK,即可重新编译。
祝学习愉快!
图片是本地的发布项目的路径 位于tomcat 文件夹之下
建议同学打开项目的发布目录,建议一下相应路径下有没有FoodAddServlet的class文件;
项目的发布路径可以查看这个问答:http://class.imooc.com/course/qadetail/36078
如果没有的话,点击项目之后,打开project->Build Automatically,打钩,设置为自动编译;或者同学选择clean,重新编译下项目;
如果还有问题,可以在问答区提问,我们的助教会帮助解决问题。祝学习愉快!
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星