title: Java:how to pass value from class/bean to servlet
body: <p>i am new to java, i'm having problem passing value from a class/bean (which are stored in arraylist) to servlet. any idea how can i achieve that? below is my code.</p>
  
  <pre><code>package myarraylist;
  
  public class fypjdbClass {
  
  String timezone;
  String location;
  
  public String getTimezone() {
      return timezone;
  }
  public void setTimezone(String timezone) {
      this.timezone = timezone;
  }
  public String getLocation() {
      return location;
  }
  
  
  public void setLocation(String location) {
      this.location = location;
  }
  
  public fypjdbClass() {
      super();
      ArrayList&lt;fypjdbClass&gt; fypjdbList = new ArrayList&lt;fypjdbClass&gt;();
      this.timezone = timezone;
      this.location = location;
  }
  
  public static void main(String[] args) {
  
  
      //Establish connection to MySQL database
      String connectionURL = "jdbc:mysql://localhost/fypjdb";
      Connection connection=null;
      ResultSet rs;
  
      try {
           // Load the database driver
          Class.forName("com.mysql.jdbc.Driver");
  
          // Get a Connection to the database
          connection = DriverManager.getConnection(connectionURL, "root", ""); 
          ArrayList&lt;fypjdbClass&gt; fypjdbList = new ArrayList&lt;fypjdbClass&gt;();
          //Select the data from the database
             String sql = "SELECT location,timezone FROM userclient";
          Statement s = connection.createStatement();
          //Create a PreparedStatement
          PreparedStatement stm = connection.prepareStatement(sql);
          rs = stm.executeQuery();
          //rs = s.getResultSet();
          while(rs.next()){
  
              fypjdbClass f = new fypjdbClass(); 
  
              f.setTimezone(rs.getString("timezone"));
              f.setLocation(rs.getString("location"));
  
              fypjdbList.add( f);
  
          }
  
  
  
          for (int j = 0; j &lt; fypjdbList.size(); j++) {
              System.out.println(fypjdbList.get(j));
  
            }
          //To display the number of record in arraylist
          System.out.println("ArrayList contains " + fypjdbList.size() + " key value pair.");
  
  
          rs.close ();
          s.close ();
          }catch(Exception e){
          System.out.println("Exception is ;"+e);
          }
  
  }
  }
  </code></pre>
  
  <p>And this is the Servlet</p>
  
  <pre><code>package myarraylist;
  
  public class arraylistforfypjServlet extends HttpServlet {
  
  private static final long serialVersionUID = 1L;
  
  /**
   * @see HttpServlet#HttpServlet()
   */
  public arraylistforfypjServlet() {
      super();
  }
  
  public static ArrayList&lt;fypjdbClass&gt; fypjdbList = new ArrayList&lt;fypjdbClass&gt;();
  
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) 
              throws ServletException, IOException {
      //processRequest(request, response); 
  
      RequestDispatcher rd = request.getRequestDispatcher("/DataPage.jsp"); //You could give a relative URL, I'm just using absolute for a clear example.
      ArrayList&lt;fypjdbClass&gt; fypjdbList = new ArrayList&lt;fypjdbClass&gt;();// You can use any type of object you like here, Strings, Custom objects, in fact any object.
      request.setAttribute("fypjdbList", fypjdbList);
      rd.forward(request, response);
  }
  
  protected void doPost(HttpServletRequest request, HttpServletResponse response) 
              throws ServletException, IOException {
      //processRequest(request, response);
      doGet(request,response);
  }
  
  }
  </code></pre>
  
  <p>i dont know if my code is right, pls advise me. thanks lot ^^</p>