HTTP状态404-JSP和Servlet问题

时间:2018-07-17 07:40:09

标签: java jsp tomcat web

提交上传请求时,我收到HTTP状态404-/ Newtry / fileuploadhandler。 **

  • index.jsp

**

   <%@page contentType="text/html" pageEncoding="UTF-8"%>
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
  <html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>File Upload Example in JSP and Servlet - Java web application</title>
</head>

<body> 
    <div>
        <h3> Choose File to Upload in Server </h3>
        <form action="fileuploadhandler" method="post" enctype="multipart/form-data">
            <input type="file" name="file" />
            <input type="submit" />
        </form>          
    </div>

</body>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
    <servlet-name>fileuploadhandler</servlet-name>
    <servlet-class>com.FileUploadHandler</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>fileuploadhandler</servlet-name>
    <url-pattern>/fileuploadhandler</url-pattern>
</servlet-mapping>


<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

FileUploadHandler.java

package com;

import java.io.File;
import java.io.IOException;
import java.util.List;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadHandler extends HttpServlet {
private final String UPLOAD_DIRECTORY = "C:/uploads";

@Override
 protected void doPost(HttpServletRequest request, HttpServletResponse 
 response)
        throws ServletException, IOException {

    //process only if its multipart content
    if(ServletFileUpload.isMultipartContent(request)){
        try {
            List<FileItem> multiparts = new ServletFileUpload(
                                     new DiskFileItemFactory()).parseRequest(request);

            for(FileItem item : multiparts){
                if(!item.isFormField()){
                    String name = new File(item.getName()).getName();
                    item.write( new File(UPLOAD_DIRECTORY + File.separator + 
  name));
                }
            }

           //File uploaded successfully
           request.setAttribute("message", "File Uploaded Successfully");
        } catch (Exception ex) {
           request.setAttribute("message", "File Upload Failed due to " + 
   ex);
        }          

    }else{
        request.setAttribute("message",
                             "Sorry this Servlet only handles file upload 
  request");
    }

    request.getRequestDispatcher("/result.jsp").forward(request, response);

   }

  }

点击上传时,我收到以下404错误消息。 enter image description here enter image description here

,我可以使用http://localhost:8080/查看tomcat主页 没有日志信息。我不知道它是tomcat还是代码问题

0 个答案:

没有答案