Outputstream and printwriter

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
delboy

Outputstream and printwriter

Post by delboy » Fri Nov 28, 2003 2:40 pm

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.

patrickherber
Posts: 8
Joined: Mon Jul 28, 2003 5:07 pm

Post by patrickherber » Sat Nov 29, 2003 6:22 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 ?)):

Code: Select all

response.setContentType("text/html"); 
PrintWriter out = response.getWriter(); 
ChartUtilities.writeChartAsJPEG(out, chart, 600, 300); 
I hope this can help you
Regards
Patrick

delboy

Post by delboy » Wed Dec 03, 2003 2:37 pm

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.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Wed Dec 03, 2003 2:42 pm

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

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

delboy

Post by delboy » Wed Dec 03, 2003 2:49 pm

if i use

<img src ="MyServlet">

how can i also pass parameters back to the servlet to determine which image i need to be created??

delboy

Post by delboy » Wed Dec 03, 2003 4:34 pm

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.....

Locked