将字符串从servlet传递到.jsp

时间:2012-11-15 23:35:24

标签: java string jsp post

运行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>

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 = 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);
    }
}

1 个答案:

答案 0 :(得分:0)

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