jspmodel-view-controllerjstl

Displaying bean in JSTL


I've problem displaying details of a bean using JSTL. My servlet code is as follow

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    
    String username =request.getParameter("username");
    String password = request.getParameter("password");
    try {
        
        con = connection.connectionfile();
        
        pstmt=con.prepareStatement("select * from Login where Username=? and Password=?");
        pstmt.setString(1,username);
        pstmt.setString(2, password);
        rs = pstmt.executeQuery();
        
        if(rs.next())
        {
            ListGrabber grabber = new ListGrabber();
            List<DvdDetail> list = new ArrayList<DvdDetail>();
            list=grabber.getBeanList();
            
            request.setAttribute("listbean",list);
            RequestDispatcher view = request.getRequestDispatcher("listdetail.jsp");
            
            view.forward(request, response);
            
            
        }
        else
        {
            response.sendRedirect("loginerror.html");
        }
        
    } 
     catch(Exception ex)
     {
        ex.printStackTrace();
     }
    
    finally {            
        out.close();
        try
        {
            rs.close();
            con.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

Bean is as follow:

   public class DvdDetail {

private int id;
private String film_name;
private String released_date;
private String directed_by;
private String actor_name;
private String actress_name;
private int price;
private String banner;

public DvdDetail(int id,String film_name,String released_date,String directed_by,String actor_name,String actress_name,int price,String banner)
{
    
    this.id=id;
    this.film_name=film_name;
    this.released_date=released_date;
    this.directed_by=directed_by;
    this.actor_name=actor_name;
    this.actress_name=actress_name;
    this.price=price;
    this.banner=banner;
    
}

public String getActor_name() {
    return actor_name;
}

public void setActor_name(String actor_name) {
    this.actor_name = actor_name;
}

public String getActress_name() {
    return actress_name;
}

public void setActress_name(String actress_name) {
    this.actress_name = actress_name;
}

public String getBanner() {
    return banner;
}

public void setBanner(String banner) {
    this.banner = banner;
}

public String getDirected_by() {
    return directed_by;
}

public void setDirected_by(String directed_by) {
    this.directed_by = directed_by;
}

public String getFilm_name() {
    return film_name;
}

public void setFilm_name(String film_name) {
    this.film_name = film_name;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

public String getReleased_date() {
    return released_date;
}

public void setReleased_date(String released_date) {
    this.released_date = released_date;
}

        

}

How would I print out multiple movies name in a JSP page but using only JSTL. In JSP I usually solve it as

List<DvdDetail> list = request.getParameter("listbean");
for(int i=0;i<list.size();i++)
{
  DvdDetail bean = list.get(i);
  ///displaying usng out.print(bean.getProperty());
}

I need to achieve same thing but using JSTL.


Solution

  • Click on the info link in the popover on the JSTL tag you added on your question to get information about the JSTL and links to tutorials.

    What you want to achieve could be done like this:

    <ul>
        <c:forEach var="dvd" items="${listbean}">
            <li><c:out value="${dvd.film_name}"/></li>
        </c:forEach>
    </ul>