Chris Cooper
Chris Cooper

Reputation: 899

Servlet request and validation

I'd like to implement the following structure:

Servlet (GET) (put collection of X on request)
  |
JSP (output list of X) <--
  |                      |
Servlet (POST) ----------- Validation error!
  |
Validated OK, continue                                                                                                             

I've implemented this using the pattern described in How to avoid Java code in JSP files? but I want to know if there's a simple way of avoiding having to reload my collection of X during the validation stage since it's no longer on the request object. I am putting some validation messages on the request scope in the POST stage so I need to be able to access these.

I'm trying to avoid a framework at this stage since the scale of the project doesn't seem to justify it.

Upvotes: 0

Views: 1305

Answers (1)

Ken Chan
Ken Chan

Reputation: 90457

Yes . The simplest way is to put the collection of X into the session .

Given a HttpServletRequest , you can get its associated HttpSession by getSession() . Then set the collection of X into the HttpSession by setAttribute() , that is:

httpRequest.getSession().setAttribute("xxxxxxx" , collectionOfX)

Then , in the Servlet (POST) , you can get the collection of X from the HttpSession by

 httpRequest.getSession().getAttribute("xxxxxxx");

Upvotes: 4

Related Questions