Maxi
Maxi

Reputation: 743

Populate select menus with data from Servlet to JSP

I want to pass some values from database to populate a page with various select menus (around 5).

I can do this using JSTL SQL tag but it's not good practice.

How can I get this data from a single servlet?
Can I send multiple lists from one servlet?

The select menus are populated from different tables. I want to use RequestDispatcher to forward the lists to the jsp.

Upvotes: 1

Views: 1461

Answers (3)

Prakash K
Prakash K

Reputation: 11698

You can use separate ArrayList to store the various lists retrieved in the Servlet and then store each list as a request attribute as mentioned by @devsundar. You can retrieve these attributes in your JSP through the request implicit object.

And if you want to just send all the lists in a single request attribute then @KaipaMSarma's suggestion is good to have a HashMap<String, ArrayList>, so your servlet code might look something like this:

List list1 = getListOneFromDatabase();
List list2 = getListTwoFromDatabase();
List list3 = getListThreeFromDatabase();
// and so on ...

Map<String, List> requestListMap = new HashMap<String, List>();
requestListMap.put("list1", list1);
requestListMap.put("list2", list2);
requestListMap.put("list3", list3);
// and so on ...

request.setAttribute("reqListMap", requestListMap);

Hope this helps.

Upvotes: 3

Jayy
Jayy

Reputation: 2436

Yes you can do. From your servlet you can hit the Database(any number of tables) and store your result sets in collection object and pass those objects as attributes to JSP.

Upvotes: 1

18bytes
18bytes

Reputation: 6029

You can set the data from database in the request attribute and render it in your JSP.

http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#setAttribute(java.lang.String, java.lang.Object)

Upvotes: 1

Related Questions