Menu

Session tracking never works

Help
2006-08-02
2013-04-26
  • Olaf Klischat

    Olaf Klischat - 2006-08-02

    I try to run web applications under httpunit using ServletRunner / ServletUnitClient. Session tracking never works, no matter what I try. If somebody could tell me what I'm doing wrong, that would be very much appreciated. Below is the complete source of a simple program that reproduces the problem. First the servlet, then the standalone application that runs the servlet, the the output of the application. When I run the same servlet in Tomcat (with firefox on the client side), everything works as it should.

    =====the servlet=====

    package httpunittest;

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

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

    public class TheServlet extends HttpServlet {

        protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
            doGetPost(req,res);
        }

        protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
            doGetPost(req,res);
        }
       
        protected void doGetPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
           
            res.setContentType("text/html");
            PrintWriter out = res.getWriter();
            out.println("<html><head><title>httpunittest</title></head>");
            out.println("<body>");
            out.println("<h1>Hello World.</h1></body>");
            Integer counter = (Integer)req.getSession(true).getAttribute("counter");
            if (null == counter) {
                counter = new Integer(0);
            } else {
                counter = new Integer(counter.intValue() + 1);
            }
            req.getSession(true).setAttribute("counter", counter);
          
            out.println("current counter value: "+counter);
            out.println("</body>");
        }
       
    }

    =====the application=====
    package httpunittest;

    import com.meterware.httpunit.PostMethodWebRequest;
    import com.meterware.httpunit.WebRequest;
    import com.meterware.httpunit.WebResponse;
    import com.meterware.servletunit.ServletRunner;
    import com.meterware.servletunit.ServletUnitClient;

    public class Servlettest {

        public static void main(String[] args) throws Exception {
            ServletRunner servletRunner = new ServletRunner();
            servletRunner.registerServlet("/*", TheServlet.class.getName());
            ServletUnitClient servletUnitClient = servletRunner.newClient();
            servletUnitClient.getClientProperties().setAutoRedirect(false);
           
            WebRequest request   = new PostMethodWebRequest( "http://foo.com/lala" );
            WebResponse response = servletUnitClient.getResource(request);
            System.out.println(response.getText());
           
            System.out.println("======");
           
            request   = new PostMethodWebRequest( "http://foo.com/lala" );
            response = servletUnitClient.getResource(request);
            System.out.println(response.getText());
        }

    }

    =====output of the application=====

    Rhino classes (js.jar) not found - Javascript disabled
    <html><head><title>httpunittest</title></head>
    <body>
    <h1>Hello World.</h1></body>
    current counter value: 0
    </body>

    ======
    <html><head><title>httpunittest</title></head>
    <body>
    <h1>Hello World.</h1></body>
    current counter value: 0
    </body>

    Obviously, the second response should contain 1 for the current counter value, not 0.

     
    • Olaf Klischat

      Olaf Klischat - 2006-08-03

      Update: I've now inserted an link into the servlet's output page which points back to the servlet itself. When I click() that link programmatically, I get a NullpointerException inside Httpunit.

      The changes:

      - added a single line to TheServlet.doGetPost(), which prints the link:

          out.println("<br/>\n<a href=\"lilili\">reload</a>");

      - changed the code in main as follows:

          public static void main(String[] args) throws Exception {
              ServletRunner servletRunner = new ServletRunner();
              servletRunner.registerServlet("/*", TheServlet.class.getName());
              ServletUnitClient servletUnitClient = servletRunner.newClient();
              servletUnitClient.getClientProperties().setAutoRedirect(false);
             
              WebRequest request   = new PostMethodWebRequest( "http://foo.com/lala" );
              WebResponse response = servletUnitClient.getResource(request);
              System.out.println(response.getText());
             
              System.out.println("======");

              response = response.getLinkWith("reload").click(); //<= line 38

              System.out.println(response.getText());
          }

      The exception:

      Exception in thread "main" java.lang.NullPointerException
          at com.meterware.httpunit.WebRequestSource.submitRequest(WebRequestSource.java:255)
          at com.meterware.httpunit.WebRequestSource.submitRequest(WebRequestSource.java:232)
          at com.meterware.httpunit.WebLink.click(WebLink.java:98)
          at httpunittest.Servlettest.main(Servlettest.java:38)

      Obviously the "_window" of the link is null, which triggers the error.

       
      • Olaf Klischat

        Olaf Klischat - 2006-08-03

        Update 2: The NullpointerException disappears when I apply this patch to WebWindow.java:

        --- /usr/local/src/httpunit-1.6.2/src/com/meterware/httpunit/WebWindow.java    Sun Mar 26 22:16:46 2006
        +++ src/com/meterware/httpunit/WebWindow.java   Thu Aug  3 21:13:07 2006
        @@ -170,6 +170,7 @@
                 }

                 if (response != null) _client.tellListeners( response );
        +        response.setWindow(this);
                 return response;
             }

        This session won't be held though UNLESS I call getResponse() instead of getResource() on the  servletUnitClient. I didn't realize there's a difference between those two.

        JFTR, here's my new main(), which doesn't throw Exception and does hold the session:

                ServletRunner servletRunner = new ServletRunner();
                servletRunner.registerServlet("/*", TheServlet.class.getName());
                ServletUnitClient servletUnitClient = servletRunner.newClient();
                servletUnitClient.getClientProperties().setAutoRedirect(false);
               
                WebResponse response = servletUnitClient.getResponse("http://foo.com/lala");
                System.out.println(response.getText());
               
                System.out.println("======");

                response = response.getLinkWith("reload").click();
                System.out.println(response.getText());

                System.out.println("======");
               
                response = servletUnitClient.getResponse("http://foo.com/lilili");
                System.out.println(response.getText());
               
                System.out.println("======");

                response = servletUnitClient.getResponse("http://foo.com/lilili");
                System.out.println(response.getText());

        output:

        <html><head><title>httpunittest</title></head>
        <body>
        <h1>Hello World.</h1>
        current counter value: 0
        <br/>
        <a href="lilili">reload</a>
        </body>

        ======
        <html><head><title>httpunittest</title></head>
        <body>
        <h1>Hello World.</h1>
        current counter value: 1
        <br/>
        <a href="lilili">reload</a>
        </body>

        ======
        <html><head><title>httpunittest</title></head>
        <body>
        <h1>Hello World.</h1>
        current counter value: 2
        <br/>
        <a href="lilili">reload</a>
        </body>

        ======
        <html><head><title>httpunittest</title></head>
        <body>
        <h1>Hello World.</h1>
        current counter value: 3
        <br/>
        <a href="lilili">reload</a>
        </body>

         

Log in to post a comment.

MongoDB Logo MongoDB