gpt4 book ai didi

java - 显示从 Servlet 到 JSP 的 MySQL 查询结果

转载 作者:行者123 更新时间:2023-11-29 05:32:41 26 4
gpt4 key购买 nike

我试图将我的查询结果存储在一个字符串中,并通过将该字符串传递给它来将它们打印到我的 JSP 页面的底部。现在,JSP 页面最初显示良好,但当我单击按钮发布命令时没有任何反应。早些时候,当我从一个 html 页面访问 servlet,并使用 PrintWriter 打印我的所有输出时,我得到了要显示的结果,但它们会显示在一个单独的页面上。

1) 以这种方式存储 out 是个好主意,还是我应该把它做成不同于字符串的东西?

2) 如何获取查询结果以发布到 JSP 页面?

数据库Servlet.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);

在发送请求之前,必须设置属性

关于java - 显示从 Servlet 到 JSP 的 MySQL 查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com