Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

charts in servlets

Posted on 2005-05-06
20
Medium Priority
?
317 Views
Last Modified: 2013-11-24
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.
0
Comment
Question by:prabhu29
  • 8
  • 5
  • 5
  • +1
20 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13943522
>>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
0
 
LVL 92

Expert Comment

by:objects
ID: 13943791
Have you setup the servlet mapping in web.xml?
0
 
LVL 92

Expert Comment

by:objects
ID: 13943795
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13943908
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
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13950232
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
0
 

Author Comment

by:prabhu29
ID: 13965530
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?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13965566
What image folder? the Chart servlet suppose to generate the chart images on the fly.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13965584
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
0
 
LVL 92

Expert Comment

by:objects
ID: 13965587
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?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13965638
>>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
0
 
LVL 92

Expert Comment

by:objects
ID: 13965655
> 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.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13965660
But this is not how its servlet is calling. So he would be better off sending the same request and observe for errors.
0
 

Author Comment

by:prabhu29
ID: 13965795
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.

0
 
LVL 92

Expert Comment

by:objects
ID: 13965849
Have you setup the servlet mapping in web.xml?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13969712
>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
0
 

Author Comment

by:prabhu29
ID: 13983808
I checked the web.xml file and also changed the url as mentioned above, Now I just get a blank page.
0
 

Author Comment

by:prabhu29
ID: 13983893
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?
0
 
LVL 15

Accepted Solution

by:
aozarov earned 1500 total points
ID: 13985274
You should put piepropsS3D.txt and  piedataS2D.txt directly under your servlets-examples folder.
0
 

Author Comment

by:prabhu29
ID: 14024489
After I put the txt files in servlets-examples folder, Now the servlet shows a Pie Diagram but not the values from the database.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 14028150
>> 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?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question