title: Displaying MySQL Query Results from Servlet to JSP
body: <p>I am trying to store the results of my query in a string, and print them to the bottom of my JSP page by passing that string to it. Right now, the JSP page displays fine initially, but nothing is happening when I click the button to post the command. Earlier when I accessed the servlet from an html page, and printed all my output to out using a PrintWriter, I got the results to display, but they would display on a separate page.</p>
  
  <p>1) Is it a good idea to store out in this way, or should I make it something different than a string?</p>
  
  <p>2) How do I get the results of the query to post to the JSP page?</p>
  
  <p>databaseServlet.java</p>
  
  <pre><code>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&lt;= numberOfColumns; i++){
                      out.concat(metaData.getColumnName(i));
                  }
                  out.concat("\n");
  
                  while (resultSet.next()){
                      for (int i = 1; i &lt;= 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);
      }
  }
  </code></pre>
  
  <p>dbServlet.jsp</p>
  
  <pre><code>&lt;?xml version = "1.0"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;!-- dbServlet.html --&gt;
  &lt;html xmlns = "http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
      &lt;title&gt;MySQL Servlet&lt;/title&gt;
      &lt;style type="text/css"&gt;
          body{background-color: green;}
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;This is the MySQL Servlet&lt;/h1&gt;
      &lt;form action = "/database/database" method = "post"&gt;
      &lt;p&gt;
          &lt;label&gt;Enter your query and click the button to invoke a MySQL Servlet
              &lt;textarea name = "query" cols="20" rows="5"&gt;&lt;/textarea&gt;
              &lt;input type = "submit" value = "Run MySQL Servlet" /&gt;
              &lt;input type = "reset" value = "Clear Command" /&gt;
          &lt;/label&gt;
      &lt;/p&gt;
      &lt;/form&gt;
      &lt;hr&gt;
      &lt;%= 
          request.getAttribute("queryResults");
      %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>