0

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.

Community
  • 1
  • 1
Chris Cooper
  • 869
  • 2
  • 18
  • 42

1 Answers1

4

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");
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • 2
    (Good practice is to remove the object from the session once you won't need it anymore, so you will not waste memory) – Pierre Sevrain Dec 01 '11 at 11:04
  • 1
    If it's not a problem to share the data among all browser tabs/windows and have them all influence on it, then it's okay to put it in session. But in this particular case it seems to be data which is supposed to be edited on a per-request basis. I would then not put it in session. – BalusC Dec 01 '11 at 11:21
  • Although in this case I can actually put it on the session, I can foresee a situation where BalusC's point is valid (i.e. it is a request-specific attribute). In this case I assume I have no choice but to reload it in the "POST" so that it is available to the EL tags in the JSP. – Chris Cooper Dec 01 '11 at 13:18