【问题标题】:Servlet class not found exception未找到 Servlet 类异常
【发布时间】:2013-04-09 18:17:24
【问题描述】:

我正在尝试编写一个非常简单的 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>

这是eclipse设置:

我查看了其他帖子,您还应该在 WEB-INF 文件夹中包含 java 文件。但我不断收到 classNotFoundException。

编辑:堆栈跟踪表明找不到uploadServlet,所以我很困惑。

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

【问题讨论】:

  • 如果你是这个意思,不需要在WEB-INF文件夹中包含java源文件。
  • 这个问题一般是servlet没有获取到class文件的时候出现的,再试试写上uploadServlet的全路径。
  • ClassNotFoundException 会告诉你它没有找到什么类。了解如何阅读和理解异常跟踪。
  • 你想在服务器上打什么?
  • servlet 代码中的response.sendRedirect("location of the result page"); 是什么?

标签: java html eclipse servlets


【解决方案1】:

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

【讨论】:

  • 感谢您的帮助,我尝试添加“servletClass”。之前,但我总是得到“请求的源,(X/Location%Of%The%File%Is%Not%Found)”。
  • 你现在有什么异常吗?如果是这种情况,请在此处粘贴 stackTrace。
  • 不,我没有收到任何异常,但控制台中会出现警告。警告:[SetPropertiesRule]{Server/Service/Engine/Host/Context} 将属性 'source' 设置为 'org.eclipse.jst.jee.server:MyTubeServer' 没有找到匹配的属性。
【解决方案2】:

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

所以,应该是这样的

<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 页面。

【讨论】:

  • 感谢您的帮助! ContextPath 只是返回到我的 index.jsp,我想这就是它的作用?当我添加 servletClass.uploadServlet 时,它只显示请求的源 (X/Location%Of%The%File%Is%Not%Found)。
  • @user1157751:您可以将错误屏幕上传到问题吗?随着您作为 EDIT 的新努力
  • 我添加了一个带有新错误的新屏幕截图,现在是 HTTP 404 错误。
  • 我觉得不需要添加上下文路径,
    就足够了!
  • 所有回复都是正确的,是我的愚蠢导致了错误。谢谢大家的帮助!!
猜你喜欢
  • 2012-10-12
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 2012-07-24
相关资源
最近更新 更多