how to set default radio button comparing database value in jsp - java

Did a lot of research but couldn't find a working solution.Here's my question.
I have a list of radio buttons named branchlist and i am fetching a value named branch from database. Now i wish to set the radio button as checked whose value is equal to the value of branch. Here is my code:
<%
ArrayList<String> branchlist = new ArrayList<String>();
branchlist.add(0,"Computer Engineering");
branchlist.add(1,"Information Technology");
branchlist.add(2,"Electronic & Telecom");
branchlist.add(3,"Instrumentation Engineering");
branchlist.add(4,"Mechanical Engineering ");
branchlist.add(5,"Civil Engineering");
for(int i=0;i<branchlist.size();i++){
//System.out.println(branchlist.get(i)+"="+branch);
if(branchlist.equals(branch)){%> <--- branch is the string value I am fetching from database.
<input type="radio" name="branch" value="<%=branchlist.get(i)%>" checked="checked" ><%=branchlist.get(i) %><br>
<% }else {%>
<input type="radio" name="branch" value="<%=branchlist.get(i)%>" ><%=branchlist.get(i) %><br>
<%}
}%>

how about this:
<%
ArrayList<String> branchlist = new ArrayList<String>();
branchlist.add(0,"Computer Engineering");
branchlist.add(1,"Information Technology");
branchlist.add(2,"Electronic & Telecom");
branchlist.add(3,"Instrumentation Engineering");
branchlist.add(4,"Mechanical Engineering ");
branchlist.add(5,"Civil Engineering");
for(int i=0;i<branchlist.size();i++){
//System.out.println(branchlist.get(i)+"="+branch);
if(branchlist.get(i).toString().equals(branch)){%>
<input type="radio" name="branch" value="<%=branchlist.get(i)%>" checked="checked" ><%=branchlist.get(i) %><br>
<% }else {%>
<input type="radio" name="branch" value="<%=branchlist.get(i)%>" ><%=branchlist.get(i) %><br>
<%}
}%>

Related

ServletRequest.getParameterValues returns only first value

I am assigning an integer converted into string value to a hidden input through a loop where s1 is the loop iterator.
<% int k=0;
String s1 = null;
while(mar.next()){
k++;
s1 = Integer.toString(k);%>
<table>
<tr><td><b>User Name </b> </td> <td><%= mar.getString("UserName") %></td></tr>
<tr><td><b>Type </b></td><td> <%= mar.getString("Type") %></td></tr>
<tr><td><b>Complain </b></td> <td><%= mar.getString("Complaint") %></td></tr>
<tr><td><b>Status </b> </td> <td><%= mar.getString("Status") %></td></tr>
<tr><td><b>Date </b> </td> <td><%= mar.getString("Date") %></td></tr>
</table>
<form action = "demo1.jsp" method = "post">
<% out.println(mar.getString("Id")); %>
<input type="radio" name = "<%= mar.getString("Id") %>"value="pending"checked>Pending
<input type="radio" name = "<%= mar.getString("Id") %>" value="done">Done
<input type="submit" name = "B" id = "submit" >
<input type = "hidden" name = "try" value = <%= s1 %> >
<% out.println(s1); %>
</form>
<br>
<br>
<%
}
Below is the code which should return a string of values from 1 to 3 (values of s1)
String[] vals;
vals = new String[3];
out.println(vals.length);
vals = request.getParameterValues("try");
out.println(vals.length);
for(int i=0; i<vals.length; i++){
out.println("Hy");
out.println(vals[i]);
}
It is only returning one value which is the first one and printing vals.length prints 1.
You are creating three forms. For each form there will be separate request made. Since each form contains only one hidden input you will always get only one value for each request for below code
request.getParameterValues("try");
Now if you have to submit all the forms with one click. here or here are the answers

error submitting multiple checkboxes

I am using this scriplet within my jsp:
<%
String q3 = request.getParameter ("checkbox1");
session.setAttribute("q3", q3);
%>
This will get the values from these checkboxes
<p> Which of the following are associated with Threading? Select two </p>
<input type="checkbox" name="checkbox1" value="LiveLock">LiveLock<br>
<input type="checkbox" name="checkbox1" value="Stack Overflow">Stack Overflow<br>
<input type="checkbox" name="checkbox1" value="Heap">Heap<br>
<input type="checkbox" name="checkbox1" value="Starvation">Starvation<br>
<input type="submit" value="Next" >
Or rather..thats what it should do. But when i grab the values and print them out as so
<p>Good day <%= session.getAttribute("uname") %> </p>
<p>For question 1 you chose <%= session.getAttribute("q1") %> </p>
<p>For question 2 you chose <%= session.getAttribute("q2") %> </p>
<p>For question 3 you chose <%= session.getAttribute("q3") %> </p>
<p>For question 4 you chose <%= session.getAttribute("q4") %> </p>
The radio buttons for q1,2,4 work fine. the check box will only return the first value that is checked or rather. The value that comes first i.e. if i select "Heap" and then "Livelock", in the print outs it will display "LiveLock"
you should use request.getParameterValues() instead of request.getParameter() because checkbox names are same.
Remember that getParameterValues() returns array so you have to do
String q3[] = request.getParameter ("checkbox1");
and for retrieving values iterate it like below
for(String s:q3)
{
System.out.println(s);
}
for printing in the browser
you can do
for(String s:q3)
{
out.println(s);
}
Use request.getParameterValues to get multiple checkbox selection:
String[] q3 = request.getParameterValues ("checkbox1");
Store in session:
session.setAttribute("q3", request.getParameterValues("checkbox1"));
Traverse to display values:
<p>Good day <%= session.getAttribute("uname") %> </p>
<p>For question 1 you chose
<%String[] ans = (String[])session.getAttribute("q3");
for(String chkd : ans) {
out.print(chkd);
out.print(", ");
}%>
</p>
Note: use of scriplets are not recommended.

Link to the correct item page by selecting the list of items in for loop

This is my jsp page that retrieve the list of items from database using for loop
<%
itemManager mgr = new itemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<tr>
<td> <img border="0" src="<%=items[i].getItemImage() %>" width="100" height="100">
</td>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemID()%>">
<br/>
<%=items[i].getItemDesc()%>
<br/>
Start Bid : <%=items[i].getStartBid()%>
<br/>
Buy it now : <%=items[i].getEndBid()%>
<br/>
Bidding close on : <%=items[i].getDuration()%>
<br/>
<input type="submit" value="View">
<%
}
%></table>
This is the jsp page that link to the item you selected previously
<table border="1" align="center">
<%
itemManager mgr = new itemManager();
Item items = mgr.getItem((Integer)session.getAttribute("ITEM_DATA"));
%>
<tr>
<td> <b> <%=items.getItemName() %></b> </td>
</tr>
</table>
This is the servlet to store the session of the selected item id and forward to the correct item jsp page.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
int id = Integer.parseInt(request.getParameter("itemID"));
session.setAttribute("ITEM_DATA",id);
RequestDispatcher rd = request.getRequestDispatcher("viewItem.jsp");
rd.forward(request, response);
}
However, after I clicked the view button. It keeps linking to the itemID = 1.
The URL dispalys "/ItemServlet?itemID=1&itemID=2" .
In fact, if I click on itemID=2 the URL should display like this:
"/ItemServlet?itemID=2"
As a result, how can I achieve this? Thanks in advance!
The problem in your code is you are using a single form and dynamically creating Input box inside the form. So all the input box will be having same name. That's why when you submit the form all the input box values are sent as request parameters. I just reduced some part in your code for better under standing. Take this as your code
<form action="item" method="get">
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<tr>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemId()%>">
<input type="submit" value="View"> </td></tr>
<%
}
%></table>
</form>
When you run this code and if you check the rendered HTML code it will be look
<form action="item" method="get">
<table>
<tr><td>
aaa
<input type="text" name="itemID" value="1">
<input type="submit" value="View"> </td></tr>
<tr>
<td>
bbb
<input type="text" name="itemID" value="2">
<input type="submit" value="View"> </td></tr>
<tr><td>
ccc
<input type="text" name="itemID" value="3">
<input type="submit" value="View"> </td></tr>
</table>
</form>
Here All the Input box having same name as "itemID". As a solution you can create the form inside the for loop, so that while submitting only one Input box value will be sent as request.
Create form inside your for loop like below code.
<table>
<%
ItemManager mgr = new ItemManager();
Item[] items = mgr.getAllItems();
for(int i = 0; i < items.length; i++){
%>
<form action="item" method="get">
<tr>
<td>
<%=items[i].getItemName()%>
<input type="text" name="itemID" value="<%=items[i].getItemId()%>">
<input type="submit" value="View"> </td></tr>
</form>
<%
}
%></table>
Hope This will help you.
change the name of text field dynamically.
And use getQueryString() in servlet to find the itemId name and value. by using EL.I hope this will help you

Form Not Submitting after Jquery Validation

$('form#injuryReportSubmit').submit(function(){
if(($('form#injuryReportSubmit textarea.description').val().length <=0) || ($('form#injuryReportSubmit select.part').val()== "choose one...") ||($('form#injuryReportSubmit select.type').val() == "choose one...") || ($('form#injuryReportSubmit select.weather').val()=="choose one..."));
{
$('div#errorMessage').show();
return false;
}
});
The code above is used to validate a form before it submits. The problem is the form will not submit even when all the test are false. Can someone help?
the form is on a jsp and looks like
<form id ="injuryReportSubmit" method ="post" action="injuryReportingPage.html" >
<p class ="first" >Date Of Injury</p>
<p class ="second">Date Reported to Manager</p>
<input type="text" id="dateOfInjury" name="dateOfInjury">
<input type="text" id="dateReported" name ="dateReported">
<p class ="weather">Weather</p>
<p class ="injuryType">Injury Type</p>
<p class ="bodyPart">Body Part</p>
<p class ="time">Time Injury Occurred</p>
<p class ="description">Description</p>
<select class ="weather" name="weather">
<%if(InjuryReportController.getWeatherList() != null){ %>
<% for(Weather weather : InjuryReportController.getWeatherList()){%>
<option><%= weather.getWeatherCondition() %></option>
<%} }%>
<option >choose one...</option>
</select>
<select class ="type" name="injuryType">
<%if(InjuryReportController.getInjuryTypeList() != null){ %>
<% for(InjuryType injuryType : InjuryReportController.getInjuryTypeList()){%>
<option><%= injuryType.getInjuryTypeName() %></option>
<%} }%>
<option>choose one...</option>
</select>
<select class ="part" name="bodyPart">
<%if(InjuryReportController.getBodyPartList() != null){ %>
<% for(BodyPart bodyPart : InjuryReportController.getBodyPartList()){%>
<option><%= bodyPart.getBodyPartName() %></option>
<%} }%>
<option >choose one...</option>
</select>
<input type="text" id="timeP" name ="timeOfInjury" value="01:00 AM">
<textarea class ="description" rows="120" cols="670" name="description"></textarea>
<input id ="report" value="Submit Report" type ="submit">
</form>
Remove the semicolon from the end of line 5 of your code.
Currently what you've got is like this:
if (/*your conditions*/); // <- note the semicolon
{
...
return false;
}
This means that the block with the curly braces is not associated with the if statement and so will execute every time. Obviously then returning false every time cancels every submit.

How can I check for gender value by NameID?

I have this code that checks duplicate name id
NameRefBean myNameBean = new NameRefBean();
myNameBean.load(NameID);
if (!myNameBean.getErrFlag()) {
errExistFlag = true;
errCode = DUPLICATE_NAME_ID;
return(false);
I have switch case where it saves data from the jsp page form
case ACTION_SAVE:
if (hdTxnType.equalsIgnoreCase("Add")) {
if (validateInputData()) {
nameBean.setNameID(tbNameID);
nameBean.setName(tbName);
nameBean.setGender(tbGender);
hdTxnType = new String("Update");
}
} else {
nameBean.setNameID(hdNameID);
nameBean.setName(tbName);
nameBean.setGender(tbGender);
}
break;
My question is how can I check for gender value when particular nameid is selected? If user changes gender value from M to F, I need to give a warning on the jsp page saying "gender already exist do you want to modify it?"
Here is my jsp page
<% if (nameBean.getErrFlag()) {%>
<CENTER><b><font color=red><%= nameBean.getErrMsg() %></b></font></CENTER>
<% } %>
<TABLE WIDTH="800" BORDER="0">
<TR>
<TD><B>Name ID: </B></TD>
<% if (nameBean.getTxnType().equalsIgnoreCase("Add"))
{ %>
<TD><INPUT TYPE=TEXT NAME="tbNameID" VALUE="<%= nameBean.getnameID()%>" ></TD>
<% } else { %>
<TD><%= nameBean.getNameID()%> </TD>
<% } %>
<TD><B> Name: </B></TD>
<TD><INPUT TYPE=TEXT NAME="tbName" VALUE="<%= nameBean.getName()%>" ></TD>
<TD><B>Gender: </B></TD>
<TD><INPUT TYPE=TEXT NAME="tbGender" VALUE="<%= nameBean.getGender()%>" ></TD>
</TR>
<BR>
<TR>
<% if (userProfileBean.hasRole("FULL") ) { %>
<TD>
<INPUT TYPE=SUBMIT NAME="action" VALUE="Save Changes">
<% if (nameBean.getTxnType().equalsIgnoreCase("Update"))
{ %>
<INPUT TYPE=SUBMIT NAME="action" VALUE="Delete">
<% } %>
<INPUT TYPE=SUBMIT NAME="action" VALUE="Cancel">
</TD>
<% } %>
You could add a piece of JavaScript that print the warning, if a user changed the gender. The only thing you have to do then, is to enable this java script if the nameid is already set (then the name already exists, and every change should print the warning.) If the nameid is null, then it is a new name and you must not enable the java script warning functionality.

Categories

Resources