td> tr> form> table> body> html> three, write file write.jsp <% - Author: Tony wange-mail: lucky_tony@163.netdate: 2001-01-01 If you have any questions about the program, you can contact me, if you have any bugs, trouble points out! ! -%> <%! public int [] getnumber (string s) {int [] mcount = new int [4]; string straTemp = 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 {straTemp = rf.readline ();} catch (exception e) {straTemp = "0";} if (strTemp == null) straTemp = "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 choice = -1; int = -1; int = -1; int = new int [4]; tmp = request.getParameter ("choice"); if (TMP == NULL) {} else {choice = new integer (tmp) .intValue ();} / string s = request.getRealPath ("count.txt"); if (choxt = 0) {count = GetNumber (s) Count [choice] ; setNumber (s, count);} response.sendredirect ("index.jsp");%> four, servlet original code: vote Image.java: / * Author: tony wange-mail: lucky_tony @
163.Netdate: 2001-01-01 If you have any questions about the program, you can contact me, if there is any bug, 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, HttpServletResponseresponse) 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.GETGRA phics (); 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.GtrotateInstance ((- 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.GtrotteInstance (0, arc.getCenterx (), arc.getCenter (); g.setTransform At); for (int i = 0; i <4; i ) {g.setcolor (color [i]); g.fillRect (100, 15 * i 20, 10, 10); g.drawstring ((charwstring) ) ('A' i) "", 120, 15 * i 20 8); JPEGIMAGEENCODER ENCODER = JPEGCODEC.CREATEJPEGENCODER (OUT); Encoder.Encode (Image); out.close ();} public void doPost (HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException {doGet (request, response);} public synchronized float [] [] getNumAndPercent (String sFileName) {float xx [] [] = new float [4] [2]; int Totalnum = 0; string straTemp = 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 {straTemp = 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;}} 5, establish questions.txt and count.txt files to save voting in index.jsp directory And the number of votes, after the user votes, modify the value of count.txt.
转载请注明原文地址:https://www.9cbs.com/read-19783.html