> Anyone please tell me whether I can use AWT in
> jsp?
I dont know if you can run it in jsp, but you can put your AWT stuff in a class and access that from a jsp.
> f so does the client need to install JVM in his
> browser for the page
No, JSP runs on the server not on the client....
If you are trying to use AWT in JSP to develop a user interface, then forget it!!
I have used AWT features to manipulate images, i.e. data processing, but all your jsp server can return to the client is HTML / javaScript and files.
From a JSP you can only use AWT for BACK END / Server Side processing.
If you want to develop a GUI / user interface you will need to use an Applet.
Thanks for ypur reply.
I'll tell u what my basic requirement is.
I want to develop graphs using JSP.
For that I've 2 options(atleast to the best of my knowledge)
(1) Using some third party tools
(2) Using AWT
But when trying to use tools, the options are limited.
So I Chose AWT.
I'm using Buffferd Image to print the graph on the client browser.
Now the problem is when I'm trying to display more components
by using JSP code(like adding a text box),
they are not getting displayed.
Please help me.
Cheers,
Viswanath Kolli.
You can return a buffered image to the user in jsp.
You CANNOT return AWT controls, buttons, input boxes etc.
You can still achieve what you want by putting controls in HTML, and displaying the output from the buffered image.
You need to
1) Have a web form where users enter data
2) Get your jsp to retrieve these values and pass them to a class using AWT
3) The class returns the buffered image to your jsp
4) Your jsp streams the Image to the browser along with an HTML form containgin your controls.
When considering third party tools did you look at
http://www.jfree.org/jfreechart/
This is open source, so you can take one of these graphs and customise it if you need to, there is no way I would start writing my own classes to do this.
Thanks for your reply.
I've seen Jfreechart and tried to display charts, it' s easy, but I think we can't change colours.Anyway, I admire if you can throw some light on whether some customization is possible.
Coming to our earlier discussion, i want you to check the sample code I'm using.
Here it is,
*****************************CODE STARTS**************************
<%@ page language="java" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="java.awt.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<%@ include file = "graphBG.jsp"%>
<%
//Data arrays
String datanames[] = {"Apples", "Oranges", "Peaches", "Lemons", "Grapefruit"};
int datavalues[] = {11, 62, 33, 102, 50};
//current y position
int y_pos = 0;
//y offset to cater for header space
int headerOffset = 50;
//inner padding to make sure bars never touch the outer border
int innerOffset = 20;
//height of bar, text and total
int barHeight = 10;
int textHeight = 20;
int displayHeight = barHeight + textHeight;
//Color used for the bars
Color barColor = new Color(153,19,19);
//Set the graph's outer width
int WIDTH = 300;
//Set the graph's outer height
int HEIGHT = (datavalues.length * displayHeight) + headerOffset + innerOffset;
//Width of the graphable area
int innerWIDTH = WIDTH - (innerOffset * 2);
//Calculate average
int average = 0;
for(int i=0; i<datavalues.length; i++)
{
average += datavalues;
}
average = average / datavalues.length;
//Calculate maximum
int maximum = 0;
for(int i=0; i<datavalues.length; i++)
{
if(datavalues > maximum)
{
maximum = datavalues;
}
}
///////////////////////////////////////////////////////////////////////////////////////
//Draw Graph Background and Header:
///////////////////////////////////////////////////////////////////////////////////////
response.setContentType("image/jpeg");
BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
graphBG gr = new graphBG();
gr.draw(biContext, WIDTH, HEIGHT, "Farm Produce", "Overall Average: " + average);
/////////////////////////////////////////////////////////////////////////////////////
//Draw data onto the graph:
/////////////////////////////////////////////////////////////////////////////////////
//Loop through & draw the bars
for(int i=0; i<datavalues.length; i++)
{
int currentValue = datavalues;
//Set y position for bar
y_pos = i * displayHeight + headerOffset;
//Set bar width
int barWidth = (innerWIDTH * currentValue) / maximum;
//Display the current value
String display = datanames + " (" + currentValue + ")";
biContext.setColor(Color.black);
biContext.drawString(display, 20, y_pos);
//Set dimensions of the bar
biContext.setColor(barColor);
Dimension bar = new Dimension(barWidth , barHeight);
Dimension barBorder = new Dimension(barWidth - 3 , barHeight - 3);
Rectangle barRect = new Rectangle(bar);
Rectangle barRectBorder = new Rectangle(barBorder);
//Draw bar and border:
barRect.setLocation(21, 5 + y_pos);
barRectBorder.setLocation(22, 6 + y_pos);
biContext.setColor(barColor);
biContext.fill(barRect);
biContext.setColor(Color.white);
biContext.draw(barRectBorder);
}
%>
<html>
<body>
<TABLE border="1" cellpadding="0" cellspacing="0" height="100%" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
<tr>
<td width='20%'>
<jsp:include page="a.jsp" flush="true" />
</td>
<td width='80%'>
<%
/////////////////////////////////////////////////////////////////////////////////////
//Display the graph
/////////////////////////////////////////////////////////////////////////////////////
//Encode:
OutputStream output = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(bi);
output.close();
%>
</td>
</tr>
</table>
</body>
</html>
*****************************CODE ENDS*****************************
The above code works fine as long as I don't include the code which is bold.
It complains that getWriter() has already been called.
Is there any solution for this?
(or) is it imposiible?
Can't I include any other JSP code in this page.
Any inputs will be very valuable to me.
Awaiting your reply.
Cheers,
Viswanath Kolli.
There is no code in bold, comment which lines you mean.
I am afraid it has been 2 years since I did anything like this so Im not going to be much help!
In principle there does not seem to be anything wrong with what you are doing, I think you will get it working in the end!
Post back which section causes the problem....
Thanks for ur reply
Chk out, i enclosed the code which is giving problem
in something like this
****************************INSERTING THIS CREATES PROBLEM******
*****************************CODE STARTS**************************
<%@ page language="java" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="java.awt.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<%@ include file = "graphBG.jsp"%>
<%
//Data arrays
String datanames[] = {"Apples", "Oranges", "Peaches", "Lemons", "Grapefruit"};
int datavalues[] = {11, 62, 33, 102, 50};
//current y position
int y_pos = 0;
//y offset to cater for header space
int headerOffset = 50;
//inner padding to make sure bars never touch the outer border
int innerOffset = 20;
//height of bar, text and total
int barHeight = 10;
int textHeight = 20;
int displayHeight = barHeight + textHeight;
//Color used for the bars
Color barColor = new Color(153,19,19);
//Set the graph's outer width
int WIDTH = 300;
//Set the graph's outer height
int HEIGHT = (datavalues.length * displayHeight) + headerOffset + innerOffset;
//Width of the graphable area
int innerWIDTH = WIDTH - (innerOffset * 2);
//Calculate average
int average = 0;
for(int i=0; i<datavalues.length; i++)
{
average += datavalues;
}
average = average / datavalues.length;
//Calculate maximum
int maximum = 0;
for(int i=0; i<datavalues.length; i++)
{
if(datavalues > maximum)
{
maximum = datavalues;
}
}
///////////////////////////////////////////////////////////////////////////////////////
//Draw Graph Background and Header:
///////////////////////////////////////////////////////////////////////////////////////
response.setContentType("image/jpeg");
BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
graphBG gr = new graphBG();
gr.draw(biContext, WIDTH, HEIGHT, "Farm Produce", "Overall Average: " + average);
/////////////////////////////////////////////////////////////////////////////////////
//Draw data onto the graph:
/////////////////////////////////////////////////////////////////////////////////////
//Loop through & draw the bars
for(int i=0; i<datavalues.length; i++)
{
int currentValue = datavalues;
//Set y position for bar
y_pos = i * displayHeight + headerOffset;
//Set bar width
int barWidth = (innerWIDTH * currentValue) / maximum;
//Display the current value
String display = datanames + " (" + currentValue + ")";
biContext.setColor(Color.black);
biContext.drawString(display, 20, y_pos);
//Set dimensions of the bar
biContext.setColor(barColor);
Dimension bar = new Dimension(barWidth , barHeight);
Dimension barBorder = new Dimension(barWidth - 3 , barHeight - 3);
Rectangle barRect = new Rectangle(bar);
Rectangle barRectBorder = new Rectangle(barBorder);
//Draw bar and border:
barRect.setLocation(21, 5 + y_pos);
barRectBorder.setLocation(22, 6 + y_pos);
biContext.setColor(barColor);
biContext.fill(barRect);
biContext.setColor(Color.white);
biContext.draw(barRectBorder);
}
%>
<html>
<body>
<TABLE border="1" cellpadding="0" cellspacing="0" height="100%" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
<tr>
******************************INSERTING THIS CREATES PROBLEM***********************
<td width='20%'>
<jsp:include page="a.jsp" flush="true" />
</td>
******************************INSERTING THIS CREATES PROBLEM******
<td width='80%'>
<%
/////////////////////////////////////////////////////////////////////////////////////
//Display the graph
/////////////////////////////////////////////////////////////////////////////////////
//Encode:
OutputStream output = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(bi);
output.close();
%>
</td>
</tr>
</table>
</body>
</html>
The problem is that, Suppose I'm using something like
<INPUT TYPE=text name='aaa' value="">
This doesn't appear on the screen.
I suppose this cud be becoz
encoder.encode(bi) in the above code already opens a
PrintWriter stream which automatically disables the default JSP Printstream object (out).
I'm not sure about it.
Inputs from ur side can be of gr8 help to me.
Cheers,
Viswanath Kolli.
Hello,
What I have done in the past is create the page that displays the image (getImage.jsp), then reference this page as the image source:
<img src="getImage.jsp?yourParameters=yourParams">
Doing this means that there is no included code / conflict between the pages.
To try this, put all your code for creating the graph in one page that does NOTHING else but display the chart.
Then in your main page try <img src="getImage.jsp"> to display the graph.
I used this to solve a similar problem where I wanted to retrieve images from a database, using an output stream. i found I could only do 1 image at a time, however, using this method I was able to call many images from the database.
The point is, you are not including the file, just getting the image output from that jsp.
I think in your case you would reverse what you are doing, putting <img src="getImage.jsp"> into a.jsp as opposed to including a.jsp in your main page.
Does this make sense?
Thank you very much.
It's working.
Right now I'm doing a project that display's trends of a company,
i mean their performance(revenue wise, resource utilization etc.)
I've never used AWT earlier.
Anyway thank u for ur help.
Cheers,
Viswanath Kolli
Just , what ever code i showed u, i included that jsp as an image source in another jsp(suppose a.jsp).
Now in a.jsp i can write all the jsp code without any problem.
here's the code,for ur reference
Source File : a.jsp
*************CODE STARTS**************
<%@ page language="java" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="java.awt.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.sun.image.codec.jpeg.*" %>
<html>
<body>
<TABLE border="1" cellpadding="0" cellspacing="0" height="100%" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
<tr>
<td width='15%'>
<input type=text name="aaa" value="">
</td>
<td width='85%' bgcolor="#90bade">
<img border="0" src="getImage.jsp">
</td>
</tr>
</table>
</body>
</html>
*************CODE ENDS**************
Glad it worked, I learned that a long time ago in another forum :-)
Its very interesting what you can do with AWT, I was using it to create thumbnail images with a watermark (website name) added.
Its things like that (the number of API's available) that make jsp so much more powerful than languages like PHP where you need to use plug ins etc. (though I am now learning PHP as the open source applications available in PHP are impossible to resist!)
Hey,
I got another way of doing it,
Instead of writing the image directly on to the browser, u can write it into a file and save in the format u like.
For your reference,
For JPEG images, the following line needs to be changed in the code i sent.
-OutputStream output = response.getOutputStream();
must be replaced with
-OutputStream output = new FileOutputStream(new File("images/chart.jpg"));
Cheers ,
Viswanath Kolli
I know you can write to file, but I think this is an inferior method, I prefer to stream the image direct to the browser with a 'zero footprint' on the file system. I think writing to a file is less efficient, and, as you would use the same file name for all users requests, it could cause problems as it updates. Streaming the image poses no risk of problems with concurrent access.......
Still fun to play with though :-)
Hi,
Is JfreeChart free for use or do we have buy it(i mean for corporate purposes)?.
It 's mentioned as free software on the site,but he explained the meaning of free as
"Free software is a matter of liberty, not price"
I have gone thru the site for details but I was unable to find where I can buy it (except the developer's guide).
Cheers
Viswanath Kolli
Hey Kumar,
here is the code for ur reference.
********************CODE STARTS************************
<%!
public class graphBG
{
Font font = new Font("ARIAL", Font.BOLD , 11);
Color headerColor = new Color(20,50,100);
public Graphics2D draw(Graphics2D biContext, int WIDTH, int HEIGHT, String headerText, String averageText)
{
//Grey color for the drop shadow
Color dropShadow = new Color(200,200,200);
//Graph and header dimensions
Dimension dim = new Dimension(WIDTH,HEIGHT);
Dimension dimGraph = new Dimension(WIDTH-21,HEIGHT-51);
Dimension dimHeader = new Dimension(WIDTH-21,20);
//Define Rectangles:
Rectangle area = new Rectangle(dim);
Rectangle graphArea = new Rectangle(dimGraph);
Rectangle headerArea = new Rectangle(dimHeader);
/////////////////////////////////////////////////////////////////////////////////////
//Set up the graph:
/////////////////////////////////////////////////////////////////////////////////////
//Set background color to white:
biContext.setColor(Color.white);
biContext.fill(area);
//Drop shadow for the graph area:
graphArea.setLocation(18, 42);//This is the drop shadow's location
biContext.setColor(dropShadow);
biContext.fill(graphArea);
//Fill the graph area (white):
graphArea.setLocation(11, 36);
biContext.setColor(Color.white);
biContext.fill(graphArea);
//Draw the graph border (black):
biContext.setColor(Color.black);
biContext.draw(graphArea);
//Header Drop Shadow:
headerArea.setLocation(18, 12);//This is the drop shadow's location
biContext.setColor(dropShadow);
biContext.fill(headerArea);
//Fill the header (blue):
headerArea.setLocation(11, 5);
biContext.setColor(headerColor);
biContext.fill(headerArea);
//Draw the header border (white):
biContext.setColor(Color.white);
biContext.draw(headerArea);
//Insert Header Text:
biContext.setFont(font);
biContext.setColor(Color.white);
biContext.drawString(headerText, 21, 18);
//Display the average
biContext.setColor(Color.white);
biContext.drawString(averageText, 150, 18);
return biContext;
}
}
%>
Bye.
Cheers,
Viswanath Kolli.
********************CODE ENDS**************************