I have a Vector Method which returns value from the database to a JSP combobox. The Method has an Input Variable String which is stored in the session. I need to Pass the Value from the Session to the method invocation in the JSP but I am not getting the values from the Database.
Here is my method:
public Vector getHostel(String campus) throws IOException{
Vector v = new Vector();
Connection conn;
try{
conn = db.getDbConnection();
String sql = "select * from hostel_master where campus_code = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, campus);
ResultSet rs = ps.executeQuery();
while(rs.next()){
String host = rs.getString(1);
v.add(host)
}
}catch(Exception asd){
System.out.println(asd.getMessage());
}
return v;
}
And My JSP file extract:
<jsp:useBean id = "obj" class="com.app.dao.HostelsDao" scope = "page"/>
<% String campus = (String)session.getAttribute("campus_code");%> <%-- I really hate this --%>
<option selected value = "SELECT">SELECT</option>
<c:forEach var = "item" items = "${obj.getHostel(campus)}">
<option>${item}</option>
</c:forEach>
I am not getting anything in my select list but when I pass the Value directly I am getting correct output. I need this value to vary depending on the campus_code value in the session.
Rather than the EL call to obj.getHostel(campus)
, in your DAO you could have class property and setter for campus_code.
You could then set the value using a <c:set target="${obj}" property="campusCode">
after the useBean tag. You would then just change your DAO method getHostel(campus) to a simple getter getHostels()
which can be accessed with ${obj.hostels}
.
The session can be accessed in JSTL with the implicit session object: ${session}
so you should be able to do ${session.campus_code}
or ${session["campus_code"]}
rather than using the scriptlet.