I am a beginner in JSP, I am just wondering will it be possible to create a pie chart in jsp without installing new classes and stuff? If yes, how can I do it? Or else will it be possible if I retreieve the data from mysql and display it into a text box and create the pie chart? Thank you very much.

Dani AI

Generated

Short answer: yes — you can produce a pie chart from MySQL data while using JSP as the view layer, but it’s better to generate the chart with a dedicated library (server-side) or with a client-side charting library. That avoids reinventing drawing code in JSP and keeps presentation, business logic and DB access clean. asked the right question; several replies pointed toward library solutions — for example, and mentioned Java charting libraries — and ’s caution about not forcing drawing into JSP is worth heeding.

Server-side (image) approach — workflow:

  1. Query MySQL via JDBC/connection-pool in a servlet or DAO.
  2. Build a pie dataset from the ResultSet.
  3. Use a Java chart library to create a JFreeChart-style pie and write it to the servlet response as image/png.
  4. Embed the chart on the JSP with <img src="ChartServlet?...">.

Example (minimal servlet sketch):

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("image/png");
    // build dataset from DB results (Map<String,Number> data)
    DefaultPieDataset dataset = new DefaultPieDataset();
    dataset.setValue("A", 40);
    dataset.setValue("B", 60);
    JFreeChart chart = ChartFactory.createPieChart("Title", dataset, true, false, false);
    ChartUtilities.writeChartAsPNG(resp.getOutputStream(), chart, 600, 400);
}

Client-side (recommended for interactivity): expose a small JSON endpoint (or render JSON inline from the servlet/JSP) and use a JS charting library (Chart.js, Google Charts, etc.) to draw the pie in the browser. This reduces server CPU and gives hover/legend/resize for free.

Practical tips and cautions: don’t put JDBC code in JSP scriptlets — use servlets or a DAO. Put third-party JARs in WEB-INF/lib. Prefer a DataSource/connection pool over DriverManager. Set correct Content-Type and caching headers for generated images. If charts are expensive, cache images or results. This keeps the app maintainable and performs better than ad-hoc drawing in JSP.

Recommended Answers

All 7 Replies

yes, but you don't want to.
It's not what JSP is intended for, and why reinvent the wheel?

And no, we're not going to help you do things that are a Really Bad Idea (tm)(p)(c)(r)

Should I use php instead?thanks...

Yes you better with PHP in this case

yep, it's the language of choice for lazy loosers who don't want to learn how to do things properly.

Member Avatar for Member #268089

There is a page http://www.jfree.org/jfreechart/. What you can do to present charts i JSP is to create chart in code then save the chart as image. then present the image on the JSP page. This chart is not dynamic but it will hopefully help solving your problem.

AFAIK, JFreechart can also be used to create dynamic on the fly charts. Read the documentation and the tutorials floating around the web and it shouldn't be that difficult.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.