views:

25

answers:

3

I got a Servlet A which has a HTTPClient configured in it - declared a GetMethod which calls Servlet B (basically passing the url to the constructor of Servlet B".

In Servlet B I am setting some session vars and when the Servlet A gets back the control after the "execute" method but the session vars set in Servlet B are returning as null.

Servlet A

doPost(req,res)
      {
         HTTPClient client = new HTTPClient();
         GetMethod get =  new GetMethod("/ServletB.do");
         client.execute(get);

         System.out.println("Value of a is :: " + session.getAttribute("a")) ; //gives a NULL
}

Servlet B

doPost(req,res)
{
HTTPSession session = req.getSession();
session.setAttibute("a",a);
session.setAttibute("b",b);

}

Can you please let me know what my options are here to get around this issue?

A: 

I can see you are calling executing a 'get' from A while you have given code of handling 'post' in B.

Also session you are using to retrieve parameter using session.getAttribute("a")) is different than 'session' seen by the B servlet.

Gopi
+2  A: 

You have at least two issues with your code:

  • You need to pass your session id from servlet A in the HTTP request to servlet B. How the session id is passed between the client and server is container specific, but usually the session id is passed as an HTTP cookie.

  • The servlet specification does not guarantee that changes in the session attributes are immediately visible to all servlets, if several servlets are simultaneously accessing the same session and one of the servlets are adding or replacing a new attribute.

Your best option is probably to move the business logic in servlet B to a separate class and use that class from both servlet A or servlet B. Or do you have some other strange reason for making an HTTP call from one servlet to a different servlet within the same web application instead of simply making a method call?

jarnbjo
A: 

The right solution depends on where those servlets are actually running.

  • If those servlets are running at the same webserver and servletcontext, then just use RequestDispatcher#include(). They will have access to exactly the same session.

    request.getRequestDispatcher("/anotherservlet").include(request, response);
    
  • If those servlets are running at the same webserver, but a different servletcontext, then consider sharing the sessions so that they will share exactly the same session. It's unclear what servletcontainer you're using, so here's just a Tomcat-targeted example to give you some hints. Just set the crosscontext attribute of the both webapp contexs to true:

    <Context crossContext="true">
    
  • If those servlets are running at a different webserver, then you need to pass the data of interest as request parameters. That's your best bet, really. They won't and can't share the same session, that would be a security hole.

    new GetMethod("http://example.com/ServletB.do?name1=value1&amp;name2=value2");
    

    If the data is relatively large, then consider a shared database so that you only need to pass the PK value around as request parameter.

BalusC

related questions