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/h tml");
ServletOutputStream out = res.getOutputStream();
// Establish the database connection
try {
// Connect to testchart
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
String url = "jdbc:odbc:testchart";
con = DriverManager.getConnectio n (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"+datacou nt+"series 1: "+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"+datacou nt+"series 2: "+srs2.getString("Value")+ "\n");
datacount++;
}
} // End try
// Error handling
catch(ClassNotFoundExcepti on 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,HttpServletRespons e 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.
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/h
ServletOutputStream out = res.getOutputStream();
// Establish the database connection
try {
// Connect to testchart
Class.forName("sun.jdbc.od
String url = "jdbc:odbc:testchart";
con = DriverManager.getConnectio
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"+datacou
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"+datacou
datacount++;
}
} // End try
// Error handling
catch(ClassNotFoundExcepti
catch(SQLException e) {out.println("SQLException
// All finished so close the database connection
finally {
try {if (con != null) con.close();}
catch (SQLException e) {}
}
} // End doGet
//------------------------
public void doPost(HttpServletRequest request,HttpServletRespons
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\"
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.
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.
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
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
http://www.jpowered.com/pie_chart/Documentation/implementingservlet.htm#step1
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
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?
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
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.
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.
ASKER
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.
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)
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>PiechartServ let</servl et-name>
<servlet-class>PiechartSer vlet</serv let-class>
</servlet>
// and after servlet-mapping/SessionExa mple
<servlet-mapping>
<servlet-name>PiechartServ let</servl et-name>
<url-pattern>/servlet/Piec hartServle t</url-pat tern>
</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
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/
// after servlet/SessionExample add this
<servlet>
<servlet-name>PiechartServ
<servlet-class>PiechartSer
</servlet>
// and after servlet-mapping/SessionExa
<servlet-mapping>
<servlet-name>PiechartServ
<url-pattern>/servlet/Piec
</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
ASKER
I checked the web.xml file and also changed the url as mentioned above, Now I just get a blank page.
ASKER
And in my Tomcat I get these messages.
INFO: InvokerFilter(ApplicationF ilterConfi g[name=Pat h Mapped Filter, filterClass=filters.Exampl eFilter]): 688 milliseconds
Error reading Properties from:http://localhost:8080/servlets-examples/piepropsS3D.txt
java.io.FileNotFoundExcept ion: http://localhost:8080/servlets-examples/piepropsS3D.txt
Error Reading Data from:http://localhost:8080/servlets-examples/piedataS2D.txt
java.io.FileNotFoundExcept ion: http://localhost:8080/servlets-examples/piedataS2D.txt
May 12, 2005 11:12:50 AM org.apache.catalina.core.A pplication Context log
INFO: InvokerFilter(ApplicationF ilterConfi g[name=Pat h Mapped Filter, filterClass=filters.Exampl eFilter]): 46 milliseconds
Please could anyone help me in fixing this?
INFO: InvokerFilter(ApplicationF
Error reading Properties from:http://localhost:8080/servlets-examples/piepropsS3D.txt
java.io.FileNotFoundExcept
Error Reading Data from:http://localhost:8080/servlets-examples/piedataS2D.txt
java.io.FileNotFoundExcept
May 12, 2005 11:12:50 AM org.apache.catalina.core.A
INFO: InvokerFilter(ApplicationF
Please could anyone help me in fixing this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
Did you change the link your servlet generates for getting the database values to point to servlets-examples as well?
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