12 of the Java/jsp Learning Series (JSPs generate JPEG images for voting)

Source: Internet
Author: User
I. Preface

The original author of this article is Tony Wang. This article involves file read/write and automatic jpg image generation. Using JSP + Servlet technology, JSP calls servlet to generate images.

Ii. index. jsp of the first file is as follows:

<% --

Author: Tony Wang

E-mail: lucky_tony@163.net

Date: 2001-001

If you have any questions about the program, contact me. If the program has any bugs, please point 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 );
%>

<HTML>
<Head>
<Title> </title>
<Link href = "css.css" rel = "stylesheet" type = "text/CSS"> </link>
</Head>

<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 vote"> </TD>
</Tr>
<Tr>
<TD align = center>

Height = 100> </TD>
</Tr>
</Form>
</Table>
</Body>
</Html>

3. Write File write. jsp

<% --
Author: Tony Wang
E-mail: lucky_tony@163.net
Date: 2001-001
If you have any questions about the program, contact me,
In addition, if the program has any bugs, please point it 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 ");
%>
4. Source servlet code: voteimage. Java:

/*
Author: Tony Wang
E-mail: lucky_tony@163.net
Date: 2001-001
If you have any questions about the program, contact me,
In addition, if the program has any bugs, please point it 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. 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 ();

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 (, 80, 50, fromang, Ang [I], arc2d. Pie );
G. Fill (ARC );
Fromang + = ang [I];
If (fromang> 360)
{
From g-= 360;
}
}

At = affinetransform. getrotateinstance (0, arc. getcenterx (), arc.

Getcentery ());
G. settransform ();

For (INT I = 0; I <4; I ++ ){
G. setcolor (color [I]);
G. fillrect (* I + 20, 10, 10 );
G. drawstring (char) ('A' + I) + "", * I + 20 + 8 );
}
Required imageencoder encoder = required codec. 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 question.txtand count.txt files are created in index.jsp respectively to save

After the user votes, the count.txt value is changed.

To thank the original author, the contents of these two files remain unchanged, as shown below:

Question.txt:

Yes, I think so!

No, I dont think so!

Sorry, I dont know the answer!

Count.txt:

12

9

5

9

Vi. directory structure:

(1) The JSP file and TXT file are in the same directory.

(2) the Java file is under the servlet directory.

VII. test:

Http: // [server: Port]/DIR/index. jsp

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.