How do i display database results in a JSP?
I have a control servlet that forward the request to the mode开发者_JAVA技巧l servlet.The model servlet retrieves results from a database and forward the display to the jsp.how do i display the result set in the jsp?Do i need to write the sql statement again in the jsp?
No, you use the request attributes map to pass data from the controlling Servlet to the JSP page.
Example. Controller Side:
void doGet(HttpServletRequest request, HttpServletResponse response)
{
List<String> names = Model.getNamesFromDB();
request.setAttribute("names", names);
// forward to JSP follows
...
}
Example. JSP page:
<%
List<String> names = (List<String>)request.getAttribute("names");
// do whatever you want with names
%>
精彩评论