1

I am trying to check/uncheck my checkbox field in JSP,based on the values stored in my db.The code snippet for jsp is:

<div class="form-group t-margin-top-10">
                           <label for="defaultContact" class="col-md-5 col-lg-4 control-label"></label>
                           <div class="col-lg-4 ">
                           <input type="checkbox" id="defaultContact${billingContactDto.billingContactId}"  name="defaultContact" /> Default Contact</div>

This is not showing default checked value (checked in this case). billingContactDto is contacts object, billingContactId is PK in billingContact object. billingContact.defaultContact is a string in billingContact(not shown here)

The question is how to fetch values of checkbox from db. Thanks in advance

user3400060
  • 299
  • 3
  • 9
  • 23
  • 1
    there are two things involved in this post. 1) set default state of checkbox based on value stored in db 2) how to fetch values from db. – Braj Jul 16 '14 at 19:40

1 Answers1

1

I am trying to check/uncheck my checkbox field in JSP,based on the values stored in my db

Try to use JSTL & EL instead of Scriplet elements. For database access use JSTL SQL Tag library.

It's better to move the database code in the Servlet.

steps to follow:

  • Just fetch the record from database
  • Store the result in an Object or List
  • Set it in request attribute
  • Redirect to JSP
  • Access the value from request attribute in JSP using JSTL or EL

Try with the HTML checked Attribute

Sample code:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:set var="gender" value="female"/>

Male <input type="checkbox" name="R1" 
            value="Male" <c:if test="${gender=='male'}">checked</c:if>>
Female <input type="checkbox" name="R1" 
            value="Female" <c:if test="${gender=='female'}">checked</c:if>>

Have a look at the similar post

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76