Link to home
Start Free TrialLog in
Avatar of prabhu29
prabhu29

asked on

charts in servlets

I have a servlet PieData which fetches data from a database. The code is given below:
public class PieData extends HttpServlet {

      String     url   = "jdbc:odbc:testchart";  // URL specifying the JDBC connection to a Access database testchart.
      Connection con   = null;                    // Database connection object
      Statement  stmt  = null;                    // Statement String
      String     query;                           // Query String
      int        datacount;


      public void doGet(HttpServletRequest req, HttpServletResponse res)
               throws ServletException, IOException {

        // Set the output characterics for the return data
        res.setContentType("text/html");
            ServletOutputStream out = res.getOutputStream();

        // Establish the database connection
            try {
                  // Connect to testchart
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                  String url = "jdbc:odbc:testchart";
                  con = DriverManager.getConnection (url,"","");
                 stmt = con.createStatement();

                 out.println("  <!-- Chart Data -->  \n");

                  // Performing SQL query for Product X
                  query = "Select Value from Sales where Year=2004 and ProductName='Product X' ORDER BY Month";

                  ResultSet srs1 = stmt.executeQuery(query);

            // Write out the data for Series 1
            datacount = 1;
                  while (srs1.next()) {
                        out.println("data"+datacount+"series1: "+srs1.getString("Value")+"\n");
                        datacount++;
                  }

                  // Performing SQL query for Product Y
                  query = "Select Value from Sales where Year=2004 and ProductName='Product Y' ORDER BY Month";
                  ResultSet srs2 = stmt.executeQuery(query);

            // Write out the data for Series 2
            datacount = 1;
                  while (srs2.next()) {
                        out.println("data"+datacount+"series2: "+srs2.getString("Value")+"\n");
                        datacount++;
                  }
} // End try


        // Error handling
            catch(ClassNotFoundException e) {out.println("Could not load database driver: " + e.getMessage());}
            catch(SQLException e) {out.println("SQLException caught: " + e.getMessage());}

        // All finished so close the database connection
            finally {
                         try {if (con != null) con.close();}
                       catch (SQLException e) {}
        }




      } // End doGet
//-----------------------------------------------------------------------------
    public void doPost(HttpServletRequest request,HttpServletResponse response)
                    throws ServletException, IOException {doGet(request, response);}
//-----------------------------------------------------------------------------
} // End class

The servlet returns data fetched from the database. This servlet is an example taken from www.jpowered.com.
But to get the chart they have specified to Incorporate the Servlet IMG tag into your HTML page.

So I inserted this code in my servlet:
                                                  out.println("<html>");
                                      out.println("<title>Thank you!</title>");
                                      out.println("<H1>Example of Chart Image</H1>");
                                      String path = "http://localhost:8080/servlet-examples/images/";
                                      //out.println("<H2>Export chart to " + path + "</H2>");
                                      out.println("<img src=http://localhost:8080/servlet/PiechartServlet?");
                                                  out.println("config=http://localhost:8080/servlets-examples/piepropsS3D.txt&");
                                      out.println("data=http://localhost:8080/servlets-examples/piedataS2D.txt");
                                                  out.println("width=\"280\" height=\"160\" border=\"0\">");
                                                 out.println("</html>");

when I run the servlet I get the data but not the chart image.
Can anyone please guide me as I am new to java.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>http://localhost:8080/servlet/PiechartServlet?

doesn't look like a good image path. If it's a good one you should be able to enter it into your browser and get an image back
Have you setup the servlet mapping in web.xml?
Loading http://localhost:8080/servlet/PiechartServlet? in the browser will likely show you what the problem is.
Give that a go and see what the error is.
Incidentally, if you're interested in invoking that servlet from any machine other than the one it's being hosted on, you shouldn't use localhost as the host. Either use the 'real' hostname or use a path relative to your web root and omit the host altogether
Avatar of aozarov
aozarov

Did you deploy to your web container the PiechartServlet and its supported libraries as described in this link
http://www.jpowered.com/pie_chart/Documentation/implementingservlet.htm#step1
Avatar of prabhu29

ASKER

I have deployed all the required files as specified in the site. The problem is I am able to get the data but not the image. I think the problem is with the image folder path.So where should I place the images folder?
What image folder? the Chart servlet suppose to generate the chart images on the fly.
are you running the server on Unix platform?
which Java version are you using?
For Unix, You might need to use the PJA toolkit see: http://www.eteks.com/pja/en/
or use -Djava.awt.headless=true when you run your webcontainer see: http://forum.java.sun.com/thread.jspa?forumID=5&threadID=444149
Your servlet is generating the image.
Have you checked the server logs for what the cause of the error is?

What does the following display when you load it:

 http://localhost:8080/servlet/PiechartServlet?
>>Your servlet is generating the image.
How can you tell that from this thread?

>> http://localhost:8080/servlet/PiechartServlet?
this would definetly yield an error as it needs the other arguments:
                                out.print("config=http://localhost:8080/servlets-examples/piepropsS3D.txt&");
                                out.println("data=http://localhost:8080/servlets-examples/piedataS2D.txt");
so maybe try this instead:
http://localhost:8080/servlet/PiechartServlet?config=http://localhost:8080/servlets-examples/piepropsS3D.txt&data=http://localhost:8080/servlets-examples/piedataS2D.txt
> this would definetly yield an error as it needs the other arguments:

Exactly :)  And that error will help determine the problem and how to fix it.
But this is not how its servlet is calling. So he would be better off sending the same request and observe for errors.
When I tried http://localhost:8080/servlet/PiechartServlet?config=http://localhost:8080/servlets-examples/piepropsS3D.txt&data=http://localhost:8080/servlets-examples/piedataS2D.txt
I got this error message. I checked the entry in web.xml. Everything is fine.

HTTP Status 404 - /servlet/PiechartServlet

--------------------------------------------------------------------------------

type Status report

message /servlet/PiechartServlet

description The requested resource (/servlet/PiechartServlet) is not available.

Have you setup the servlet mapping in web.xml?
>Have you setup the servlet mapping in web.xml?
Yes, that is probably your problem (at least for this stage).
Your documentation mention nothing about the web.xml.
Old versions of Tomcat could invoke servlets without their defenitions/mappings in the web.xml (by just calling them with /servlet/ServletClassName).
This does not seem to work any more.

what you need to do is to edit your servlets-examples/WEB-INF/web.xml file and add this two entries:
// after servlet/SessionExample add this  
  <servlet>
        <servlet-name>PiechartServlet</servlet-name>
        <servlet-class>PiechartServlet</servlet-class>
    </servlet>

// and after servlet-mapping/SessionExample
<servlet-mapping>
        <servlet-name>PiechartServlet</servlet-name>
        <url-pattern>/servlet/PiechartServlet</url-pattern>
    </servlet-mapping>

Then also change the URL that used to invoked the servlet to include the context prefix (servlets-examples).
e.g
http://localhost:8080/servlets-example/servlet/PiechartServlet?config=http://localhost:8080/servlets-examples/piepropsS3D.txt&data=http://localhost:8080/servlets-examples/piedataS2D.txt
I checked the web.xml file and also changed the url as mentioned above, Now I just get a blank page.
And in my Tomcat I get these messages.

INFO: InvokerFilter(ApplicationFilterConfig[name=Path Mapped Filter, filterClass=filters.ExampleFilter]): 688 milliseconds
Error reading Properties from:http://localhost:8080/servlets-examples/piepropsS3D.txt
java.io.FileNotFoundException: http://localhost:8080/servlets-examples/piepropsS3D.txt
Error Reading Data from:http://localhost:8080/servlets-examples/piedataS2D.txt
java.io.FileNotFoundException: http://localhost:8080/servlets-examples/piedataS2D.txt
May 12, 2005 11:12:50 AM org.apache.catalina.core.ApplicationContext log
INFO: InvokerFilter(ApplicationFilterConfig[name=Path Mapped Filter, filterClass=filters.ExampleFilter]): 46 milliseconds

Please could anyone help me in fixing this?
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After I put the txt files in servlets-examples folder, Now the servlet shows a Pie Diagram but not the values from the database.
>> Diagram but not the values from the database.
Did you change the link your servlet generates for getting the database values to point to servlets-examples as well?