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
相关文档:
1.基本概念的理解
绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:
C:xyz est.txt 代表了test.txt文件的绝对路径。http://www.sun.com/index.htm也代表了一个URL绝对路径。
相对路径:相对与某个基准目录的路径。包含Web的相对路径(HTML中的相对目录),例如:在
Ser ......
在JSP/Servlet 中主要有以下几个地方可以设置编码,pageEncoding="UTF-8"、contentType="text/html;charset=UTF -8"、request.setCharacterEncoding("UTF-8")和response.setCharacterEncoding ("UTF-8"),其中前两个只能用于JSP中,而后两个可以用于JSP和Servlet中。
1、pageEncoding="UTF-8"的作用是设置JSP编译成Servlet ......
JSP自定义标签学习(高级)
关键字: 自定义标签
BodyTag接口
接口BodyTag的实现类,它是负责操作主体内容(bodyContent).
定义方法:public void doInitBody();
public int doAtferBody();
提示,那些在doStartTag()方法之后被调用的方法依赖于doStartTag()方法返回值并且看在JSP页 ......
建立数据库:
我的数据库images
create table img (
id int primary key auto_increment,
name varchar(80),
pic longblob
)
要保证网站根目录 有个 images 文件夹
插入数据库 从本地文件夹
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.io.*" ......
简单示例
JSP页面中,我们经常有引用各种图片等资源,例如下面的jsp片段中
<div class="main">
<a href="http://www.sina.com.cn/abc.htm">
<strong>我的档案</strong>
&nbs ......