I have a form which gets user information and saves it into database. My form consists of text fields and radio buttons. (I am using jsp, bean servlet, jdbc)
Now i have made another form which is to edit the user entered information, it also shows their previous entered information in the text fields.
I can display the previous entered information in the text fields but my problem is that I cannot show the previous checked radio buttons, how can i do that?
Below is an example of text field showing previous entered value
<tr>
<td>Email:</td>
<td><input type="text" name="editregisterEmail" maxlength="10" size="15" value="<jsp:getProperty name="StudentBean" property="email"/>"></td>
</tr>
The problem is here, how can I show users the previous checked radio button for them to recheck?
I have the following code which gets previous radio button value which was checked by user
<jsp:getProperty name="StudentBean" property="department"/>
Below is the problem
<tr>
<td>Department: </td>
<td>
<input type="radio" name="editregisterDepartment" value="Information Technology"> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration"> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering"> Engineering
</td>
</tr>
you better use expression language (EL) as below:
<tr>
<td>Department: </td>
<td>
<input type="radio" name="editregisterDepartment" value="Information Technology" ${requestScope['StudentBean'].department == 'Information Technology'? 'checked':''}> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration" ${requestScope['StudentBean'].department == 'Business Administration'? 'checked':''}> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering" ${requestScope['StudentBean'].department == 'Engineering'? 'checked':''}> Engineering
</td>
</tr>
requestScope can be change depending in which scope you have defined your bean
A simple switch will do.
<%
StudentBean s=new StudentBean();
switch(s.getDepartment()){
case "Information Technology":
%>
<input type="radio" name="editregisterDepartment" value="Information Technology" checked> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration"> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering"> Engineering
<%
break;
case "Business Administration":
%>
<input type="radio" name="editregisterDepartment" value="Information Technology"> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration" checked> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering"> Engineering
<%
break;
case "Engineering":
%>
<input type="radio" name="editregisterDepartment" value="Information Technology"> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration"> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering" checked> Engineering
<%
break;
}
%>
If you are not using JavaEE 7, you will not be able to switch on a string value, then you will have to use if
s, like this:
<%
StudentBean s=new StudentBean();
if(s.getDepartment().equals("Information Technology"){
%>
<input type="radio" name="editregisterDepartment" value="Information Technology" checked> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration"> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering"> Engineering
<%
}else if(s.getDepartment().equals("Business Administration"){
%>
<input type="radio" name="editregisterDepartment" value="Information Technology"> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration" checked> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering"> Engineering
<%
}else if(s.getDepartment().equals("Engineering"){
%>
<input type="radio" name="editregisterDepartment" value="Information Technology"> Information Technology
<input type="radio" name="editregisterDepartment" value="Business Administration"> Business Administration
<input type="radio" name="editregisterDepartment" value="Engineering" checked> Engineering
<%
}
%>
I have used the if condition. It worked for me in my jsp page. I am using java 8 and apache tomcat 6.
<%
String id=request.getParameter("id");
User u=UserDao.getRecordById(Integer.parseInt(id));
%>
...
...
<tr><td>Sex:</td>
<td>
<%if(u.getSex().equalsIgnoreCase("male")) { %>
<input type="radio" name="sex" value="male" checked/>Male
<input type="radio" name="sex" value="female"/>Female
<% } else if(u.getSex().equalsIgnoreCase("female")) {%>
<input type="radio" name="sex" value="male" />Male
<input type="radio" name="sex" value="female" checked/>Female
<% } %>
</td></tr>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.