|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
唉!都是钱闹的1.Swing和.net开发比较------从市场份额看.net开发主要占据大部分的中小型和中型的的桌面开发,原因是它封装了很多工具js|投票1、媒介
本文原作者为TonyWang,该文章触及到文件的读写和jpg图片的主动天生。使用jsp+servlet的手艺,jsp挪用servlet天生图片。
2、首文件index.jsp以下:
<%--
Author:TonyWang
E-mail:lucky_tony@163.net
Date:2001-01-01
假如对程序有甚么疑问,能够和我接洽,别的程序假如有甚么bug,贫苦指出!!
--%>
<%@pagecontentType="text/html;charSet=gb2312"%>
<%
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires",0);
%>
<%!
publicString[]getQuestion(Strings)
{
String[]strQ=newString[4];
StringstrTemp=null;
inti;
java.io.RandomAccessFilerf=null;
try{
rf=newjava.io.RandomAccessFile(s,"r");
}catch(Exceptione)
{
System.out.println(e);
System.exit(0);
}
for(i=0;i<4;i++)
{
try{
strTemp=rf.readLine();
}catch(Exceptione){
strTemp="NoneQuestion";
}
if(strTemp==null)strTemp="NoneQuestion";
strQ[i]=strTemp;
}
returnstrQ;
}
%>
<%
Strings=null;
String[]question=newString[4];
s=request.getRealPath("question.txt");
question=getQuestion(s);
%>
<html>
<head>
<title></title>
<linkhref="CSS.css"rel="StyleSheet"type="text/css"></link>
</head>
<body>
<tablewidth="180"border="1"bordercolor="#999999">
<tr>
<tdalign=center>冰帆查询拜访</td>
</tr>
<formname=frmmethod=postaction=write.jsp>
<tr>
<td>
<%
Stringss=null;
for(inti=0;i<4;i++)
{
ss="<inputtype="radio"name="choice"value="+i+">"+
(char)(A+i)+"、"+question[i]+"<br>";
out.println(ss);
}
%>
</td>
</tr>
<tr>
<tdalign=center><inputtype=submitvalue="我投一票"></td>
</tr>
<tr>
<tdalign=center><imgsrc="/vote/servlet/VoteImage"width=150
height=100></td>
</tr>
</form>
</table>
</body>
</html>
3、写文件write.jsp
<%--
Author:TonyWang
E-mail:lucky_tony@163.net
Date:2001-01-01
假如对程序有甚么疑问,能够和我接洽,
别的程序假如有甚么bug,贫苦指出!!
--%>
<%!
publicint[]getNumber(Strings)
{
int[]mCount=newint[4];
StringstrTemp=null;
inti;
java.io.RandomAccessFilerf=null;
try{
rf=newjava.io.RandomAccessFile(s,"r");
}catch(Exceptione)
{
System.out.println(e);
System.exit(0);
}
for(i=0;i<4;i++)
{
try{
strTemp=rf.readLine();
}catch(Exceptione){
strTemp="0";
}
if(strTemp==null)strTemp="0";
mCount[i]=newInteger(strTemp).intValue();
}
returnmCount;
}
publicvoidsetNumber(Strings,int[]x)
{
try{
java.io.PrintWriterpw=newjava.io.PrintWriter(newjava.io.
FileOutputStream(s));
for(inti=0;i<4;i++){
pw.println(x[i]+"");
}
pw.close();
}catch(Exceptione){
System.out.println("Writefileerror:"+e.getMessage());
}
}
%>
<%
Stringtmp=null;
intchoice=-1;
int[]count=newint[4];
tmp=request.getParameter("choice");
if(tmp==null){
}else{
choice=newInteger(tmp).intValue();
}
/////////////
Strings=request.getRealPath("count.txt");
if(choice>=0){
count=getNumber(s);
count[choice]++;
setNumber(s,count);
}
response.sendRedirect("index.jsp");
%>
4、servlet原代码:VoteImage.java:
/*
Author:TonyWang
E-mail:lucky_tony@163.net
Date:2001-01-01
假如对程序有甚么疑问,能够和我接洽,
别的程序假如有甚么bug,贫苦指出!!
*/
importjava.io.*;
importjava.util.*;
importcom.sun.image.codec.jpeg.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
importjava.awt.*;
importjava.awt.geom.*;
importjava.awt.image.*;
publicclassVoteImageextendsHttpServlet
{
privateStringstrFile=null;
privateColorcolor[]={Color.red,Color.black,Color.orange,
Color.green};
privateintbaseAng=30;
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponse
response)
throwsServletException,IOException
{
strFile=request.getRealPath("count.txt");
float[][]xy=newfloat[4][2];
xy=getNumAndPercent(strFile);
int[]ang=newint[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");
ServletOutputStreamout=response.getOutputStream();
BufferedImageimage=newBufferedImage(150,100,BufferedImage.
TYPE_INT_RGB);
Graphics2Dg=(Graphics2D)image.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.white);
g.fillRect(0,0,150,100);
AffineTransformat=null;
Arc2Darc=null;
intfromAng=baseAng;
at=AffineTransform.getRotateInstance((-20*java.lang.Math.PI)
/180,45,37);
g.setTransform(at);
intr=6;
intdx=(int)(r*java.lang.Math.cos((baseAng+ang[0])/2.0*java.
lang.Math.PI/180));
intdy=(int)(r*java.lang.Math.sin((baseAng+ang[0])/2.0*java.
lang.Math.PI/180));
arc=newArc2D.Double(10+dx,24-dy,80,50,fromAng,ang[0],Arc2D.PIE);
g.setColor(color[0]);
g.fill(arc);
fromAng+=ang[0];
for(inti=1;i<4;i++)
{
g.setColor(color[i]);
arc=newArc2D.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(inti=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);
}
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponse
response)
throwsServletException,IOException
{
doGet(request,response);
}
publicsynchronizedfloat[][]getNumAndPercent(StringsFileName)
{
floatxx[][]=newfloat[4][2];
inttotalNum=0;
StringstrTemp=null;
inti=0;
java.io.RandomAccessFilerf=null;
try
{
rf=newjava.io.RandomAccessFile(sFileName,"r");
}catch(Exceptione)
{
System.out.println(e);
System.exit(0);
}
for(i=0;i<4;i++)
{
intm=0;
try{
strTemp=rf.readLine();
}catch(Exceptione){
strTemp="0";
}
if(strTemp==null)strTemp="0";
m=newInteger(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;
}
returnxx;
}
}
5、在index.jsp目次下创建question.txt和count.txt文件分离用来保留投
票的成绩和投票的数目,用户投票后,就修正count.txt的值。
为了对原作者暗示感激,这2个文件内容稳定化,以下:
question.txt:
Yes,Ithinkso!
No,Idontthinkso!
Sorry,Idontknowtheanswer!
count.txt:
12
9
5
9
6、目次布局:
(1)jsp文件和txt文件统一个目次
(2).java文件是servlet目次下
Java到底会发战成什么样,让我们拭目以待吧,我始终坚信着java会更好。以上都是俺个人看法,欢迎大家一起交流. |
|