我的servlet类没有从ECLIPSE IDE中的tomcat服务器实例化

时间:2015-05-01 15:32:53

标签: java eclipse tomcat servlets

exception 

javax.servlet.ServletException: Error instantiating servlet class BeerSelect
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)


root cause 

java.lang.ClassNotFoundException: BeerSelect
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.12 logs.

的index.jsp

     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN"            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Insert title here</title>
   </head>
   <body>
   <h1 align="center">Beer Selection Page</h1>
   <form method="post"
       action=”SelectBeer.do”>
        Select beer characteristics<p>
    Color:
    <select name=”color” size=”1”>
    <option value=”light”> light </option>
    <option value=”amber”> amber </option>
     <option value=”brown”> brown </option>
      <option value=”dark”> dark </option>
      </select>
    <br><br>
    <center>
    <input type="submit">
     </center>
     </form>
     </body>
     </html>

BeerSelect.java

     import javax.servlet.*;
       import javax.servlet.http.*;
        import java.io.*;

        public class BeerSelect extends HttpServlet 
        {
          public void doPost(HttpServletRequest request,
          HttpServletResponse response)
            throws IOException, ServletException {
           response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("Beer Selection Advice<br>");
            String c = request.getParameter("color");
            out.println("<br>Got beer color " + c);
         }
          }

的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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>headfirstdemo</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>
    <description></description>
    <display-name>BeerSelect</display-name>
    <servlet-name>BeerSelect</servlet-name>
    <servlet-class>BeerSelect</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BeerSelect</servlet-name>
    <url-pattern>/selectBeer.do</url-pattern>
  </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:0)

我相信你已经在java包中指定了你的servlet。如果是这样,您还必须在servlet映射中指定该包路径。

<servlet>
    <description></description>
    <display-name>BeerSelect</display-name>
    <servlet-name>BeerSelect</servlet-name>
    <servlet-class>[specify your package].BeerSelect</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BeerSelect</servlet-name>
    <url-pattern>/selectBeer.do</url-pattern>
  </servlet-mapping>

代表:org.headfirst.example.BeerSelect