qq_35979095
小鱼Ava
2017-02-07 06:45
采纳率: 60%
浏览 2.7k
已采纳

学习Servlet时运行报错,如何解决?

JSP页面错误
type Exception report

message Error instantiating servlet class com.rl.servlet.ServeltDemo1

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error instantiating servlet class com.rl.servlet.ServeltDemo1
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)

root cause

java.lang.IllegalAccessException: Class org.apache.catalina.core.DefaultInstanceManager can not access a member of class com.rl.servlet.ServeltDemo1 with modifiers ""
sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
java.lang.Class.newInstance(Unknown Source)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)

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

控制台报错
严重: Allocate exception for servlet helloServlet
java.lang.IllegalAccessException: Class org.apache.catalina.core.DefaultInstanceManager can not access a member of class com.rl.servlet.ServeltDemo1 with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:119)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:828)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

**实现类代码**:
class ServeltDemo1 implements Servlet {
@Override
public void init(ServletConfig arg0) throws ServletException {
    System.out.println("相应请求");
    System.out.println("Servlet组件被创建了");

}
@Override
public void service(ServletRequest request, ServletResponse response)
        throws ServletException, IOException {
    response.getOutputStream().write("<font color = 'red'>Hello Servlet</font>".getBytes());
}
@Override
public void destroy() {
    System.out.println("Servlet销毁了");
}

}

web.xml代码
<?xml version="1.0" encoding="UTF-8"?>


<!-- 设置Servlet名称 -->
helloServlet
<!-- 具体的Servlet类 -->
com.rl.servlet.ServeltDemo1


helloServlet
/hello

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • qq_35979095
    小鱼Ava 2017-06-22 03:28
    已采纳

    实现类添加public
    实现类代码
    public class ServeltDemo1 implements Servlet {
    @Override
    public void init(ServletConfig arg0) throws ServletException {
    System.out.println("相应请求");
    System.out.println("Servlet组件被创建了");

    }
    @Override
    public void service(ServletRequest request, ServletResponse response)
    throws ServletException, IOException {
    response.getOutputStream().write("Hello Servlet".getBytes());
    }
    @Override
    public void destroy() {
    System.out.println("Servlet销毁了");
    }

    点赞 评论
  • zq1314521
    stronger-a 2017-02-07 07:01

    应该是servlet的编写有问题,导致容器无法成功实例化,仔细检查一下代码,或者贴出代码让大家看看。

    点赞 评论
  • qq_35979095
    小鱼Ava 2017-02-07 07:21

    实现类代码
    package com.rl.servlet;

    import java.io.IOException;

    import javax.servlet.Servlet;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;

    class ServeltDemo1 implements Servlet {

    /**
     * 执行时机:当Servlet实例被创建的时候被调用,做初始化工作
     * ServletConfig:Servlet的配置对象,初始化的时候可以进行配置
     */
    @Override
    public void init(ServletConfig arg0) throws ServletException {
        System.out.println("相应请求");
        System.out.println("Servlet组件被创建了");
    
    }
    
    /**
     * 执行时机:当一个请求来请求当前的Servlet的时候被调用
     * 处理当前的Servlet的业务逻辑并且把响应返回给浏览器
     */
    @Override
    public void service(ServletRequest request, ServletResponse response)
            throws ServletException, IOException {
        response.getOutputStream().write("<font color = 'red'>Hello Servlet</font>".getBytes());
    }
    
    /**
     * 执行时机:是Servlet的实例对象被销毁的时候
     * 做一些收尾或清理工作
     */
    @Override
    public void destroy() {
        System.out.println("Servlet销毁了");
    }
    
    /**
     * 获得ServletConfig的配置对象
     */
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
    
    /**
     * 获得当前Servlet的一些属性信息(了解)
     */
    @Override
    public String getServletInfo() {
        return null;
    }
    

    }

    web.xml代码:
    <?xml version="1.0" encoding="UTF-8"?>


    <!-- 设置Servlet名称 -->
    helloServlet
    <!-- 具体的Servlet类 -->
    com.rl.servlet.ServeltDemo1


    <!-- 指定要映射的Servlet的名字 -->
    helloServlet
    <!-- Servlet的具体映射路径 -->
    /hello

    点赞 评论

相关推荐