老师帮看看,跳转页面时404报错

老师帮看看,跳转页面时404报错

package com.leo.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.DiskFileItem;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.apache.jasper.tagplugins.jstl.core.ForEach;


/**

 * 添加菜品的servlet

 */

public class FoodAddServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//数据接收,文件上传

try {

//1.创建一个磁盘文件项工厂

DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();

//2.创建核心解析类

ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);

//3.解析request请求,返回list集合,集合中存放的是FileItem对象

List<FileItem> list = servletFileUpload.parseRequest(request);

//4.遍历集合,获得每个FileItem

for(FileItem fileItem : list){

//判断是否是文件上传项

if(fileItem.isFormField()) {

//普通表单项

//接收表单项参数的值

//获得表单项name属性的值

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);

}


}

--------------------------------------------------------------------------

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<base href="/FoodManagementSystem">

<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="/FoodManagementSystem/FoodAddServlet" method="post" enctype="multipart/form-data">

<table border="1px" width="400px" cellspacing="0px" cellpadding="0px">

<tr>

<td>菜品&nbsp;ID</td>

<td><input type="text" name="id"></td>

</tr>

<tr>

<td>菜&nbsp;&nbsp;名</td>

<td><input type="text" name="foodName"></td>

</tr>

<tr>

<td>口&nbsp;&nbsp;味</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>价&nbsp;&nbsp;格</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>

------------------------------------------------------------------------------------------------

<?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_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>FoodManagementSystem</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.leo.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>

</web-app>

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

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

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


正在回答

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

2回答
<servlet> 
    <servlet-name>FoodAddServlet</servlet-name>
     <servlet-class>com.leo.servlet.InitServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>FoodAddServlet</servlet-name> 
    <url-pattern>/FoodManagementSystem/FoodAddServlet</url-pattern> 
</servlet-mapping>

上边的代码中有两处错误,

1、<servlet-class></servlet-class>中要写的是FoodAddServlet的具体地址,而不是InitServlet的

2、同学的访问路径<url-pattern></url-pattern> 中要改成

<url-pattern>/FoodAddServlet</url-pattern>

否则,访问的时候,就变成以下的访问路径了。这里的访问路径不包括项目名http://localhost:8080/FoodManagementSystem/FoodManagementSystem/FoodAddServlet


如果我的回答解决了你的疑惑,请采纳!祝学习愉快!


  • leoleo524 提问者 #1
    可以了,谢谢老师解答
    2018-11-06 10:40:22
chrismorgen 2018-11-06 09:48:11

你好同学,建议你在web.xml中注册FoodAddServlet的配置信息,否则就会出现路径404的错误,祝学习愉快~

  • 提问者 leoleo524 #1
    增加了下面的配置信息,但是还是404的错误 <servlet> <servlet-name>FoodAddServlet</servlet-name> <servlet-class>com.leo.servlet.InitServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FoodAddServlet</servlet-name> <url-pattern>/FoodManagementSystem/FoodAddServlet</url-pattern> </servlet-mapping>
    2018-11-06 10:12:27
  • 提问者 leoleo524 #2
    可以了,谢谢老师解答
    2018-11-06 10:40:44
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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