I’ve written a JSP that has a form and the form contains a dropdown list and a texfield. The dropdown is populated from a mysql database using beans which act as DAOs and DTOs. Problem is the dropdown doesn’t display the values from the database but when I remove the textfield from the form and leave only the dropdown, it displays fine. someone knows why? Please let me know what to do. The code for the forms is below:
JSP that works:
<%@ page import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="camp" scope="session" class="p1.campusDao"/>
<%--
Document : addCamp
Created on : May 20, 2011, 11:28:31 AM
Author : ken
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
body {
background-color:#AFC7C7;
padding: 0;
margin: 0;
}
div.wrapper {
margin-left: 10%;
margin-right: 10%;
background-color:#6D7B8D;
height: 607px;
padding-top:0px;
border: thin solid #000000;
}
div#image{
padding-top:1%;
padding-bottom:1%;
}
div#adminlogin{
width:35%;
height:40%;
background-color:#AFC7C7;
border-width:thin;
border-style:solid;
border-color:#000000;
margin: 0 auto;
text-align:left;
overflow: hidden;
padding: 5px;
}
hr {
height:1px;
color:#000000;
background-color:#000000;
width:99%;
margin-left: 0 ;
margin-right: auto ;
border-style:solid;
}
.inputtext {
width: 250px;
height: 30px;
Font-Family:Arial;
Font-Size:18px
}
</style>
<script language="javascript" type="text/javascript">
function clearText(field){
if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;
}
</script>
</head>
<body>
<div class="wrapper">
<div id="image">
<p>
<img src="${pageContext.request.contextPath}/images/logo.gif"
alt="banner"
width=100%
height="100"
/></p>
</div><!--end of image div-->
<div id=adminlogin >
<p style="text-align:center">Adding Hostel</p>
<hr/>
<form id="cam" method="POST" action="${pageContext.request.contextPath}/getHos">
<select name="campuses" size="1" id="camps">
<c:forEach items="${camp.stateList}" var="ca">
<%--<option value="1"><c:out value="${ca.campnm}"/></option>--%>
<option>${ca.campnm}</option>
</c:forEach>
</select>
<br>
<input type="Submit" name="cmdSub" value="SUBMIT">
</form>
<p STYLE="color : #E41B17;">${message}</p>
<c:remove var="message" scope="session" />
</div><%--end of admin login div--%>
</div>
</body>
</html>
JSP with textfield in form and doesn’t display dropdown elements in UI:
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ page import="java.util.*" %>
<jsp:useBean id="campu" scope="session" class="p1.campusDao"/>
<%--
Document : addCamp
Created on : May 20, 2011, 11:28:31 AM
Author : ken
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
body {
background-color:#AFC7C7;
padding: 0;
margin: 0;
}
div.wrapper {
margin-left: 10%;
margin-right: 10%;
background-color:#6D7B8D;
height: 607px;
padding-top:0px;
border: thin solid #000000;
}
div#image{
padding-top:1%;
padding-bottom:1%;
}
div#adminlogin{
width:35%;
height:40%;
background-color:#AFC7C7;
border-width:thin;
border-style:solid;
border-color:#000000;
margin: 0 auto;
text-align:left;
overflow: hidden;
padding: 5px;
}
hr {
height:1px;
color:#000000;
background-color:#000000;
width:99%;
margin-left: 0 ;
margin-right: auto ;
border-style:solid;
}
.inputtext {
width: 250px;
height: 30px;
Font-Family:Arial;
Font-Size:18px
}
</style>
<script language="javascript" type="text/javascript">
function clearText(field){
if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;
}
</script>
</head>
<body>
<div class="wrapper">
<div id="image">
<p>
<img src="${pageContext.request.contextPath}/images/logo.gif"
alt="banner"
width=100%
height="100"
/></p>
</div><!--end of image div-->
<div id=adminlogin >
<p style="text-align:center">Adding Hostel</p>
<hr/>
<form method=POST id="hostel" action="${pageContext.request.contextPath}/getHos">
<p/>
<%--The textfield for hostel name--%>
<label>Hostel Name<input type="text" id="hostel" name="hosNm" value=""/></label><br/>
<br/>
<label>Campus Name <select name="campuses" size="1" id="hostel">
<c:forEach items="${campu.stateList}" var="cam">
<%--<option value="1"><c:out value="${cam.campnm}"/></option>--%>
<option>${cam.campnm}</option>
</c:forEach>
</select></label>
<br/>
<br/>
<input type="submit" value="Add" style="font-size:14pt; margin-left: 158px; height: 30px;"/>
</form>
<p STYLE="color : #E41B17;">${message}</p>
<c:remove var="message" scope="session" />
</div><%--end of admin login div--%>
</div>
</body>
</html>
In your second JSP (do you really have two separate JSP files?) you have a
instead of
Fix it accordingly. Then the
<c:xxx>tags will be parsed.I’d also get rid of
<%@ page import="java.util.*" %>since it is useless and only allows for poor practices (scriptlets).