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 3497746
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:24:29+00:00 2026-05-18T12:24:29+00:00

I have a JSP page that queries a MySQL database, I want to send

  • 0

I have a JSP page that queries a MySQL database, I want to send the Resultset object to an HTML page as a response object? I need the resultset object to populate a table and a chart.

  1. How do I cast the resultSet object into a javascript object?

  2. How do I send a resultSet object from JSP to HTML? (I mean the syntax)

I am using get xmlHTTPrequest to call the JSP page

  • 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-05-18T12:24:29+00:00Added an answer on May 18, 2026 at 12:24 pm

    Don’t use JSP. Use a Servlet which queries the DB, obtains a List with results and converts it to a JSON string which JS can seamlessly use.

    First create a javabean class which represents a single row of the DB table. E.g. Product.

    public class Product {
        private Long id;
        private String name;
        private String description;
        private BigDecimal price;
    
        // Add/generate c'tors, getters, setters and other boilerplate.
    }
    

    The create a DAO class which fires the query and maps the ResultSet to a List<Product>.

    public class ProductDAO {
    
        // ...
    
        public List<Product> find(String search) throws SQLException {
            Connection connection = null;
            PreparedStatement statement = null;
            ResultSet resultSet = null;
            List<Product> products = new ArrayList<Product>();
    
            try {
                connection = database.getConnection();
                statement = connection.prepareStatement(SQL_FIND);
                statement.setString(1, search);
                resultSet = statement.executeQuery();
                while (resultSet.next()) {
                    Product product = new Product();
                    product.setId(resultSet.getLong("id"));
                    product.setName(resultSet.getString("name"));
                    product.setDescription(resultSet.getString("description"));
                    product.setPrice(resultSet.getBigDecimal("price"));
                    products.add(product);
                }
            } finally {
                if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
                if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
                if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
            }
    
            return products;
        }
    }
    

    Then create a Servlet class which uses the DAO class to obtain the products and converts it to a JSON string with a little help of Google Gson.

    public class ProductServlet extends HttpServlet {
    
        // ...
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try {
                List<Product> products = productDAO.find(request.getParameter("search"));
                response.setContentType("application/json");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write(new Gson().toJson(products));
            } catch (SQLException e) {
                throw new ServletException("DB error", e);
            }
        }
    }
    

    Map this servlet in web.xml on an url-pattern of /products and call it in JavaScript as follows (I am using jQuery since it eliminates crossbrowsersensitive boilerplate so that you end up with 10 times less JavaScript code).

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>SO question 4407861</title>
            <script src="http://code.jquery.com/jquery-latest.min.js"></script>
            <script>
                $(document).ready(function() {
                    $('#searchform').submit(function() {
                        $.getJSON("products", $(this).serialize(), function(products) {
                            var table = $('#resulttable');
                            $.each(products, function(index, product) {
                                $('<tr>').appendTo(table)
                                    .append($('<td>').text(product.id))
                                    .append($('<td>').text(product.name))
                                    .append($('<td>').text(product.description))
                                    .append($('<td>').text(product.price));
                            });
                        });
                        return false;
                    });
                });
            </script>
        </head>
        <body>
            <form id="searchform">
                <input type="text" name="search">
                <input type="submit">
            </form>
            <table id="resulttable"></table>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have HTML code in the jsp page that I want it to be
I have a JSP page that renders a block of HTML. In normal usage,
Hello i have a JSP page that contain a form, i want to validate
I have a JSP page that handles file downloads. I set the response header
Right now I have a JSP page that allows to sort some items, when
I'm using Spring Web Flow (v. 1.0.5) and I have a JSP page that
I have a functional JSP page that accepts URL parameters and updates a table
I have an JSP page where I want to display an image from a
I have a JSP page which accepts SQL queries, performs them then returns the
I have jsp page that contains <span class=requiredFieldsMessageAsterix>*</span> I use a jsp include to

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.