0

First.java :这是我的第一个 java 类

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:这是第二个 java 类

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:部署描述符

<?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>

错误:

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)

我在 First.java 类中遇到错误。它说“错误实例化servlet类com.First”我已经添加了像这个构建路径> configurebuildpath>libraries>add external jars这样的jar。在此之后转到订购和导出选项卡>全选。

请帮忙。

4

1 回答 1

0

不应该public class First extends HttpServlet吗?

我们应该声明 servlet 类,public以便服务器可以实例化它。

至少容器抱怨这样:

严重:为 servlet A 分配异常

java.lang.IllegalAccessException:类 org.apache.catalina.core.StandardWrapper 无法 访问 com.First 类的成员, 在 java.lang.Class.newInstance0的 sun.reflect.Reflection.ensureMemberAccess(Unknown Source) 处使用修饰符“”(未知来源)在 java.lang.Class.newInstance(未知来源)

于 2013-06-12T15:58:35.410 回答