1. Preface
The original author of this article is Tony Wang, which involves the reading and writing of files and the automatic generation of jpg images. Using the jsp+servlet technology, jsp calls servlet to generate pictures.
2. The first file is as follows:
<%--
Author: Tony Wang
E-mail: lucky_tony@
Date: 2001-01-01
If you have any questions about the program, you can contact me. If there are any bugs in the program, please point them out! !
--%>
<%@ page contentType="text/html;charSet=gb2312"%>
<%
("Cache-Control","no-store");
("Expires",0);
%>
<%!
public String[] getQuestion(String s)
{
String[] strQ = new String[4];
String strTemp = null;
int i;
rf = null;
try {
rf = new (s,"r");
} catch(Exception e)
{
(e);
(0);
}
for(i=0;i<4;i++)
{
try {
strTemp = ();
} catch(Exception e) {
strTemp = "None Question";
}
if(strTemp==null)strTemp = "None Question";
strQ[i] = strTemp;
}
return strQ;
}
%>
<%
String s = null;
String[] question = new String[4];
s = ("");
question = getQuestion(s);
%>
<html>
<head>
<title></title>
<link href="" rel="StyleSheet" type="text/css"></link>
</head>
<body>
<table width="180" border="1" bordercolor="#999999">
<tr>
<td align=center>Ice Sail Investigation</td>
</tr>
<form name=frm method=post action=>
<tr>
<td>
<%
String ss = null;
for (int i=0;i<4;i++)
{
ss = "<input type=\"radio\" name=\"choice\" value=" + i+">"+
(char)("A"+i)+"、"+ question[i]+"<br>";
(ss);
}
%>
</td>
</tr>
<tr>
<td align=center><input type=submit value="I vote "></td>
</tr>
<tr>
<td align=center><img src="/vote/servlet/VoteImage" width=150 height=100></td>
</tr>
</form>
</table>
</body>
</html>
3. Write a document
<%--
Author: Tony Wang
E-mail: lucky_tony@
Date: 2001-01-01
If you have any questions about the program, please contact me.
In addition, if there is any bug in the program, please point it out! !
--%>
<%!
public int[] getNumber(String s)
{
int[] mCount = new int[4];
String strTemp = null;
int i;
rf = null;
try {
rf = new (s,"r");
} catch(Exception e)
{
(e);
(0);
}
for(i=0;i<4;i++)
{
try {
strTemp = ();
} catch(Exception e) {
strTemp = "0";
}
if(strTemp==null)strTemp = "0";
mCount[i] = new Integer(strTemp).intValue();
}
return mCount;
}
public void setNumber(String s,int[] x)
{
try {
pw = new (new .
FileOutputStream(s));
for (int i=0;i<4;i++){
(x[i]+"");
}
();
} catch(Exception e) {
("Write file error:"+());
}
}
%>
<%
String tmp = null;
int choice = -1;
int[] count = new int[4];
tmp = ("choice");
if (tmp==null){
} else {
choice = new Integer(tmp).intValue();
}
/////////////
String s = ("");
if(choice>=0){
count = getNumber(s);
count[choice]++;
setNumber(s,count);
}
("");
%>
4. The original servlet code::
/*
Author: Tony Wang
E-mail: lucky_tony@
Date: 2001-01-01
If you have any questions about the program, please contact me.
In addition, if there is any bug in the program, please point it out! !
*/
import .*;
import .*;
import .*;
import .*;
import .*;
import .*;
import .*;
import .*;
public class VoteImage extends HttpServlet
{
private String strFile = null;
private Color color[]={,,,
};
private int baseAng = 30;
public void doGet(HttpServletRequest request,HttpServletResponse
response)
throws ServletException,IOException
{
strFile = ("");
float[][] xy = new float[4][2];
xy = getNumAndPercent(strFile);
int[] ang = new int[4];
ang[0] = (int)(xy[0][1]*360);
ang[1] = (int)(xy[1][1]*360);
ang[2] = (int)(xy[2][1]*360);
ang[3] = 360-ang[0]-ang[1]-ang[2];
("Cache-Control","no-store");
("Expires",0);
("image/jpeg");
ServletOutputStream out=();
BufferedImage image=new BufferedImage(150,100,BufferedImage.
TYPE_INT_RGB);
Graphics2D g=(Graphics2D)();
(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
();
(0,0,150,100);
AffineTransform at = null;
Arc2D arc = null;
int fromAng = baseAng;
at = ((-20*)
/180,45,37);
(at);
int r =6;
int dx = (int)(r*((baseAng+ang[0])/2.0*java.
/180));
int dy = (int)(r*((baseAng+ang[0])/2.0*java.
/180));
arc = new (10+dx,24-dy,80,50,fromAng,ang[0],);
(color[0]);
(arc);
fromAng+=ang[0];
for (int i=1;i<4;i++)
{
(color[i]);
arc = new (10,24,80,50,fromAng,ang[i],);
(arc);
fromAng+=ang[i];
if (fromAng>360)
{
fromAng-=360;
}
}
at = (0,(),arc.
getCenterY());
(at);
for (int i=0;i<4;i++){
(color[i]);
(100,15*i+20,10,10);
((char)("A"+i)+"",120,15*i+20+8);
}
JPEGImageEncoder encoder=(out);
(image);
();
}
public void doPost(HttpServletRequest request,HttpServletResponse
response)
throws ServletException,IOException
{
doGet(request,response);
}
public synchronized float[][] getNumAndPercent(String sFileName)
{
float xx[][] = new float[4][2];
int totalNum = 0 ;
String strTemp = null;
int i = 0;
rf = null;
try
{
rf = new (sFileName,"r");
} catch(Exception e)
{
(e);
(0);
}
for (i=0;i<4;i++)
{
int m=0;
try {
strTemp = ();
} catch (Exception e){
strTemp = "0";
}
if (strTemp == null) strTemp = "0";
m = new Integer(strTemp).intValue();
xx[i][0]=m;
totalNum += m;
}
if (totalNum==0) totalNum=1;
for ( i=0;i<4;i++){
xx[i][1] = xx[i][0]/totalNum;
}
return xx;
}
}
5. Create and save files in the directory.
The votes and the number of votes are modified after the user votes.
In order to express gratitude to the original author, the contents of these two files remain unchanged, as follows:
:
Yes,I think so!
No,I dont think so!
Sorry,I dont know the answer!
:
12
9
5
9
6. Directory structure:
(1) The same directory as jsp file and txt file
(2).java file is in the servlet directory
7. Test:
http://[server:port]/dir/