Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:05:56+00:00 2026-06-14T10:05:56+00:00

I cannot get my out string to print to my .jsp file after the

  • 0

I cannot get my out string to print to my .jsp file after the post method is run. Below are my .java and .jsp files. 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.

EDIT:
I finally have all of my output going to the JSP page. 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. 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.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:05:57+00:00Added an answer on June 14, 2026 at 10:05 am

    You cannot just do an out.println and print on the JSP. YOu need to put it into your response object and forward to the JSP. The JSP can then pick it up and print.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning Java in Eclipse IDE. I cannot figure out how to get
The file is being created successfully, but I cannot get PrintWriter to print anything
I have the below code put in Eclipse, and I cannot get Eclipse to
In the tests below, I cannot get Console.WriteLine to really print when using yield
I'm trying to get started with Scala and cannot get out of the starting
I cannot figure out why I get this error during check-in. I checked in
I cannot figure out how to get rid of errors that should not be
I cannot figure out a way to get other channel types, e.g. sender channel,
When running the following code I get no output but I cannot work out
I've looked through tons of posts and cannot figure out why I can't get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.