Q:

Servlet, Jsp and Hibernate.  How to print a query result in a jsp page

I've a Java Servlet which takes a list of messages from a database using hibernate. 
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
    SessionFactory factory = session.getSessionFactory();
    Session s = factory.openSession(); 
    List<Message> messages = s.createQuery("FROM Message").list();
    //print this list in home.jsp
}

How can I send this messages to home.jsp? 

A:

SERVLET:
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        SessionFactory factory = session.getSessionFactory();
        Session s = factory.openSession(); 
        List<Message> messages = s.createQuery("FROM Message").list();

        //associate with a request attribute
        request.setAttribute("messages", message);

        //forward to your JSP
        request.getRequestDispatcher("messages.jsp").forward(request, response);
    }

JSP
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<html>
   <body>
      <%-- will iterate the messages collection put in 
           request scope in the servlet --%>
      <c:forEach items="${messages}" var="message">
         Message = ${message.someProperty"}
      </c:forEach>
   </body>
</html>

Useful references:
https://www.tutorialspoint.com/jsp/jstl_core_foreach_tag.htm
https://www.tutorialspoint.com/jsp/jsp_expression_language.htm

