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

1
  • 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. Commented Jul 16, 2014 at 19:40

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.