Melodie Gauthier
Melodie Gauthier

Reputation: 715

Share object between servlets

I am doing my first web application and have a question about request/sessions doGet/doPost. At first, in a servlet called 'userConnection', user has to connect by entering username/password. Then I am creating a bean 'user' with other variables (username,password,etc..). I need this bean in the logic of the next servlet, MAKE_OFFER

In the doPost() method of userConnection I am doing

<..creating user bean object..>
request.setAttribute("user", user); 
this.getServletContext().getRequestDispatcher( MAKE_OFFER ).forward(request, response);

MAKE_OFFER is the URL to another servlet. In this page, user has to fill a form to make an offer, and I am using the user bean information for some validation. But since getRequestDispatcher is calling the doPost() method, user didn't get the chance to fill the form yet so I'm getting errors about the form being empty. Should the user attribute be a session attribute instead of a request attribute? Is there a way I can bring the 'user' object to the MAKE_OFFER servlet, from the userConnection servlet, with or without calling doPost?

Thanks

Upvotes: 0

Views: 98

Answers (1)

Steve C
Steve C

Reputation: 19445

I think you have a couple of issues here:

  1. As you suggested yourself, you need a mechanism for preserving client state between requests. Using a session attribute is the most common and easiest way to do this;
  2. It looks like you are forwarding processing directly to your MAKE_OFFER servlet. Instead, you need to forward to the page (presumably a JSP) that contains your offer form. The form submit should subsequently POST to your MAKE_OFFER servlet.

Upvotes: 1

Related Questions