1

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
Daniel Kutik
  • 6,997
  • 2
  • 27
  • 34
vrd
  • 25
  • 2
  • 2
  • 7
  • not displaying nething. its just total blank – vrd Oct 18 '12 at 09:41
  • 2
    [The homework tag has been deprecated/obsoleted and should no longer be used.](http://meta.stackexchange.com/questions/147100/trogdor-ate-my-homework-tag) – Lion Oct 18 '12 at 09:42
  • If you want to reduce the complexity of the JSP, follow best practices and move the Java out of the JSP and into Java classes (using beans, custom tags...). Always avoid using Scriptlets. Well-organized JSP applications contain no scriptlets at all (unless in some specific situations where it's absolutely necessary). – Lion Oct 18 '12 at 09:48
  • 1
    [This](http://stackoverflow.com/q/12947423/1037210) is your previous question appears to be the same as this one. – Lion Oct 18 '12 at 09:58
  • can i use java beans concept here foer displaying the acadenic year ,section ......etc..in dropdown format... – vrd Oct 18 '12 at 10:38

3 Answers3

7

Try

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

To Itterate you can use

<select>
<%for(String txt : new EmpBean().dataList()){%>
    <option><%=txt%></option>
<%}%>
</select>
Grim
  • 1,938
  • 10
  • 56
  • 123
3

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?

Community
  • 1
  • 1
BartoszMiller
  • 1,245
  • 1
  • 15
  • 24
  • You're right. But I'd consider this a minor case, compared to these "all my database stuff in a JSP" questions. At least the OP is using scriptlets for display purposes. – f_puras Oct 18 '12 at 11:10
1
<tr>
    <td><select name="" id="" style="width: 150px;"">
            <option value="-1"><%=itr.next()%></option>
    </select></td>
</tr>

you forgot the td tag

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40