【发布时间】: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