ServletContext.getAttributes(“Name”)返回null

时间:2011-07-28 09:43:53

标签: java servlets

我编写了servlet。我想测试我的servlet的init()方法的功能。但是当我用servletRunnermIt为我的servlet编写测试用例时,给出了以下异常。我还得到了配置和上下文对象。如果我想从这两个对象获取init-parameters,则返回null。

com.meterware.httpunit.HttpException: Error on HTTP request: 405 HTTP method GET is not supported by this URL [http://127.0.0.1:8888/NewsletterServlet]
    at com.meterware.httpunit.WebClient.validateHeaders(WebClient.java:623)
    at com.meterware.httpunit.WebClient.updateClient(WebClient.java:500)
    at com.meterware.httpunit.WebWindow.updateWindow(WebWindow.java:192)
    at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:183)
    at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:158)
    at com.meterware.httpunit.WebClient.getResponse(WebClient.java:122)
    at com.newscenter.server.NewsletterServletTest.testFetchNews(NewsletterServletTest.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
    at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2 个答案:

答案 0 :(得分:0)

网址看起来很可疑,http://127.0.0.1:8888/NewsletterServlet

通常,servlet URL应包含Web应用程序名称,例如http://127.0.0.1:8888/ WebAppName / NewsletterServlet。但我不认为你的情况。这可能是问题所在。

答案 1 :(得分:0)

错误消息表明您已经

super.doGet(request, response);

在servlet的doGet()实现中。去掉它。

HttpServlet类基于模板方法模式,其中所有非重写doXxx()方法都发送HTTP 405错误,指示不支持该方法。当您覆盖它时,它不会发送HTTP 405错误,而只会发送由特定overriden方法生成的任何结果。但是,无论如何,当你调用super方法时,你仍会得到405。