vrd
vrd

Reputation: 25

How to call Java method in JSP?

I m working on school project and I need to display academic year,section and medium dynamically from database in jsp file in drop down format. I am fetching the database values from java class and trying call tat java method in jsp to display those values but I am not getting nething der and i dnt want write query in jsp file.. please help me guyz m trying from last 3 days... my java class

public class EmpBean {
    public java.util.List dataList(){
         ArrayList list=new ArrayList();
         try {
             Class.forName("driver");
             Connection con = DriverManager.getConnection("url", "user", "pwd");
             Statement st = con.createStatement();
             System.out.println("hiiiii");
             ResultSet rs = st.executeQuery("select * from employee");

             while(rs.next()){
                 list.add(rs.getString("name"));
                 list.add(rs.getString("address"));
                 list.add(rs.getString("contactNo"));
                 list.add(rs.getString("email"));
             }

             System.out.println(rs.getString("contactNo"));
         }

         catch(Exception e){}

         return   list;
     }
 }

and my jsp file:

<%@page language="java" import="java.util.*" %>
<html>
    <body> 
        <table border="1" width="303">
            <tr>
                <td width="119"><b>Name</b></td>
            </tr>

           <%Iterator itr;%>
           <%
                EmpBean p = new EmpBean();
                List list= (List) p.dataList(); 
           %>

                for (itr=list.iterator(); itr.hasNext(); ) {
           %>
           <tr>
               <select name="" id="" style="width: 150px;"">
                   <option value="-1"><%=itr.next()%></option>
               </select>
           </tr>
           <%
               }
           %>
        </table>
    </body>
</html>

Edit 1:

Error message:

> The server encountered an internal error () that prevented it from
> fulfilling this request. exception org.apache.jasper.JasperException:
> An exception occurred processing JSP page
> /Administrative/collectFees.jsp at line 93 90: <td colspan="4"><div
> id="fndiv"> 91: <%Iterator itr;%> 92: <% List data=
> (List)request.getAttribute("data"); 93: for (itr=data.iterator();
> itr.hasNext(); ){ 94: %> 95: <select name="year1" id="yr1"
> style="width: 150px;" onclick="showDetails()"> 96: <option
> value="-1">><%=itr.next()%></option> root cause
> java.lang.NullPointerException

Upvotes: 1

Views: 39226

Answers (3)

BartoszMiller
BartoszMiller

Reputation: 1227

It's good to stick to good coding conventions.

Suming up the below thread: "The use of scriptlets (those <% %> things) in JSP is indeed highly discouraged"

How to avoid Java code in JSP files?

Upvotes: 3

Bhavik Shah
Bhavik Shah

Reputation: 5183

<tr>
    <td><select name="" id="" style="width: 150px;"">
            <option value="-1"><%=itr.next()%></option>
    </select></td>
</tr>

you forgot the td tag

Upvotes: 1

Grim
Grim

Reputation: 2040

Try

<%
java.util.List list = new EmpBean().dataList();
%>

To Itterate you can use

<select>
<%for(String txt : new EmpBean().dataList()){%>
    <option><%=txt%></option>
<%}%>
</select>

Upvotes: 7

Related Questions