首页 > 代码库 > jsp01

jsp01

编写一个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