大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> JSP专区 -> JAVA/JSP学习系列之十二

JAVA/JSP学习系列之十二

时间: 2021-07-31 作者:daque

一、媒介    正文原作家为tony wang ,该作品波及到文献的读写和jpg图片的机动天生。运用jsp+servlet的本领,jsp挪用servlet天生图片。 二、首文献index.jsp如次:<%-- author: tony wang e-mail: lucky_tony@163.net date: 2001-01-01 即使对步调有什么疑义,不妨和我接洽, 其余步调即使有什么bug,烦恼指出!! --%><%@ 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>冰帆观察</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="我 投 一 票"></td></tr><tr><td align=center><img src="/vote/servlet/voteimage" width=150 height=100></td></tr></form></table></body></html>三、写文献write.jsp<%--author: tony wange-mail: lucky_tony@163.netdate: 2001-01-01即使对步调有什么疑义,不妨和我接洽,其余步调即使有什么bug,烦恼指出!!--%><%!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");%>四、servlet原代码:voteimage.java :/*author: tony wange-mail: lucky_tony@163.netdate: 2001-01-01即使对步调有什么疑义,不妨和我接洽,其余步调即使有什么bug,烦恼指出!!*/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.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,httpservletresponseresponse)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;}}五、在index.jsp目次下创造question.txt和count.txt文献辨别用来生存开票的题目和开票的数目,用户开票后,就窜改count.txt的值。为了对原作家表白感动,这2个文献实质不变革,如次:question.txt:yes,i think so! no,i dont think so! sorry,i dont know the answer!count.txt:12959 六、目次构造:(1)jsp文献和txt文献同一个目次(2).java文献是servlet目次下七、尝试:http://[server:port]/dir/index.jsp

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.