This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes obtaining dropdown list items from mysql database in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "obtaining dropdown list items from mysql database in jsp" Watch "obtaining dropdown list items from mysql database in jsp" New topic
Author

obtaining dropdown list items from mysql database in jsp

Kenneth Owino
Greenhorn

Joined: Aug 09, 2010
Posts: 23
Hi! How can I get dropdown list items from mysql to my jsp page. Is it possible to use a servlet to connect to the database and fetch the values as an array and send them to the jsp which receives them in the dropdown list? some clue please cause this thing looks simple but I don't know how to go about with it
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
1)From Data Access Layer hit the DB and get the result .
2)Place the obtained data into the arrayList (BusinessUtil/Layer)and also in some scope(request,session).
3)Forward to the JSP page.
4)Iterate using JSTL.

The below example will give you some idea. Do the scriplet part into the BusinessLayer.
index.jsp
-----------
<%
List dataBase = new ArrayList();
dataBase.add("test1");
dataBase.add("test2");
dataBase.add("test3");
request.setAttribute("list",dataBase);
%>
${test}
<c:forEach var="result" items="${list}">
<cut value="${result}"/><br>
</c:forEach>

Note: Avoid using the scriplets in the JSP .
Kenneth Owino
Greenhorn

Joined: Aug 09, 2010
Posts: 23
Thank you. I got it done. created a DAO, DTO and populated the JSP using JSTL forEach and EL
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Nice. Using the JSTL and EL is far superior than using discredited scriptlets.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: obtaining dropdown list items from mysql database in jsp
 
Similar Threads
Problem with Cascading Dropdown List
dropdown from database
problem in database
How to dynamically populate
how to populate drop down list using servlet and jsp