Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checkbox Isuue

 
Sonu Sen
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am using pure jsp, servlets and Hibernate.I am not using any tag library or struts. I have a one form in this form i have a 4-5 check boxes. my question is How to insert the checkbox value in the database ?

My POJO file is .....

public boolean isBwTVFlag(){
return this.bwTVFlag;
}

public void setBwTVFlag(boolean bwTVFlag){
this.bwTVFlag = bwTVFlag;
}

public boolean isColorTVFlag(){
return this.colorTVFlag;
}

public void setColorTVFlag(boolean colorTVFlag){
this.colorTVFlag = colorTVFlag;
}

public boolean isPhoneFlag(){
return this.phoneFlag;
}

public void setPhoneFlag(boolean phoneFlag){
this.phoneFlag = phoneFlag;
}

public boolean isSingleBedFlag(){
return this.singleBedFlag;
}

public void setSingleBedFlag(boolean singleBedFlag){
this.singleBedFlag = singleBedFlag;
}


My Servlet Is........


Boolean bwTVFlag = Boolean.valueOf(request.getParameter("bwTVFlag"));

Boolean colorTVFlag = Boolean.valueOf(request.getParameter("colorTVFlag"));

Boolean phoneFlag = Boolean.valueOf(request.getParameter("phoneFlag"));

Boolean singleBedFlag = Boolean.valueOf(request.getParameter("singleBedFlag"));

Boolean twinBedFlag = Boolean.valueOf(request.getParameter("twinBedFlag"));

String flag = request.getParameter("editFlag");

if(flag.equalsIgnoreCase("true")){
String id = request.getParameter("editID");
-------}
else{

rmb.setBwTVFlag(bwTVFlag);
rmb.setColorTVFlag(colorTVFlag);
rmb.setPhoneFlag(phoneFlag);
rmb.setSingleBedFlag(singleBedFlag);
rmb.setTwinBedFlag(twinBedFlag);


and my jsp page is....

<input type="hidden" name="editFlag" value="true">
<input type="hidden" name="editID" value="<%=rmb.getRoomId()%>">

<div style="width:200px;margin-left:70px;height:0px;position:relative;top:-28px;"><br />

<ul class="checklist cl3">
<li class="alt"><label for="c1"><input id="c1" name="bwTVFlag" type="checkbox" value="true"/>B/W T.V</label></li>
<li><label for="c2"><input id="c2" name="colorTVFlag" type="checkbox" value="true" />Color T.V</label></li>
<li class="alt"><label for="c3"><input id="c3" name="phoneFlag" type="checkbox" value="true" />Phone</label></li>
<li><label for="c4"><input id="c4" name="singleBedFlag" type="checkbox" value="true" />Single Bed</label></li>
<li class="alt"><label for="c5"><input id="c5" name="twinBedFlag" type="checkbox" value="true" />Twin Bed</label></li>


I hope i am clere ,,,Please help me...
 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You can create a seperate DAO for inserting these values

-----DAO will contain parameters for checkbox values



-----Your servlet code will contain something like this
 
Sonu Sen
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ujjwal for your reply....

But ujjwal i dont want to add another file ..When i click on the Submit button in jsp page the value should insert into the database. I know that in the jsp page, On the Value Attribute of the checkbox some code is add but i can't get that code..

My Full Servlet is....

if(act.equalsIgnoreCase("addRoomBed")){
log.info("inside addRoomBed Action");
RoomBed rmb = null;
String wardName = request.getParameter("wardNameCB");
String roomNo = request.getParameter("roomNoTF");
String rate = request.getParameter("rateTF");
String bedNo = request.getParameter("bedNoTF");
Boolean bwTVFlag = Boolean.valueOf(request.getParameter("bwTVFlag"));
Boolean colorTVFlag = Boolean.valueOf(request.getParameter("colorTVFlag"));
Boolean phoneFlag = Boolean.valueOf(request.getParameter("phoneFlag"));
Boolean singleBedFlag = Boolean.valueOf(request.getParameter("singleBedFlag"));
Boolean twinBedFlag = Boolean.valueOf(request.getParameter("twinBedFlag"));
String flag = request.getParameter("editFlag");
if(flag.equalsIgnoreCase("true")){
String id = request.getParameter("editID");
RoomBedBo rbBo = new RoomBedBo();
List editRoomBed1 = rbBo.getRoomBed(Long.parseLong(id.trim()));
rmb = (RoomBed)editRoomBed1.get(0);
}else{
rmb= new RoomBed();
}
//rmb.setWardName(wardName);
rmb.setRoomNo(roomNo);
rmb.setRate(rate);
rmb.setBedNo(bedNo);
rmb.setActive(new Boolean(true));
/*rmb.setBwTVFlag(new Boolean(true));
rmb.setColorTVFlag(new Boolean(false));
rmb.setPhoneFlag(new Boolean(false));
rmb.setSingleBedFlag(new Boolean(false));
rmb.setTwinBedFlag(new Boolean(false));*/
rmb.setBwTVFlag(bwTVFlag);
rmb.setColorTVFlag(colorTVFlag);
rmb.setPhoneFlag(phoneFlag);
rmb.setSingleBedFlag(singleBedFlag);
rmb.setTwinBedFlag(twinBedFlag);


RoomBedBo rbBo = new RoomBedBo();
boolean saveRecord = rbBo.addRoomBed(rmb);
if(saveRecord = false) {
response.sendRedirect("/error.jsp");
rbBo=null;
}
else {
List roomBedList = rbBo.getAllRoomBedList(1);
request.setAttribute("roomBedList", roomBedList);
request.setAttribute("nextPageNumber", 2);
request.setAttribute("previousPageNumber", 1);
getServletContext().getRequestDispatcher("/RoomBedSetup.jsp").forward(request, response);
//neb=null;
}


And My jsp file is.......


<input type="hidden" name="Action" value="addRoomBed"> <!-- for new and Save -->
<%
if (request.getAttribute("editRoomBedList") != null) {
List getRoomBedList = (List) request.getAttribute("editRoomBedList");

if (getRoomBedList.size() > 0) {
RoomBed rmb =(RoomBed)getRoomBedList.get(0) ;
//out.println("------------"+rmb);
%>
<input type="hidden" name="editFlag" value="true"> <!-- for new and Save -->
<input type="hidden" name="editID" value="<%=rmb.getRoomId()%>"> <!-- for new and Save -->
<DIV class = "screenLableInputDiv"> <!--Div for Patient F Name-->
<Div class = "screenLable"> Ward Name </Div>
<select name="wardNameCB" size="1" >
<option value="" selected>Select</option>


</select>
</Div> <!--Div for Patient Name ends-->


<DIV class = "screenLableInputDiv"> <!--Div for Patient F Name-->
<Div class = "screenLable"> Room No. </Div>
<Div> <input type = "text" name = "roomNoTF" maxlength ="20" value="<%=rmb.getRoomNo()%>"></input> </Div>
</Div> <!--Div for Patient Name ends-->
<DIV class = "screenLableInputDiv"> <!--Div for Patient F Name-->
<Div class = "screenLable"> Rate </Div>
<Div> <input type = "text" name = "rateTF" maxlength ="20" value="<%=rmb.getRate()%>"></input> </Div>
</Div> <!--Div for Patient Name ends-->
<DIV class = "screenLableInputDiv"> <!--Div for Patient F Name-->
<Div class = "screenLable"> Bed No. </Div>
<Div> <input type = "text" name = "bedNoTF" maxlength ="20" value="<%=rmb.getBedNo()%>"></input> </Div>
</Div> <!--Div for Patient Name ends-->



<%
}
}else{
%>
<input type="hidden" name="editFlag" value="false"> <!-- for new and Save -->
<DIV class = "screenLableInputDiv"> <!--Div for Patient F Name-->
<Div class = "screenLable"> Ward Name </Div>
<select name="WardName" size="1">
<option value="" selected>Select</option>
<%Iterator itr;

if(request.getAttribute("wardList")!=null){
List wardname= (List)request.getAttribute("wardList");
if(wardname.size()>0){
for (int i=0;i<wardname.size();i++ )
{
Ward wrd = (Ward)wardname.get(i);
%>
<option value=<%=wrd.getWardName()%>><%=wrd.getWardName()%></option>
<% }
}
}

%>
</select>
</DIV> <!--DIV for Patient Name ends-->

<DIV class = "screenLableInputDiv"> <!--DIV for Patient F Name-->
<Div class = "screenLable"> Room No. </Div>
<Div> <input type = "text" name = "roomNoTF" maxlength ="20"></input> </Div>
</Div> <!--Div for Patient Name ends-->
<DIV class = "screenLableInputDiv"> <!--Div for Patient F Name-->
<Div class = "screenLable"> Rate </Div>
<Div> <input type = "text" name = "rateTF" maxlength ="20"></input> </Div>
</Div> <!--Div for Patient Name ends-->
<DIV class = "screenLableInputDiv"> <!--Div for Patient F Name-->
<Div class = "screenLable"> Bed No. </Div>
<Div> <input type = "text" name = "bedNoTF" maxlength ="20"></input> </Div>
</Div> <!--Div for Patient Name ends-->

<div style="width:200px;margin-left:70px;height:0px;position:relative;top:-28px;"><br />

<ul class="checklist cl3">
<li class="alt"><label for="c1"><input id="c1" name="bwTVFlag" type="checkbox" value="true"/>B/W T.V</label></li>
<li><label for="c2"><input id="c2" name="colorTVFlag" type="checkbox" value="true" />Color T.V</label></li>
<li class="alt"><label for="c3"><input id="c3" name="phoneFlag" type="checkbox" value="true" />Phone</label></li>
<li><label for="c4"><input id="c4" name="singleBedFlag" type="checkbox" value="true" />Single Bed</label></li>
<li class="alt"><label for="c5"><input id="c5" name="twinBedFlag" type="checkbox" value="true" />Twin Bed</label></li>
</div>
 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Check that you are not getting null values here :



if you are not getting null then, try my below code

 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that the session is org.hibernate.Session; and not HTTPSession
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Will take care from next time......

Thanks...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic