我想將編譯的Servlet類部署到Apache Tomcat服務器8.0.30上。在Tomcat上部署Servlet 8.0.30
,但我得到以下異常:
javax.servlet.ServletException: Error instantiating servlet class HelloWorld
java.lang.ClassNotFoundException: HelloWorld
我的tomcat webapps/ROOT/
文件夾中不包含類文件夾,所以我創建了一個與複製HelloWorld.class
進去。我在web.xml
添加了以下幾行:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
誰能告訴我我做錯了什麼?
@wero:這是我的HelloWorld.java的內容:
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException
{
// Do required initialization
message = "Hello World";
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy()
{
// do nothing.
}
}
我把文件放到你提到的地方,然後我重新啓動了服務器,但仍然出現錯誤:請求的資源不可用。 – user1858851
@ user1858851然後顯示'HelloWorld.java' – wero
請在編輯中找到HelloWorld.java。 – user1858851