选择显示字体大小

java/jsp学习系列之十二

一、前言

   本文原作者为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 wang
e-mail: lucky_tony@163.net
date: 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 wang
e-mail: lucky_tony@163.net
date: 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,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);
aff.netransform at = null;
arc2d arc = null;
int fromang = baseang;

at = aff.netransform.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 = aff.netransform.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;
}
}

五、在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:

12

9

5

9

六、目录结构:

(1)jsp文件和txt文件同一个目录

(2).java文件是servlet目录下

七、测试

http://[server:port]/dir/index.jsp
  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons