0 Replies Latest reply: Aug 8, 2008 1:22 AM by 843810 RSS

    resultset from servlet to jsp

    843810
      i new to jsp
      i am doing small project
      in this application i need to retrieve value from data base and it should be stored in combobox
      if compile this i am getting null pointer exception

      i did combobox using servlet and bean
      the retrieve value should be transfer form servlet to jsp -html select option
      please if any node give me the correct program or check this program
      where i did mistake please point it


      admin.java
      import java.io.*;
      import java.util.*;
      import java.sql.*;
      import javax.servlet.*;
      import javax.servlet.http.*;

      import java.util.ArrayList;


      public class admin extends HttpServlet{

      private ServletConfig config;

      public void init(ServletConfig config)

      throws ServletException{

      this.config=config;

      }

      public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException,IOException{

      PrintWriter out = response.getWriter();

      response.setContentType("text/html");

      admin1 ab=new admin1();     

      Connection connection;
      ResultSet rs;
      Statement statement;

      System.out.println("hi");




      ArrayList deptlist=new ArrayList();




      try {




      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

      connection = DriverManager.getConnection("jdbc:odbc:Record");

      statement=connection.createStatement();

      String sql = "Select NI from Record order by 1 asc";
                     
                     rs = statement.executeQuery(sql);
                               
                     while(rs.next())
                     {
                     

                     String NI= rs.getString("NI");

                     out.println(NI);

                     System.out.println(NI);               
                     
                deptlist.add(NI);               
                     

                     
                     }


                     request.getSession().setAttribute("NI",deptlist);
                RequestDispatcher dispatcher= getServletContext().getRequestDispatcher("/admin.jsp");
                     dispatcher.forward(request, response);
                     
                     }

                catch(Exception e1)
                {
                out.println("error");
                e1.printStackTrace();
                }
      }

      }



      admin.java

      public class admin1
      {
           private String ni, city, purpose ,dsn, sn, pa, sa, year;





      public admin1()
      {   
           
      sn ="";
      year ="";
      dsn="";
      ni="";
           city="";
           purpose="";
           pa="";
           sa="";     

      }

           
           
           
           public admin1(String sn, String year, String dsn, String ni, String city, String purpose, String sa, String pa)
           {

                this.sn = sn;
                this.year = year;
                this.dsn = dsn;
                this.ni = ni;
                this.city= city;
                this.purpose = purpose;
                this.sa = sa;
                this.pa = pa;

           }





           public void setSN(String n)
           {
                sn=n;          
           }
           public void setYear(String y)
           {
                year=y;
           }
           public void setDSN(String d)
           {
                dsn=d;
           }
           public void setNI(String na)
           {
                ni=na;
           }

           public void setCity(String c)
           {
                city=c;          
           }
           public void setPurpose(String p)
           {
                purpose=p;
           }
           public void setSA(String a)
           {
                sa=a;
           }
           public void setPA(String pe)
           {
                pa=pe;
           }

           
           

           
           public String getSN()
           {
                return sn;
           }

           public String getYear()
           {
                return year;
           }

           public String getDSN()
           {
                return dsn;
           }

           public String getNI()
           {
                return ni;
           }

           public String getCity()
           {
                return city;
           }

           public String getPurpose()
           {
                return purpose;
           }

           public String getSA()
           {
                return sa;
           }
           public String getPA()
           {
                return pa;
           }





      }


      Admin.jsp

      <%@ page language="java" %>
      <%@ page import ="java.sql.*;"%>



      <html>
      <head>
      <title>Login Page</title>

      </head>
      <body bgcolor="teal" >

      <h1><center><b><font color="yellow">Login</h1></center><font color="yellow">Sign Out</font></font></b></font></b>


      <hr size="7" color="yellow">
      <br>



      <form name="frm" action="admin" method="Post" >

      <%@ include file="left.jsp"%>

      <font color="Yellow" size="3">

      <select property="select" name="select" onchange="frm.submit();">
      <options collection="deptlist" property="select" key="NI" />

      <option>select university</option>



      </select><br>

      <br>

      <b>Sanction No:                  </b><input type="text" name="sno" value=""/><br>
      <br>

      <b>Year:                        
           </b><input type="text" name="year" value=""/><br>

      <br>
      <b>Date of Sanction No:    </b><input type="text" name="dsn" value=""/><br>
      <br>
      <b>Name of Institute:         </b><input type="text" name="ni" value="" size="50"/><br>
      <br>
      <b>City:                    
                </b><input type="text" name="city" value="" size="50"/><br>
      <br>
      <b>Purpose:                         </b><input type="text" name="purpose" value="" size="75"/><br>
      <br>
      <b>Sanction Amount:          </b><input type="text" name="sa" value=""/><br>
      <br>
      <b>Pending Amount:           </b><input type="text" name="pa" value=""/><br>
      <br>
      <br>

           <input type="submit" name="Update" value="Update" />
           <input type="submit" name="Delete" value="Delete" />

           <input type="reset" name="cancel" value="Cancel" />


      <br>



      </font>
      </form>
      </body>
      </html>