I already have an existing servlet writing out html code using the following commands to get a printwriter.
response.setContentType("text/html");
PrintWriter out = response.getWriter();
I am now trying to include trying to printout a JFreeChart for inclusion in this servlet using the code below:
OutputStream out2 = response.getOutputStream();
ChartUtilities.writeChartAsJPEG(out2, chart, 600, 300);
The problem seems to be that I can only ask for one type? if i use the outputstream first then the servlet code never gets written out ect.
How can i use these both in the same piece of code or how can i change the outputstream to make use of the printwriter.
Cheers delboy.
Outputstream and printwriter
-
- Posts: 8
- Joined: Mon Jul 28, 2003 5:07 pm
Hi,
I think that you need to have a servlet (or at least a request) which returns the HTML page and one which only return the image (this one is called from the image tag -> <img src="servlet/ChartServlet">).
In this servlet you have than to set the output type as JPEG (I don't remember the mime type (images/jpeg ?)):
I hope this can help you
Regards
Patrick
I think that you need to have a servlet (or at least a request) which returns the HTML page and one which only return the image (this one is called from the image tag -> <img src="servlet/ChartServlet">).
In this servlet you have than to set the output type as JPEG (I don't remember the mime type (images/jpeg ?)):
Code: Select all
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ChartUtilities.writeChartAsJPEG(out, chart, 600, 300);
Regards
Patrick
Does anybody have an example where they are printing out html code and include a dynamically created chart enclosed in <img> tags??
It seems to be impossible?
I dont want to have to create several charts as this will result in a greater performance hit.
But it also seems that I can only use the printwriter if i want HTML output and therefore I cant use the outputstream.
Somebody must have done this so must have an example.
Pretty please delboy.
It seems to be impossible?
I dont want to have to create several charts as this will result in a greater performance hit.
But it also seems that I can only use the printwriter if i want HTML output and therefore I cant use the outputstream.
Somebody must have done this so must have an example.
Pretty please delboy.
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
It is not impossible, there is a worked example in the JFreeChart Developer Guide. But since I need money to pay the mortgage, I can't give everything away for free, sorry...
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


Since I get paid by somebody else i suppose i can give the answer.
Plus it got to me trying things out for several days.
The reality of what people are saying is that you need to make a second call back into the servlet which isnt the best but...
What i have done is split my servlet into two functions or you can have two servlets as previously mentioned.
Then check the incoming parameters from the HttpServletRequest.
Depending on which parameter comes in then use this to output either html or image using either the printwriter or the outputstream.
Therefore as an example you can include an image as such:
//from html method hence using out
out.println("<img src=\"servlet/lbp.LBPServlet?CreateImage=test\">");
Your Servlet doget method will probably look like:
(obviously you might have different images and hence probably need to encapsulate into a better method rather than just looking for not null.)
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String imagetocreate = request.getParameter("CreateImage");
if(imagetocreate != null){
response.setContentType("image/jpeg");
JFreeChart chart = c.ChartExample();
OutputStream out2 = response.getOutputStream();
ChartUtilities.writeChartAsJPEG(out2, chart, 600, 300);
out2.close();
}
else{
your code to do html.....
Plus it got to me trying things out for several days.
The reality of what people are saying is that you need to make a second call back into the servlet which isnt the best but...
What i have done is split my servlet into two functions or you can have two servlets as previously mentioned.
Then check the incoming parameters from the HttpServletRequest.
Depending on which parameter comes in then use this to output either html or image using either the printwriter or the outputstream.
Therefore as an example you can include an image as such:
//from html method hence using out
out.println("<img src=\"servlet/lbp.LBPServlet?CreateImage=test\">");
Your Servlet doget method will probably look like:
(obviously you might have different images and hence probably need to encapsulate into a better method rather than just looking for not null.)
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
String imagetocreate = request.getParameter("CreateImage");
if(imagetocreate != null){
response.setContentType("image/jpeg");
JFreeChart chart = c.ChartExample();
OutputStream out2 = response.getOutputStream();
ChartUtilities.writeChartAsJPEG(out2, chart, 600, 300);
out2.close();
}
else{
your code to do html.....