I'm writing a jsp where in the data has to be pulled from database and display it in different rows. Currently, I'm able to pull the data but unable to know how can i display them in rows. Below is my code.
<html>
<head>
<script>
var request;
function sendInfo() {
var v = document.vinform.t1.value;
var url = "Controller?val=" + v;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
try {
request.onreadystatechange = getInfo;
request.open("GET", url, true);
request.send();
} catch (e) {
alert("Unable to connect to server");
}
}
function getInfo() {
if (request.readyState == 4) {
var val = request.responseText;
document.getElementById('amit').innerHTML = val;
}
}
</script>
</head>
<body>
<marquee>
<h1>This is an example of ajax</h1>
</marquee>
<form name="vinform">
Enter id:<input type="text" name="t1" onkeyup="sendInfo()">
</form>
<span id="amit"> </span>
</body>
</html>
DataDAO
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
public class DataDao {
private Connection connection;
public DataDao() throws Exception {
connection = DBUtility.getConnection();
}
public ArrayList<String> getFrameWork(String frameWork) {
ArrayList<String> list = new ArrayList<String>();
PreparedStatement ps = null;
String data;
try {
ps = connection.prepareStatement("select * from statusTable where [case owner]=?");
ps.setString(1, frameWork);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
data = rs.getString("case Number");
list.add(data);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return list;
}
}
Controller
import java.io.IOException;
import java.util.ArrayList;
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 com.dao.DataDao;
import com.google.gson.Gson;
@WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
try {
String id = request.getParameter("val");
DataDao dataDao = new DataDao();
ArrayList<String> list = dataDao.getFrameWork(id);
String searchList = new Gson().toJson(list);
response.getWriter().write(searchList);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
DBUTILITY
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtility {
private static Connection connection = null;
public static Connection getConnection() throws Exception {
if (connection != null)
return connection;
else {
// Store the database URL in a string
String userName = "sa";
String password = "T!ger123";
String url = "jdbc:sqlserver:XXXXXX;DatabaseName=TEST";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// set the url, username and password for the databse
connection = DriverManager.getConnection(url, userName, password);
return connection;
}
}
}
When i run this program the output is as in below format.
["2086351 ","2086356 ","2086357 ","2086358 ","2086358 "]
But i want the data to be displayed in a table with the values in different rows(values are separated by a line in the screenshot).
I know that we can get this done by using.
<table>
<c:forEach var="rowObj" items="${myClass.myArrayList}">
<tr>
<td>${rowObj.column1}</td>
<td>${rowObj.column2}</td>
<td>${rowObj.column3}</td>
</tr>
</c:forEach>
</table>
But i'm unable to know where is this supposed to be called. Please let me know how can i fix this.
Here the problem is not about getting the data, it is how it is displayed.
Thanks