在 java class 方法中创建了一个 HTML 命令并将其导入到 JSP 但它不起作用
created a HTML command in a java class method and imported it to JSP but it was not working
我在 eclipse 中创建了一个动态项目,然后创建了一个插入方法,然后将其添加到 jsp 及其工作中,然后我尝试创建一个读取方法,然后将所有内容添加到其中并导入jsp 但刚刚打印到控制台的 HTML 命令没有创建 table
这是我的Item.javaclass读取项目的方法
public String readItems() {
String output = "";
try {
Connection con = connect();
if (con == null) {
return "Error While connecting to the database for reading ";
}
//preapre the html table to be displayed
output = "<table boder= '1'><tr><th>Item Code</th>"
+"<th>Item Name</th><th>Item Price</th>"
+ "<th>Item Description</th>"
+ "<th>Update</th><th>Remove</th></tr>";
String query = "select * from item";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
// iterate through the rows in the result set
while(rs.next()) {
String itemID = Integer.toString(rs.getInt("itemID"));
String itemCode = rs.getString("itemCode");
String itemName = rs.getString("itemName");
String itemPrice = Double.toString(rs.getDouble("itemPrice"));
String itemDesc = rs.getString("itemDesc");
// Add a row into the html table
output += "<tr><td>" + itemCode + "</td>";
output += "<td>" + itemName + "</td>";
output += "<td>" + itemPrice + "</td>";
output += "<td>" + itemDesc + "</td>";
// buttons
output += "<td><input name='btnUpdate' "
+ " type='button' value='Update'></td>"
+ "<td><form method='post' action='items.jsp'>"
+ "<input name='btnRemove' "
+ " type='submit' value='Remove'>"
+ "<input name='itemID' type='hidden' "
+ " value='" + itemID + "'>" + "</form></td></tr>";
}
con.close();
// Complete the html table
output += "</table>";
}
catch (Exception e)
{
output = "Error while reading the items.";
System.err.println(e.getMessage());
}
return output;
}
然后我将它导入到 jsp
<%
Item itemObj = new Item();
System.out.print(itemObj.readItems());
%>
输出为
Successfully connectedItem CodeItem NameItem PriceItem DescriptionUpdateRemoveKdsjame200.0nice onekljames200.0nice<input name='bt.........
您需要通过 out
JspWriter 对象从 itemObj.readItems()
发送结果,而不是 System.out
,如果您希望它成为发送给客户端的内容的一部分。
我在 eclipse 中创建了一个动态项目,然后创建了一个插入方法,然后将其添加到 jsp 及其工作中,然后我尝试创建一个读取方法,然后将所有内容添加到其中并导入jsp 但刚刚打印到控制台的 HTML 命令没有创建 table
这是我的Item.javaclass读取项目的方法
public String readItems() {
String output = "";
try {
Connection con = connect();
if (con == null) {
return "Error While connecting to the database for reading ";
}
//preapre the html table to be displayed
output = "<table boder= '1'><tr><th>Item Code</th>"
+"<th>Item Name</th><th>Item Price</th>"
+ "<th>Item Description</th>"
+ "<th>Update</th><th>Remove</th></tr>";
String query = "select * from item";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
// iterate through the rows in the result set
while(rs.next()) {
String itemID = Integer.toString(rs.getInt("itemID"));
String itemCode = rs.getString("itemCode");
String itemName = rs.getString("itemName");
String itemPrice = Double.toString(rs.getDouble("itemPrice"));
String itemDesc = rs.getString("itemDesc");
// Add a row into the html table
output += "<tr><td>" + itemCode + "</td>";
output += "<td>" + itemName + "</td>";
output += "<td>" + itemPrice + "</td>";
output += "<td>" + itemDesc + "</td>";
// buttons
output += "<td><input name='btnUpdate' "
+ " type='button' value='Update'></td>"
+ "<td><form method='post' action='items.jsp'>"
+ "<input name='btnRemove' "
+ " type='submit' value='Remove'>"
+ "<input name='itemID' type='hidden' "
+ " value='" + itemID + "'>" + "</form></td></tr>";
}
con.close();
// Complete the html table
output += "</table>";
}
catch (Exception e)
{
output = "Error while reading the items.";
System.err.println(e.getMessage());
}
return output;
}
然后我将它导入到 jsp
<%
Item itemObj = new Item();
System.out.print(itemObj.readItems());
%>
输出为
Successfully connectedItem CodeItem NameItem PriceItem DescriptionUpdateRemoveKdsjame200.0nice onekljames200.0nice<input name='bt.........
您需要通过 out
JspWriter 对象从 itemObj.readItems()
发送结果,而不是 System.out
,如果您希望它成为发送给客户端的内容的一部分。