简体   繁体   English

将字符串从servlet传递到.jsp

[英]Pass string from servlet to .jsp

I cannot get my out string to print to my .jsp file after the post method is run. 运行post方法后,我无法得到输出字符串以打印到我的.jsp文件。 Below are my .java and .jsp files. 以下是我的.java和.jsp文件。 I have removed the code there as by this time I know what I was trying was far from intuitive. 我已经删除了那里的代码,因为这一次我知道我的尝试远非直观。 I have simply left comments where I know the two respective lines of code need to go. 我只在需要知道两条代码的地方留下了注释。 I am simply trying to print the string "out" in my .jsp file after the servlet has executed the SQL command. 在servlet执行SQL命令之后,我只是试图在我的.jsp文件中打印字符串“ out”。

EDIT: I finally have all of my output going to the JSP page. 编辑:我终于将所有输出都转到JSP页面。 The only problem I'm having now is that my less-than-ideal method of storing my query results in the out string is leading to my output being one long line of results, instead of a nice spacing or table. 我现在遇到的唯一问题是,将查询结果存储在out字符串中的方法不理想,导致我的输出是一长串结果,而不是漂亮的空格或表格。 I know I would be better off looping through the results in a table in the jsp, but I can't seem to get that to work either. 我知道最好在jsp的表中循环遍历结果,但我似乎也无法使它正常工作。

dbServlet.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 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);
    }
}

You cannot just do an out.println and print on the JSP. 您不能只执行out.println并在JSP上打印。 YOu need to put it into your response object and forward to the JSP. 您需要将其放入您的响应对象中并转发给JSP。 The JSP can then pick it up and print. 然后,JSP可以将其拾取并打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM