Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remembering the radiobtn/checkbox state between jsp pages

 
vanan saravanan
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i maintain the state of radio buttons and checkboxs in html forms when i move from one jsp page to another. Thank you for your help. Greatly appreciated..
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can keep their state in the user session, use setAttribute.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Radio buttons and checkboxes that are checked when the form that contains them is submitted will send a name/value pair to the server. Radio buttons and checkboxes that are unchecked will send nothing.

Form parameters are retrieved with the HttpServletRequest.getParameter(String) method; the String argument being the name.
If the name argument doesn't match any form parameters, getParameter will return null.

This means that calling getParameter will return null if the radio button or checkbox is unchecked. If a checkbox or radio button is checked, then calling getParameter with that object's name will return a value.

When generating the HTML for a radio button or checkbox, adding the attribute "checked" will cause that button or checkbox to be checked.

Example:
<input type="checkbox" name="someCheckbox" checked />
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moreover, for checkboxes with the same name, use getParameterValues.
 
Durgaprasad
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use the folowing code in the jsp

if(request.getParameterValues()!=null){
for(int i=0;i<request.getParameterValues().length;i++){
<input type=checkbox name="somename" if(request.getParameterValues()!=null)checked>

}
}

syntax and indentatins are not correct.
Hope uu can managae
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Durgaprasad
Welcome to JavaRanch!

We're pleased to have you here with us in the servlets forum, but there
are a few rules that need to be followed, and one is that proper names are
required. Please take a look at the
JavaRanch Naming Policy and
adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You can change it here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic