Servlet类未找到异常

时间:2013-04-17 05:39:03

标签: java html eclipse servlets

我正在尝试编写一个非常简单的servlet,而HTML表单的操作使用servlet。

这是我的HTML表单:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Upload Video</title>
</head>
<body>

<%
    Object userSession = session.getAttribute("email");

if(userSession == null)
{
    out.println("Welcome to the MyTube, you are not logged in. <br /><br >");
    out.println("Please click the following to login: <a href=\"login.jsp\">here</a>");
    out.println("Or Click this to register: <a href=\"register.jsp\">here</a>");
}
else
{
    out.println("<form action=\"servletClass\" method=\"post\" enctype=\"multipart/form-data\">");
    out.println("<input type=\"file\" name=\"file\" />");
    out.println("<input type=\"submit\" />");
    out.println("</form>");
}

%>

</body>
</html>

这是我的servlet类:

public class uploadServlet extends HttpServlet
{
    String awsAccessKey = "WHOOOPS";
    String awsSecretKey = "WHOOOPS/YWPkmKfe";
    AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey);

    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
    {
        boolean isMulti = ServletFileUpload.isMultipartContent(request);
        if (isMulti) 
        {
            ServletFileUpload upload = new ServletFileUpload();

            try 
            {
                FileItemIterator iter = upload.getItemIterator(request);
                while (iter.hasNext()) 
                {
                    FileItemStream item = iter.next();
                    InputStream inputStream = item.openStream();
                    if (item.isFormField()) 
                    {

                    } 
                    else
                    {
                        String fileName = item.getName();
                        if (fileName != null && fileName.length() > 0) 
                        {
                            S3Service s3Service = new RestS3Service(awsCredentials);

                            S3Object fileObject = new S3Object();
                            fileObject.setDataInputStream(inputStream);
                            fileObject.setContentLength(Integer.parseInt(request.getHeader("Content-Length")));
                            s3Service.putObject("vidvidbucket", fileObject);

                            //read stream of file uploaded

                            //store as a temporary file

                            //upload the file to s3
                        }
                    }
                }
            } 
            catch (Exception e) 
            {

            }
        }

        response.sendRedirect("location of the result page");
    }
}

这是我的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>MyTubeServer</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>
    <servlet-name>servletClass</servlet-name>
    <servlet-class>uploadServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>servletClass</servlet-name>
    <url-pattern>/servletClass</url-pattern>
  </servlet-mapping>

</web-app>

这是日食设置:

enter image description here

我查看了其他帖子,你还应该在你的WEB-INF文件夹中包含java文件。但我一直得到classNotFoundException。

编辑:stacktrace表示找不到uploadServlet,所以我很困惑。

编辑2:在将servlet-class的XML文件设置为servletClass.uploadServlet而不是servlet-class之后,现在我得到了所请求的源,(X / Location%Of%%File%%is%Not Found Found)没有任何例外

enter image description here

2 个答案:

答案 0 :(得分:3)

1)在你的web.xml中试试这个:

<servlet>
<servlet-name>servletClass</servlet-name>
<servlet-class>servletClass.uploadServlet</servlet-class>
</servlet>

重启服务器并检查。

2)第二个错误的原因是这一行:

 response.sendRedirect("location of the result page");   
在您的Servlet代码中

。请重定向到适当的可用资源。有关更多信息,请参阅 API

答案 1 :(得分:3)

您的web.xml不正确。该图显示了包层次结构

enter image description here

所以,应该是这样的

<servlet>
<servlet-name>servletClass</servlet-name>
<servlet-class>servletClass.uploadServlet</servlet-class>
</servlet>

重启Tomcat

其次,尝试在表单操作中提供上下文路径。

<%
String contextPath = request.getContextPath();
%>

out.println("<form action=\""+contextPath+"/servletClass\" method=\"post\" enctype=\"multipart/form-data\">");

编辑: 我认为现在servlet被调用但这是错误的

response.sendRedirect("location of the result page");

将其更改为某个有效的JSP页面。