javatomcatservletscookieseclipse-europa

Error instantiating servlet class in Eclipse Europa?


First.java : this is my first java class

package com;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

 class First extends HttpServlet {

     public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException 
     {  
        // res.setContentType("html/text");
         String name= req.getParameter("fname");
         Cookie ck= new Cookie("uname",name);
         res.addCookie(ck);

         RequestDispatcher r= req.getRequestDispatcher("Servlet2");
         r.forward(req,res);


     }
 }

SecondServlet.java: this is second java class

package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SecondServlet extends HttpServlet{

    public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException{

        resp.setContentType("html/text");
        PrintWriter out= resp.getWriter();
        Cookie []ck=req.getCookies();
        out.print(ck[0]);
    }
}

web.xml: Deployment descriptor

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>
    cooooooooooooookie</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <servlet>
    <servlet-name>A</servlet-name>
    <servlet-class>com.First</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>A</servlet-name>
    <url-pattern>/cook</url-pattern>
    </servlet-mapping>
    <servlet>
    <servlet-name>B</servlet-name>
    <servlet-class>com.SecondServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>B</servlet-name>
    <url-pattern>/Servlet2</url-pattern>
    </servlet-mapping>
</web-app>

Error:

Jun 12, 2013 9:03:15 PM 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: C:\Program Files\Java\jre6\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Java\jre7\bin\client;C:\Program Files\Java\jre7\bin;C:\Program Files\Broadcom\Broadcom 802.11 Network Adapter\Driver;;C:\Ruby193\bin;;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Java\jdk1.6.0_14\bin;.
Jun 12, 2013 9:03:15 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:cooooooooooooookie' did not find a matching property.
Jun 12, 2013 9:03:15 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-2030
Jun 12, 2013 9:03:15 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 362 ms
Jun 12, 2013 9:03:15 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Jun 12, 2013 9:03:15 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.26
Jun 12, 2013 9:03:15 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-2030
Jun 12, 2013 9:03:15 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jun 12, 2013 9:03:15 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/20  config=null
Jun 12, 2013 9:03:15 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 268 ms
Jun 12, 2013 9:03:22 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet A as unavailable
Jun 12, 2013 9:03:22 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet A
java.lang.IllegalAccessException: Class org.apache.catalina.core.StandardWrapper can not access a member of class com.First with modifiers ""
    at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1116)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:809)
    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:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)

I am getting error at First.java class. it says "Error instantiating servlet class com.First" i ahve added jars like this build path> configurebuildpath>libraries>add external jars. after this went to order and export tab >select all.

please help.


Solution

  • Shouldn't it be public class First extends HttpServlet ?

    We should declare servlet class as public so that server can instantiate it.

    At least the Container complains so :

    SEVERE: Allocate exception for servlet A

    java.lang.IllegalAccessException: Class org.apache.catalina.core.StandardWrapper can not access a member of class com.First with modifiers "" at sun.reflect.Reflection.ensureMemberAccess(Unknown Source) at java.lang.Class.newInstance0(Unknown Source) at java.lang.Class.newInstance(Unknown Source)