JSP彩色验证码
产生验证码图片的文件-----image.jsp
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc){//给定范围获得随机颜色
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
// 在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
//生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
//画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
// 取随机产生的认证码(4位数字)
String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
// 将认证码显示到图象中
&nb
相关文档:
很多例如登录或者注销登录的地方需要使用到Cookie,今天学到在JSP中如何操作Cookie分享下
4.Cookie类常用的方法
1.setValue()/getValue() —>获取cookie对象的值。
2.getName()—>获取cookie对象的名称,循环的时候可以有选择的使用Cookie
3.setMaxAge()/getMaxAge()—>设置或获取cookie对象有 ......
一、概述
JSP中有一块重要的技术:自定义标签(Custom Tag),最近这几天在学习Struts的时候发现Struts中使用了很多自定义标签,如html、bean等。所以我就做了个简单的试验,学习一下这种技术。
首先介绍一下这种技术吧!
1.优 ......
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page errorPage="jsp3_error.jsp" %>
<%@ page import="java.util.*,java.io.*" %>
<html>
<head>
<title>
jsp综合应用
</title>
</head>
<jsp:useBean id="jsp3BeanId" scope="session" class="pan ......
一、安装JDK
首先下载JDK 5.0(JDK 5.0的下载页面为:http://java.sun.com/j2se/1.5.0/download.jsp); 然后运行JDK 5.0安装程序jdk-1_5_0_06-windows-i586-p.exe,安装过程中所有选项保持默认;最后配置JDK的环境变量:在“我的电脑”上点右键—>“属性”—>“高级& ......
1.调用JSP页面显示乱码
通过浏览器调用JSP页面,在客户端浏览器中所有的中文内容出现乱码。
solution:
首先确认本JSP在编辑器中保存时,使用的是GBK的编码格式,然后在JSP页面的开始部分添加 <%@ pageEncoding="GBK" %>就可以解决中文乱码问题。
2.调用Servlet页面显示乱码
通过浏览器调用Servlet,Servlet在浏 ......