我编写了一个servlet,当我尝试运行代码时,它提出了以下消息: "服务器localhost上的服务器Tomcat7.0无法启动。"我已经尝试了所有可能的技巧,但它不起作用。我已按照之前给出的其他类似链接中的说明清理了服务器。之前已成功执行JSP文件。我作为servlet文件运行的代码是:
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import business.User;
import data.UserIO;
@WebServlet("/AddToEmailListServlet")
public class AddToEmailListServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String firstname=request.getParameter("firstname");
String lastname=request.getParameter("lastname");
String emailaddress=request.getParameter("emailaddress");
String getmail=request.getParameter("addEmail");
String country=request.getParameter("country");
ServletContext sc=this.getServletContext();
String path=sc.getRealPath("/WEB-INF/EmailList.txt");
User user=new User(lastname,firstname,emailaddress);
UserIO.add(user, path);
response.setContentType("text/html;charset-UTF-8");
PrintWriter out=response.getWriter();
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
+"<html>\n"
+"<head>\n"
+"<title>Insert title here</title>\n"
+"</head>\n"
+"<body>\n"
+"<h1>Thanks for joining our email list.</h1>\n"
+"<p>Here is the information you entered.</p>\n"
+"<table cellspacing=\"5\" cellpadding=\"5\" border=\"1\">\n"
+"<tr>\n"
+"<td align=\"right\">First name:</td>\n"
+"<td>"+firstname+"</td>\n"
+"</tr>\n"
+"<tr>\n"
+"<td align=\"right\">Last name:</td>\n"
+"<td>"+lastname+"</td>\n"
+"</tr>\n"
+"<tr>\n"
+"<td align=\"right\">Email address:</td>\n"
+"<td>"+emailaddress+"</td>\n"
+"</tr>\n"
+"</table>\n"
+"<p>Toenter another email address click on the Back <br>\n"
+"button browser or the Return button shown <br> below</p>\n"
+"Your country is"+country+"<br>\n"
+"<form action=\"JoinEmailList.html\" method=\"post\">\n"
+ "<input type=\"submit\" value=\"Return\">\n"
+"</form>\n"
+"</body>\n"
+"</html>\n");
out.close();
}
}
必要的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">
<!-- the definition of the servlet -->
<servlet>
<servlet-name>AddToEmailListServlet</servlet-name>
<servlet-class>AddToEmailListServlet</servlet-class>
</servlet>
<!-- the mapping for the servlets -->
<servlet-mapping>
<servlet-name>AddToEmailListServlet</servlet-name>
<url-pattern>/addToEmailList</url-pattern>
</servlet-mapping>
<!-- other settings -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>JoinEmailList2.html</welcome-file>
</welcome-file-list>
</web-app>
我还需要做些什么?
控制台上的堆栈跟踪是:
Aug 28, 2014 11:26:43 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Aug 28, 2014 11:26:43 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Test' did not find a matching property.
Aug 28, 2014 11:26:44 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 28, 2014 11:26:44 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 28, 2014 11:26:44 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 798 ms
Aug 28, 2014 11:26:44 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 28, 2014 11:26:44 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.55
Aug 28, 2014 11:26:44 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 28, 2014 11:26:44 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 28, 2014 11:26:44 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 649 ms
Aug 28, 2014 11:28:03 AM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet AddToEmailListServlet as unavailable
Aug 28, 2014 11:28:03 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet AddToEmailListServlet
java.lang.ClassNotFoundException: email.AddToEmailListServlet
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:530)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:512)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:2)
标签中的名称不相同。它们应该相同,比如
<servlet>
<servlet-name>AddToEmailListServlet</servlet-name>
<servlet-class>email.AddToEmailListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddToEmailListServlet</servlet-name>
<url-pattern>/addToEmailList</url-pattern>
</servlet-mapping>