Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retaining value of checkbox when returning back from a servlet

 
kan tao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I'm trying to retain the value of the checkbox when returning to the same servlet from another servlet. How can i implement it.

Scenario:

Servlet A has checkbox which post to servlet B then i want to go back to servlet A again and modify the checkbox that i selected before.


So how do i maintain the previously selected checkboxes [i want the checkbox to be checked when it reloads] ???

P.S. Hope my explanation is understandable.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is you checkbox coded - just a normal HTML checkbox, or is it from some kind of framework (JSF, MyFaces, etc)??

If it is a normal checkbox, you can use javascript (written from the Servlet), and if it is from a framework, there should be a property to set on the returning object.
 
kan tao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the idea of Httpsession but now my prob is the display part.

Here is some section of my code:

servelt A



servlet B




So when the submit button Back is press, How to retrive the session value in Servlet A and retain the checked value [display those checked boxes as checked]??
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4262
229
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to print all this HTML tags from a Servlet? No JSP?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just set the checked attribute onto the checkbox element that you wish to be checked.

And yeah, building up HTML inside strings inside a Servlet is a very poor practice.
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use the encodeRedirectURL() method in conjunction with sendRedirect() when redirecting to another part of your website. This must be done in order to property retain the session ID. From the JavaEE API:

All URLs sent to the HttpServletResponse.sendRedirect method should be run through this method. Otherwise, URL rewriting cannot be used with browsers which do not support cookies.
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please BeForthrightWhenCrossPostingToOtherSites. The same question was posted here.
 
kan tao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply everyone...


hi Michael Angstadt

Thanks for the reply. But i got another issue now ..

this code is suppose to save the session id and send to "servletA" where i can retrieve the session data through

But somehow its not working and returning NULL always. Can you point out where i'm doing it wrong??
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you also post the code that sets the attribute? If the attribute name you're using to set the attribute doesn't match the name you're using to get the attribute, then it would make sense that it's returning null.
 
kan tao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet B



Servlet A



I have even try using forward but still its not working.

Below is the link where i was discussing the issue :

http://www.java-forums.org/java-servlet/30663-retaining-value-checkbox-when-returning-back-servlet.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic