计数器实现如下,请问如何将数字改成图片的,在线求救呀
<%!synchronized void countPeople()//串行化计数函数
{
ServletContext application = getServletContext();
Integer number = (Integer) application.getAttribute("Count");
if (number == null) //如果是第1个访问本站
{
number = new Integer(1);
application.setAttribute("Count", number);
} else {
number = new Integer(number.intValue() + 1);
application.setAttribute("Count", number);
}
}%>
<%
if (session.isNew())//如果是一个新的会话
countPeople();
Integer yourNumber = (Integer) application.getAttribute("Count");
%>
<P>
欢迎访问本站,您是第
<%=yourNumber%>
个访问用户。
<%!synchronized void countPeople()//串行化计数函数
{
ServletContext application = getServletContext();
Integer number = (Integer) application.getAttribute("Count");
if (number == null) //如果是第1个访问本站
{
number = new Integer(1);
application.setAttribute("Count", number);
} else {
number = new Integer(number.intValue() + 1);
application.setAttribute("Count", number);
}
}%>
<%
if (session.isNew())//如果是一个新的会话
countPeople();
Integer yourNumber = (Integer) application.getAttribute("Count");
%>
<P>
欢迎访问本站,您是第
<%=yourNumber%>
个访问用户。
解决方案 »
- jfreechart柱状图每个柱子颜色要求不一致的问题!全部是同一系列!
- 怎么样用java语句判断表已经存在
- Struts2真的好吗?讨论。
- 现在云计算这么火,我们该学点什么?欢迎大家讨论
- 我想问一下现在的JAVA做什么类型的程序比较多,是C/S或B/S架构的那种类型的程序比较多?
- 能够简单说明一下ehcache在什么位置起什么作用呢?
- dos运行JAVA 程序时,如果希望显式程序运行的时间,该如何设置??
- jdom如何解析属性里有冒号的XML?谢谢!
- tomcat的怪问题。。。。 (在线等)
- 100分请教JBuilder9中怎么修改ejb-jar.xml文件?
- 求个邮件设计思路!来者给分。
- 急!帮忙看看 struts1 中的action 无法链接上数据库
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
//添加内容
g.drawString("your number",13*i+6,16);具体参考下验证码吧!网上大把。不过更加推荐楼主做出0-9的图片,然后用代码控制显示。
我有0-9的图片,放在images包下的,可是我就是显示不出来
//创建内存图像
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
//创建随机类的实例
Random random = new Random();
// 设定图像背景色(因为是做背景,所以偏淡)
g.setColor(getRandColor(random, 200, 250));
g.fillRect(0, 0, width, height);
//备选字体
String[] fontTypes = {
"\u5b8b\u4f53", "\u65b0\u5b8b\u4f53", "\u9ed1\u4f53", "\u6977\u4f53", "\u96b6\u4e66"};
int fontTypesLength = fontTypes.length;
//在图片背景上增加噪点
g.setColor(getRandColor(random, 160, 200));
g.setFont(new Font("Times New Roman", Font.PLAIN, 14));
for (int i = 0; i < 6; i++) {
g.drawString("*********************************************", 0, 5 * (i + 2));
}
//取随机产生的认证码(6个汉字)
//保存生成的汉字字符串
String sRand = "";
for (int i = 0; i < 6; i++) {
int start = random.nextInt(length);
String rand = base.substring(start, start + 1);
sRand += rand;
//设置字体的颜色
g.setColor(getRandColor(random, 10, 150));
//设置字体
g.setFont(new Font(fontTypes[random.nextInt(fontTypesLength)], Font.BOLD, 18 + random.nextInt(6)));
//将此汉字画到图片上
g.drawString(rand, 24 * i + 10 + random.nextInt(8), 24);
}
//将认证码存入session
session.setAttribute("rand", sRand);
g.dispose();
//输出图象到页面
OutputStream os=response.getOutputStream();
ImageIO.write(image, "JPEG",os);
//以下为避免出现异常的处理
os.flush();
os.close();
os = null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
你看看吧 不过这是把随机数打印成图片的,只要把那个该了就行了
数字可以有多种显示方式,使用图片来显示只是一种方式而已。所以把数字从服务器发送给浏览器,在浏览器里使用js,控制图片显示出来就可以了。
先把你的数字拆分后放进一个数组或集合中
<%
for(int i =0;i < list.size();i++){
%>
<img src="<%=list.get(i)%>.jpg"/>
<%
}
%>
以数字89为例。
<%
int num = 89;
char [] charArray = String.valueOf(num).toCharArray();
StringBuffer html = new StringBuffer("<table><tr>");
for(int index = 0 ; index < charArray ; index++){
html.append("<td>"+charArray[index]+".jpg</td>");
}
html.append("</tr></table>");
out.write(html);
%>