SEVERE:为servlet Catalog Page java.lang.InstantiationException分配异常

时间:2014-01-11 05:49:48

标签: session servlets tomcat6

我正在练习“使用购物车和会话跟踪的在线商店”或coreservlet教程。我在运行CatalogPage servlet时发现异常。堆栈跟踪如下:

SEVERE: Allocate exception for servlet CatalogPage
java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:30)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1149)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

CatalogPage.java是一个抽象的servlet类,它由两个类扩展,每个类分别有init()defination。代码如下:

public abstract class CatalogPage extends HttpServlet {
    private Item[] items;
    private String[] itemIDs;
    private String title;

    protected void setItems(String[] itemIDs) {
        this.itemIDs = itemIDs;
        items = new Item[itemIDs.length];
    }

    protected void setTitle(String title) {
        this.title = title;
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        if (items == null) {
            response.sendError(response.SC_NOT_FOUND, "Missing Items.");
            return;
        }
        PrintWriter out = response.getWriter();
        out.println(ServletUtilities.headWithTitle(title)
                + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">"
                + title + "</H1>");
        Item item;
        for (int i = 0; i < items.length; i++) {
            out.println("<HR>");
            item = items[i];
            // Show error message if subclass lists item ID
            // that’s not in the catalog.
            if (item == null) {
                out.println("<FONT COLOR=\"RED\">" + "Unknown item ID "
                        + itemIDs[i] + "</FONT>");
            } else {
                out.println();
                String formURL = "OrderPage";
                // Pass URLs that reference own site through encodeURL.
                formURL = response.encodeURL(formURL);
                out.println("<FORM ACTION=\"" + formURL + "\">\n"
                        + "<INPUT TYPE=\"HIDDEN\" NAME=\"itemID\" "
                        + "       VALUE=\"" + item.getItemID() + "\">\n"
                        + "<H2>" + item.getShortDescription() + " ($"
                        + item.getCost() + ")</H2>\n"
                        + item.getLongDescription() + "\n" + "<P>\n<CENTER>\n"
                        + "<INPUT TYPE=\"SUBMIT\" "
                        + "VALUE=\"Add to Shopping Cart\">\n"
                        + "</CENTER>\n<P>\n</FORM>");
            }
        }
        out.println("<HR>\n</BODY></HTML>");

    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
public class KidsBooksPage extends CatalogPage {
    public void init() {
        String[] ids = { "lewis001", "alexander001", "rowling001" };
        setItems(ids);
        setTitle("All-Time Best Children’s Fantasy Books");
    }
}
public class TechBooksPage extends CatalogPage {
    public void init() {
        String[] ids = { "hall001", "hall002" };
        setItems(ids);
        setTitle("All-Time Best Computer Books");
    }
}

web.xml中的sevlet映射如下:

<servlet>
      <servlet-name>CatalogPage</servlet-name>
      <servlet-class>com.servletcontroller.CatalogPage</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>CatalogPage</servlet-name>
      <url-pattern>/CatalogPage</url-pattern>
  </servlet-mapping> 
  <servlet>
      <servlet-name>KidsBooksPage</servlet-name>
      <servlet-class>com.servletcontroller.KidsBooksPage</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>KidsBooksPage</servlet-name>
      <url-pattern>/KidsBooksPage</url-pattern>
  </servlet-mapping>
  <servlet>
      <servlet-name>TechBooksPage</servlet-name>
      <servlet-class>com.servletcontroller.TechBooksPage</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>TechBooksPage</servlet-name>
      <url-pattern>/TechBooksPage</url-pattern>
  </servlet-mapping>

请帮我找出该异常的根本原因。

1 个答案:

答案 0 :(得分:0)

CatalogPage.java是一个抽象类,因此无法以这种方式实例化并显示。根据{{​​3}}的第281页,它不应该显示,因为“...每个显示页面的代码列出了页面标题和项目的标识符。 ..“。 ,CatalogPage.java没有。因此,唯一的显示页面是KidsBooksPage.java和TechBooksPage.java。