1

Possible Duplicate:
Passing Object from JSP to Servlet

I have an object that is being sent successfully from a servlet to a jsp. In that jsp page I have a button and when user click on that button the same object should be sent from that jsp page to another servlet. how can I do that ??

Community
  • 1
  • 1
user1407310
  • 173
  • 4
  • 4
  • 9

1 Answers1

4

Send it as request attribut when clicking on button a) Set request attrbute in jsp

request.setAttribute("thatobject",thatobject);

In Servlet retrive it as

Thatobject obj = (ThatObject) request.getAttribute("thatobject");

b) Set the object in session session.setAttribute("thatobject",thatobject) and retrive it as

Thatobject obj = (ThatObject) session.getAttribute("thatobject");

Oh my mistake Updated...

Note: Also iam not providing null check. I think you can handle it

Edit:

Even you can do other way, which is very sofistiacted way,

a) Create a Bean class
b) provide Object as a attribute in bean class and provide getter and setter methods too
C) in jsp use that bean to set the object when the page is submitting to servlet
d) retrive the object from the bean in servelt

Edit 2:

Please check this link there is clear explanation. JSP2Servlet

Edit 3

Note : If you are submiting form , the request becomes new request and the object will become null, when you use request.getattribute in servlet. So above approach will not work on Form submit in jsp.

So to overcome this please folw the instructions provide in below link

Passing Object from JSP to Servlet

Community
  • 1
  • 1
developer
  • 9,116
  • 29
  • 91
  • 150