我试图将查询结果存储在一个字符串中,并通过将该字符串传递给它,将它们打印到我的JSP页面的底部。现在,JSP页面最初显示正常,但是当我单击按钮以发布命令时,没有任何反应。早些时候,当我从html页面访问servlet并使用PrintWriter将所有输出打印出来时,我将显示结果,但结果将显示在单独的页面上。
1)以这种方式存储是一个好主意,还是应该使它不同于字符串?
2)如何获取查询结果以发布到JSP页面?
databaseServlet.java
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; @SuppressWarnings("serial") public class databaseServlet extends HttpServlet { private Connection conn; private Statement statement; public void init(ServletConfig config) throws ServletException { try { Class.forName(config.getInitParameter("databaseDriver")); conn = DriverManager.getConnection( config.getInitParameter("databaseName"), config.getInitParameter("username"), config.getInitParameter("password")); statement = conn.createStatement(); } catch (Exception e) { e.printStackTrace(); } } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String out = "\n"; String query = request.getParameter("query"); if (query.toString().toLowerCase().contains("select")) { //SELECT Queries try { ResultSet resultSet = statement.executeQuery(query.toString()); ResultSetMetaData metaData = resultSet.getMetaData(); int numberOfColumns = metaData.getColumnCount(); for(int i = 1; i<= numberOfColumns; i++){ out.concat(metaData.getColumnName(i)); } out.concat("\n"); while (resultSet.next()){ for (int i = 1; i <= numberOfColumns; i++){ out.concat((String) resultSet.getObject(i)); } out.concat("\n"); } } catch (Exception f) { f.printStackTrace(); } } else if (query.toString().toLowerCase().contains("delete") || query.toLowerCase().contains("insert")) { //DELETE and INSERT commands try { conn.prepareStatement(query.toString()).executeUpdate(query.toString()); out = "\t\t Database has been updated!"; } catch (Exception l){ l.printStackTrace(); } } else { //Not a valid response out = "\t\t Not a valid command or query!"; } RequestDispatcher dispatcher = request.getRequestDispatcher("/dbServlet.jsp"); dispatcher.forward(request, response); request.setAttribute("queryResults", out); } }
dbServlet.jsp
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- dbServlet.html --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>MySQL Servlet</title> <style type="text/css"> body{background-color: green;} </style> </head> <body> <h1>This is the MySQL Servlet</h1> <form action = "/database/database" method = "post"> <p> <label>Enter your query and click the button to invoke a MySQL Servlet <textarea name = "query" cols="20" rows="5"></textarea> <input type = "submit" value = "Run MySQL Servlet" /> <input type = "reset" value = "Clear Command" /> </label> </p> </form> <hr> <%= request.getAttribute("queryResults"); %> </body> </html>
dispatcher.forward(request, response); request.setAttribute("queryResults", out);
应该是这样
request.setAttribute("queryResults", out); dispatcher.forward(request, response);
在分派请求之前,必须设置属性