1

运行 post 方法后,我无法将输出字符串打印到我的 .jsp 文件中。下面是我的 .java 和 .jsp 文件。我已经删除了那里的代码,因为此时我知道我所尝试的远非直观。我只是在我知道两行代码需要去的地方留下了评论。我只是想在 servlet 执行 SQL 命令后在我的 .jsp 文件中打印字符串“out”。

编辑:我终于将所有输出都转到 JSP 页面。我现在遇到的唯一问题是,我将查询结果存储在输出字符串中的不太理想的方法导致我的输出是一长串结果,而不是一个很好的间距或表格。我知道在 jsp 的表中循环遍历结果会更好,但我似乎也无法让它工作。

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>Hello World!</h1>
    <h2>This is the MySQL Servlet</h2>
    <form action = "/database/database" method = "post">
    <p>
        <label>Enter your query and click the button to invoke a MySQL Servlet
            <input type = "text" name = "query" />
            <input type = "submit" value = "Run MySQL Servlet" />
        </label>
    </p>
    </form>
    <hr>
    <%= request.getAttribute("queryResults") %>
</body>
</html>

数据库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 = out + "\t" + metaData.getColumnName(i) + "\n";
                }
                out = out + "\n";

                while (resultSet.next()){
                    for (int i = 1; i <= numberOfColumns; i++){
                        out = out + "\t" + resultSet.getObject(i) + "\n";
                    }
                    out = out + "\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!";
        }
        request.setAttribute("queryResults", out);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/dbServlet.jsp");
        dispatcher.forward(request,  response);
    }
}
4

1 回答 1

0

您不能只执行 out.println 并在 JSP 上打印。您需要将其放入您的响应对象并转发给 JSP。然后 JSP 可以将其拾取并打印。

于 2012-11-16T02:24:35.030 回答