Dhruv Pal
Dhruv Pal

Reputation: 959

Call a java class function without using scriptlets in jsp

I need to call a class form jsp for which i don't want to use scriptlets.I am not sure either to use tld here or something else . I just need a hint

So using sciplets it was like this in jsp

<table width="99%" border="0" cellspacing="0" cellpadding="0">
 <%   sql= "select class,period,sub from timetable where uid='"+uid+"' " ;
                           rs = stmt.executeQuery(sql) ;
                           while (rs.next()) {      
                           %>

                            <tr>
                              <td class="table_img"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                  <td width="33%" height="19" class="notice_text"><div align="center"><%=rs.getString(2)%></div></td>
                                  <td width="29%" class="notice_text"><div align="center"><%=rs.getString(1)%></div></td>
                                  <td width="38%" class="notice_text"><div align="center"><%=rs.getString(3)%></div></td>
                                </tr>
                              </table></td>
                            </tr> 
                            <%                 }            %>
                          </table>    

So at first i create a class and function which fetches this data like this

public class TrHome extends ConnectionClass{


    public List<TimeTablePojo> getTimeTableDetails(String scid, String uid){
        Statement statement = getStatement();//getting connection form extended class
        ResultSet resultSet = null;
        String query = "select class,period,sub from timetable where uid='"+uid+"' " ;
        List<TimeTablePojo> listPojo = new ArrayList<TimeTablePojo>();
        try{
        resultSet = statement.executeQuery(query);
            while(resultSet.next()){
                TimeTablePojo tPojo = new TimeTablePojo();
                tPojo.setClas(resultSet.getString(1));
                tPojo.setPeriod(resultSet.getInt(2));
                tPojo.setSub(resultSet.getString(3));
                listPojo.add(tPojo);
            }
        }catch(SQLException se){
            System.err.println("sql exception in getTimeTableDetails(String scid, String uid) in TrHome.java : "+se);
        }finally{
            closeResultSet(resultSet);
            closeConnection();//closing connection
        }
        return listPojo;
    }

}   

But now i don't any idea how to create object of this class and call this function to get data in jsp.Well after geting data i can iterate it using jstl but problem is how to call this function. As per my understanding i have to create tld .Is that right or there is a way through? Also I don't want to set data in request in servlet.

Upvotes: 0

Views: 1833

Answers (1)

Pankaj
Pankaj

Reputation: 592

.You can use 'useBean' to access the class in the jsp,then you can call the method of the class by passing the parameters and process the

<jsp:useBean id="connBean" 
                class="packageName.TrHome ">

The method will return the list on which you can iterate in the jsp.

Upvotes: 2

Related Questions