hi, 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.
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<fypjdbClass> fypjdbList = new ArrayList<fypjdbClass>();
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<fypjdbClass> fypjdbList = new ArrayList<fypjdbClass>();
//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 < 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);
}
}
}
And this is the Servlet
package myarraylist;
public class arraylistforfypjServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public arraylistforfypjServlet() {
super();
}
public static ArrayList<fypjdbClass> fypjdbList = new ArrayList<fypjdbClass>();
/**
* @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<fypjdbClass> fypjdbList = new ArrayList<fypjdbClass>();// 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);
}
}
i dont know if my code is right, pls advise me. thanks lot ^^