码迷,mamicode.com
首页 > Web开发 > 详细

jsp01

时间:2016-11-20 21:24:20      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:访问   null   imageio   fill   i++   font   tin   python   random   

编写一个JSP页面,实现统计该页面被访问的次数

定义一个全局变量counter  用application对象的属性存储 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>统计页面访问次数</title>
</head>
<body>
	<%
		int counter = 1;
		if(application.getAttribute("counter") != null){
			counter = ((Integer) application.getAttribute("counter")).intValue()+1;
		}
		out.print("访问量:"+counter+"<br/>");
		application.setAttribute("counter", counter);
	 %>
</body>
</html>

编写绘制验证码图片的JSP

Random设置随机数 Graphics new一个图 设置背景色 字体色和输出数字

<body>
	<p>
	<%
		String s="";
		int intCount = 0;
		intCount = (new Random()).nextInt(9999);
		if(intCount < 1000){
			intCount += 1000;
		}
		s = intCount + "";
		
		session.setAttribute("validateCode",s);
		response.setContentType("image/jpeg");
		BufferedImage image = new BufferedImage(35,14,BufferedImage.TYPE_INT_RGB);
		Graphics gra = image.getGraphics();
		gra.setColor(Color.yellow);
		gra.fillRect(1, 1, 33, 12);
		gra.setColor(Color.black);
		gra.setFont(new Font("宋体",Font.PLAIN,12));
		char c;
		for(int i=0;i<4;i++){
			c = s.charAt(i);
			gra.drawString(c+"  ", i*7+4, 11);
		}
		gra.dispose();
		try{
			ImageIO.write(image,"jpeg",response.getOutputStream());
		}catch (Exception e){
		}
		out.clear();
		out = pageContext.pushBody();
	 %>
	 
</body>

  

  

jsp01

标签:访问   null   imageio   fill   i++   font   tin   python   random   

原文地址:http://www.cnblogs.com/bingo2-here/p/6083515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!