aspose file tools*
The moose likes Servlets and the fly likes Retaining value of checkbox when returning back from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Retaining value of checkbox when returning back from a servlet" Watch "Retaining value of checkbox when returning back from a servlet" New topic
Author

Retaining value of checkbox when returning back from a servlet

kan tao
Greenhorn

Joined: Jul 09, 2010
Posts: 9
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

Joined: Oct 12, 2001
Posts: 1179

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.


Regards, Rene Larsen
Dropbox Invite
kan tao
Greenhorn

Joined: Jul 09, 2010
Posts: 9
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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3106
    
  41

Why do you need to print all this HTML tags from a Servlet? No JSP?


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61239
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

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.


SCJP 6 || SCWCD 5
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Also, please BeForthrightWhenCrossPostingToOtherSites. The same question was posted here.
kan tao
Greenhorn

Joined: Jul 09, 2010
Posts: 9
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

Joined: Jun 17, 2009
Posts: 273

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

Joined: Jul 09, 2010
Posts: 9
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
 
 
subject: Retaining value of checkbox when returning back from a servlet