js| vote
First, the preface
The original author of this article is Tony Wang, which covers the reading and writing of files and the automatic generation of JPG images. Using Jsp+servlet technology, the JSP calls the servlet to generate pictures.
Second, the first document INDEX.JSP as follows:
<%--
Author:tony Wang
E-mail:lucky_tony@163.net
Date:2001-01-01
If you have any questions about the program, you can contact me, another program if there are any bugs, trouble points out!!
--%>
<%@ page contenttype= "text/html;charset=gb2312"%>
<%
Response.setheader ("Cache-control", "No-store");
Response.setdateheader ("Expires", 0);
%>
<%!
Public string[] Getquestion (String s)
{
string[] Strq = new String[4];
String strtemp = null;
int i;
Java.io.RandomAccessFile RF = NULL;
try {
RF = new Java.io.RandomAccessFile (s, "R");
catch (Exception e)
{
System.out.println (e);
System.exit (0);
}
for (i=0;i<4;i++)
{
try {
strtemp = Rf.readline ();
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 = Request.getrealpath ("Question.txt");
Question = Getquestion (s);
%>
<title></title>
<link href= "Css.css" rel= "StyleSheet" type= "Text/css" ></link>
<body>
<table width= "180" border= "1" bordercolor= "#999999" >
<tr>
<TD align=center> Ice Sail survey </td>
</tr>
<form name=frm method=post action=write.jsp>
<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>";
OUT.PRINTLN (ss);
}
%>
</td>
</tr>
<tr>
<TD align=center><input type=submit value= "I cast one vote" ></td>
</tr>
<tr>
<TD align=center>
Height=100></td>
</tr>
</form>
</table>
</body>
Third, write the document write.jsp
<%--
Author:tony Wang
E-mail:lucky_tony@163.net
Date:2001-01-01
If you have any questions about the program, you can contact me,
Another program if there are any bugs, trouble point out!!
--%>
<%!
Public int[] GetNumber (String s)
{
int[] Mcount = new Int[4];
String strtemp = null;
int i;
Java.io.RandomAccessFile RF = NULL;
try {
RF = new Java.io.RandomAccessFile (s, "R");
catch (Exception e)
{
System.out.println (e);
System.exit (0);
}
for (i=0;i<4;i++)
{
try {
strtemp = Rf.readline ();
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 {
Java.io.PrintWriter pw = new Java.io.PrintWriter (new java.io.
FileOutputStream (s));
for (int i=0;i<4;i++) {
Pw.println (x[i]+ "");
}
Pw.close ();
catch (Exception e) {
System.out.println ("Write File Error:" +e.getmessage ());
}
}
%>
<%
String tmp = NULL;
int choice =-1;
Int[] count = new Int[4];
TMP = Request.getparameter ("choice");
if (tmp==null) {
} else {
Choice = new Integer (TMP). Intvalue ();
}
/////////////
String s = Request.getrealpath ("Count.txt");
if (choice>=0) {
Count = GetNumber (s);
count[choice]++;
Setnumber (S,count);
}
Response.sendredirect ("index.jsp");
%>
Four, the servlet original code: Voteimage.java:
/*
Author:tony Wang
E-mail:lucky_tony@163.net
Date:2001-01-01
If you have any questions about the program, you can contact me,
Another program if there are any bugs, trouble point out!!
*/
Import java.io.*;
Import java.util.*;
Import com.sun.image.codec.jpeg.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.awt.*;
Import java.awt.geom.*;
Import java.awt.image.*;
public class Voteimage extends HttpServlet
{
Private String strfile = null;
Private Color Color[]={color.red,color.black,color.orange,
Color.green};
private int baseang = 30;
public void doget (HttpServletRequest request,httpservletresponse
Response
Throws Servletexception,ioexception
{
strfile = Request.getrealpath ("Count.txt");
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];
Response.setheader ("Cache-control", "No-store");
Response.setdateheader ("Expires", 0);
Response.setcontenttype ("Image/jpeg");
Servletoutputstream Out=response.getoutputstream ();
BufferedImage image=new bufferedimage (150,100,bufferedimage.
TYPE_INT_RGB);
Graphics2D g= (graphics2d) image.getgraphics ();
G.setrenderinghint (Renderinghints.key_antialiasing,
RENDERINGHINTS.VALUE_ANTIALIAS_ON);
G.setcolor (Color.White);
G.fillrect (0,0,150,100);
AffineTransform at = null;
arc2d arc = null;
int Fromang = Baseang;
at = affinetransform.getrotateinstance (( -20*JAVA.LANG.MATH.PI)
/180,45,37);
G.settransform (at);
int r = 6;
int dx = (int) (R*java.lang.math.cos (baseang+ang[0])/2.0*java.
Lang. math.pi/180));
int dy = (int) (R*java.lang.math.sin (baseang+ang[0])/2.0*java.
Lang. math.pi/180));
Arc = new arc2d.double (10+dx,24-dy,80,50,fromang,ang[0],arc2d.pie);
G.setcolor (Color[0]);
G.fill (ARC);
FROMANG+=ANG[0];
for (int i=1;i<4;i++)
{
G.setcolor (Color[i]);
Arc = new arc2d.double (10,24,80,50,fromang,ang[i],arc2d.pie);
G.fill (ARC);
Fromang+=ang[i];
if (fromang>360)
{
fromang-=360;
}
}
at = affinetransform.getrotateinstance (0,arc.getcenterx (), Arc.
Getcentery ());
G.settransform (at);
for (int i=0;i<4;i++) {
G.setcolor (Color[i]);
G.fillrect (100,15*i+20,10,10);
g.DrawString ((char) (' A ' +i) + "", 120,15*i+20+8);
}
JPEGImageEncoder Encoder=jpegcodec.createjpegencoder (out);
Encoder.encode (image);
Out.close ();
}
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;
Java.io.RandomAccessFile RF = NULL;
Try
{
RF = new Java.io.RandomAccessFile (sFileName, "R");
catch (Exception e)
{
System.out.println (e);
System.exit (0);
}
for (i=0;i<4;i++)
{
int m=0;
try {
strtemp = Rf.readline ();
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;
}
}
The establishment of Question.txt and count.txt documents under the INDEX.JSP directory are used to save the cast
The question of the ticket and the number of votes, after the user has voted, modifies the value of the count.txt.
In order to thank the original author, the contents of these 2 documents do not change, as follows:
Question.txt:
Yes,i so!
No,i dont the so!
Sorry,i dont know the answer!
Count.txt:
12
9
5
9
VI. directory structure:
(1) JSP file and TXT file the same directory
(2). java files are in the servlet directory
Seven, test:
http://[server:port]/dir/index.jsp