so I have a Servlet that retrieves some data from the database. For each row of this data I created an object and added all objects to an ArrayList of that object.
Now, I need the Servlet to pass the ArrayList back to the caller JSP, and print each attribute from each object in the ArrayList.
Do you guys would suggest the best way to do this? They way I am doing it obviously isn't working.
This is my JSP:
<%--
Document : ChartData
Created on : Feb 11, 2014, 11:44:09 PM
Author : fabiolanza
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<script>
function myFunction() {
var request = new XMLHttpRequest();
request.open("GET", "http://localhost:8080/Test/Servlet2", true);
request.onreadystatechange = function() {
if (request.readyState === 4) {
<%
java.util.ArrayList<objects.Data> DataArray = (java.util.ArrayList<objects.Data>) request.getAttribute("DataArray");
for(objects.Data d: DataArray){
out.print(d.type);
out.print(d.value);
out.print(d.month);
}
%>
}
};
request.send(null);
}
</script>
<button onclick="myFunction()">Try it</button>
<br><a href="/Test/index.html">Home</a>
</body>
</html>
This is my Servlet: package servlet;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.ArrayList;
/**
*
* @author fabiolanza
*/
public class Servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ArrayList<objects.Data> DataArray = new ArrayList<objects.Data>();
request.setAttribute("DataArray", DataArray);
// JDBC driver name and database URL
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/Tests";
// Database credentials
String USER = "fabio";
String PASS = "hacking";
Connection conn = null;
Statement stmt = null;
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Execute SQL query
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM data";
ResultSet rs = stmt.executeQuery(sql);
out.println("<html><body>");
// Extract data from result set
while (rs.next()) {
objects.Data d = new objects.Data(rs.getString("Type"), rs.getInt("Value"), rs.getString("Month"));
DataArray.add(d);
}
String link = "/Test/Database.html";
out.println("<a href="+link+">Database</a>");
out.println("</body></html>");
// request.getRequestDispatcher("ChartData.jsp").forward(request, response);
// Clean-up environment
out.close();
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.println(e);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Thanks!