Ajax + servlet + java + bean + jsp + mysql
Ajax + servlet + java + bean + jsp + mysql
Hi,
i've a problem with my web app, i'm new to datatable and jsp, and i cannot get it work.
What i want to do is really simple: show a datatable that is populated with an ajax call to the servlet, this servlet get the data from the bean (that get it from the db).
here my code:
JSP
[code]
id
desc
model
$(document).ready(function() {
$(".jqueryDataTable").dataTable( {
"bProcessing": false,
"bServerSide": false,
"sAjaxSource": "./PopulateTableDevice",
"bJQueryUI": true,
"aoColumns": [
{ "mData": "id" },
{ "mData": "desc" },
{ "mData": "model" }
]
} );
} );
[/code]
Servlet PopulateTableDevice
[code]
@WebServlet("/PopulateTableDevice")
public class PopulateTableDevice extends HttpServlet {
private static final long serialVersionUID = 1L;
public PopulateTableDevice() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
List deviceList = DeviceDaoBean.getAllDevice();
DataTableDevice dtd = new DataTableDevice();
dtd.setAaData(deviceList);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(dtd);
out.print(json);
}
}
[/code]
DeviceBean
[code]
public class DeviceBean {
private Integer id;
private String desc;
private String model;
... getter and setter...
[/code]
DeviceDaoBean
[code]
public class DeviceDaoBean {
static Connection currentCon = null;
static ResultSet rs = null;
public static ArrayList getAllDevice() {
Statement stmt = null;
String searchQuery = "select * from DEVICE";
ArrayList deviceList = new ArrayList();
try {
currentCon = ConnectionManager.getConnection();
stmt = currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
while(rs.next()) {
DeviceBean device = new DeviceBean();
device.setDesc(rs.getString("DESC"));
device.setId(rs.getInt("ID"));
device.setModel(rs.getInt("MODEL"));
deviceList.add(device);
}
ConnectionManager.closeConnection(currentCon, stmt);
rs.close();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return deviceList;
}
}
[/code]
MySQL table
[code]
CREATE TABLE DEVICE
(
ID INT NOT NULL AUTO_INCREMENT,
DESC CHAR(255) CHARACTER SET utf8,
MODEL CHAR(255) CHARACTER SET utf8,
PRIMARY KEY (ID)
);
[/code]
Probably my approach is wrong, i think in a way too simple. What's wrong with this code?
I can't find on google a decent datatable's example that works with java beans and servlet, any suggestions?
My next thing to do, if i fix this problem, is to get the column name dynamically from the java bean.
Kind Regards,
Stefano.
i've a problem with my web app, i'm new to datatable and jsp, and i cannot get it work.
What i want to do is really simple: show a datatable that is populated with an ajax call to the servlet, this servlet get the data from the bean (that get it from the db).
here my code:
JSP
[code]
id
desc
model
$(document).ready(function() {
$(".jqueryDataTable").dataTable( {
"bProcessing": false,
"bServerSide": false,
"sAjaxSource": "./PopulateTableDevice",
"bJQueryUI": true,
"aoColumns": [
{ "mData": "id" },
{ "mData": "desc" },
{ "mData": "model" }
]
} );
} );
[/code]
Servlet PopulateTableDevice
[code]
@WebServlet("/PopulateTableDevice")
public class PopulateTableDevice extends HttpServlet {
private static final long serialVersionUID = 1L;
public PopulateTableDevice() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();
List deviceList = DeviceDaoBean.getAllDevice();
DataTableDevice dtd = new DataTableDevice();
dtd.setAaData(deviceList);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(dtd);
out.print(json);
}
}
[/code]
DeviceBean
[code]
public class DeviceBean {
private Integer id;
private String desc;
private String model;
... getter and setter...
[/code]
DeviceDaoBean
[code]
public class DeviceDaoBean {
static Connection currentCon = null;
static ResultSet rs = null;
public static ArrayList getAllDevice() {
Statement stmt = null;
String searchQuery = "select * from DEVICE";
ArrayList deviceList = new ArrayList();
try {
currentCon = ConnectionManager.getConnection();
stmt = currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
while(rs.next()) {
DeviceBean device = new DeviceBean();
device.setDesc(rs.getString("DESC"));
device.setId(rs.getInt("ID"));
device.setModel(rs.getInt("MODEL"));
deviceList.add(device);
}
ConnectionManager.closeConnection(currentCon, stmt);
rs.close();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return deviceList;
}
}
[/code]
MySQL table
[code]
CREATE TABLE DEVICE
(
ID INT NOT NULL AUTO_INCREMENT,
DESC CHAR(255) CHARACTER SET utf8,
MODEL CHAR(255) CHARACTER SET utf8,
PRIMARY KEY (ID)
);
[/code]
Probably my approach is wrong, i think in a way too simple. What's wrong with this code?
I can't find on google a decent datatable's example that works with java beans and servlet, any suggestions?
My next thing to do, if i fix this problem, is to get the column name dynamically from the java bean.
Kind Regards,
Stefano.
This discussion has been closed.
Replies
the issue was here:
[code]
"sAjaxSource": "./PopulateTableDevice",
[/code]
fixed to
[code]
"sAjaxSource": "../PopulateTableDevice",
[/code]