jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Lemieux (QC/EMC)" <David.Lemi...@ericsson.com>
Subject getServletContext() throws a NullPointerException
Date Tue, 11 Nov 2003 16:20:27 GMT
Hi,

If I call the servlet directly, everything work fine. But if I run it with cactus, it throws
a NullPointerException.
I've included the junit ant log, the test class and the class under test. There is nothing
in the server log.

The appserver is weblogic 8.1
Cactus version is 1.5rc1


/************************************************/
/************************************************/
       [junit] Testcase: testExample took 1.016 sec
       [junit] Testcase: testXXX took 0.094 sec
       [junit] Testcase: testShouldNotBeSuccess took 0.031 sec
       [junit] Testcase: testResultSessionBean took 0.219 sec
       [junit] Testcase: testResponseHelloServlet took 0.187 sec
       [junit] 	Caused an ERROR
       [junit] null
       [junit] java.lang.NullPointerException
       [junit] 	at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:205)
       [junit] 	at XXX.applications.sampleapplication.HelloServlet.processRequest(Unknown
Source)
       [junit] 	at XXX.applications.sampleapplication.TestHelloServlet.testResponseHelloServlet(Unknown
Source)
       [junit] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       [junit] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       [junit] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       [junit] 	at org.apache.cactus.ServletTestCase.runCactusTest(ServletTestCase.java:295)
       [junit] 	at org.apache.cactus.ServletTestCase.runBare(ServletTestCase.java:263)
       [junit] 	at org.apache.cactus.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:156)
       [junit] 	at org.apache.cactus.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:130)
       [junit] 	at org.apache.cactus.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:158)
       [junit] 	at org.apache.cactus.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java)
       [junit] 	at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:138)
       [junit] 	at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:158)
       [junit] 	at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java)
       [junit] 	at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(ServletTestRedirector.java:109)
       [junit] 	at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(ServletTestRedirector.java:158)
       [junit] 	at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java)
       [junit] 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
       [junit] 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
       [junit] 	at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1053)
       [junit] 	at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:387)
       [junit] 	at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:28)
       [junit] 	at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:27)
       [junit] 	at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6316)
       [junit] 	at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
       [junit] 	at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
       [junit] 	at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3622)
       [junit] 	at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2569)
       [junit] 	at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
       [junit] 	at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
       [junit] 
       [junit] Testcase: testResponseHelloServlet
       [junit] TEST XXX.AllTest FAILED

/******************************************************************/
/******************************************************************/

package XXX.applications.sampleapplication;

import java.io.IOException;

import javax.servlet.ServletException;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.WebResponse;

import XXX.test.genericframework.log.LoggerFactory;

public class TestHelloServlet extends ServletTestCase{

	...

	public void beginResponseHelloServlet(WebRequest myWebRequest){
		LoggerFactory.getInstance().logInfo("beginResponseHelloServlet","before addParameter");
		myWebRequest.addParameter("username","David");
	}

	public void endResponseHelloServlet(WebResponse theResponse){
		LoggerFactory.getInstance().logInfo("endResponseHelloServlet","in endXXX");
		assertTrue("Assert that the submitted name is the one on the page",theResponse.getText().indexOf("David")
!= -1);
	}

	
	public void testResponseHelloServlet(){
		LoggerFactory.getInstance().logInfo("testResponseHelloServlet","test");
		HelloServlet myHelloServlet = new HelloServlet();
		
		assertNotNull("servlet not null",myHelloServlet);
		assertNotNull("request not null",request);
		assertNotNull("response not null",response);

		try {
			myHelloServlet.init();
			myHelloServlet.processRequest(request,response);
		} catch (ServletException e) {
			// XXX Auto-generated catch block
			e.printStackTrace();
			fail(e.toString());
		} catch (IOException e) {
			// XXX Auto-generated catch block
			e.printStackTrace();
			fail(e.toString());
		}
	}
}
/************************************************************************/
/************************************************************************/

    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws
ServletException, IOException {

	  System.out.println("Debut");
      response.setContentType("text/html");
      response.setBufferSize(8192);
      PrintWriter out = response.getWriter();
      
      String message = null;

		... // Set message using session bean

		if(message == null){
			message = "";
		}
      // then write the data of the response
      out.println("<html>" +
                  "<head><title>Hello</title></head>");

		// then write the data of the response
      out.println("<body  bgcolor=\"#ffffff\">" +
         "<img src=\"/mml/images/mmllogo.gif\"  width=\"204\" height=\"100\">" + 
		"<h2>" + message + "</h2>" +
         "<form method=\"get\">" +
         "<input type=\"text\" name=\"username\" size=\"25\">" +
         "<p></p>" +
         "<input type=\"submit\" value=\"Submit\">" +
         "<input type=\"reset\" value=\"Reset\">" +
         "</form>");

      String username = request.getParameter("username");
      if ( username != null && username.length() > 0 ) {
/*	THE NEXT LINE THROWS THE EXCEPTION	*/
/*******/ RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/sampleapplication/helloresponse");
                                    
            if (dispatcher != null){
				dispatcher.include(request, response);
            }

      }
      out.println("</body></html>");
      out.close();
	  System.out.println("Fin");
    }
/**********************************************************************************/
/**********************************************************************************/


David Lemieux



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message