0

i want to populate combobox in my jsp using another jsp. i already try many thing but those doesn't work for me

jsp design code

<form id="obj" class="login-form" action="PickUpInvertProcess.jsp">
<table>
<tr>
<td class="td1">Client:</td>
<td class="td2"><select tabindex="1"name="clientname"> 
<c :forEach var="name" items="${obj.name}">
<option value="${name}">${name}</option>
</c :forEach>
</select>
</td>

using script in jsp to load comboBox value

<body>
<%!
public Set<String> loadDropDown() throws SQLException {
Set<String> CLIENTDATA = new HashSet<String>();
Dbcon dbc = new Dbcon();
Connection con = dbc.ConnDb();
Statement st = null;
ResultSet rs = null;
    try {
        String Query = "here query";
        st = con.createStatement();
        rs = st.executeQuery(Query);
        while(rs.next()){
            CLIENTDATA.add(rs.getString("name"));
        }
    } catch (Exception e) {
    } finally{
        dbc.disconnect(null, st, rs, null);
    }
    return CLIENTDATA;
}
%>
Community
  • 1
  • 1
tom
  • 11
  • 6

2 Answers2

0

Consider loading the list in the @Controller class and passing it as a model attribute. See here a simple example.

Then assuming the passed attribute is called names, the combobox can be filled the following way:

<select id="id_names" name="name">
    <c:forEach items="${names}" var="name">
        <option>${name}</option>
    </c:forEach>
</select>
mzalcmanis
  • 46
  • 1
  • 4
  • thanku, for your reply......but i want to populate combo box using another jsp...if you have any idea about that then, please tell me – tom Aug 25 '17 at 11:29
  • If you use the new jsp only for a concrete object from the model then simply use jsp:include directive. However, if you want to use it for arbitrary lists, then you will need to make a [custom tag](http://www.journaldev.com/2099/jsp-custom-tags-example-tutorial), because you can only pass string arguments to jsp files and not objects. – mzalcmanis Aug 25 '17 at 12:10
0

The following will iterate a collection of objects which exist is some scope i.e which have been placed into some scope by a previous process:

<c:forEach var="name" items="${clientDataList}">

You need then to put the results of your query into a valid scope. Using Request scope you can do this:

<%!
Set<String> CLIENTDATA = new HashSet<String>();
Dbcon dbc = new Dbcon();
Connection con = dbc.ConnDb();
Statement st = null;
ResultSet rs = null;
    try {
        String Query = "here query";
        st = con.createStatement();
        rs = st.executeQuery(Query);
        while(rs.next()){
            CLIENTDATA.add(rs.getString("name"));
        }
    } catch (Exception e) {
    } finally{
        dbc.disconnect(null, st, rs, null);
    }

    request.setAttribute("clientDataList", CLIENTDATA);
%>

However you should note that using Java code in JSP files went out of fashion in around 2002 and read this: How to avoid Java code in JSP files?

Alan Hay
  • 22,665
  • 4
  • 56
  • 110