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
相关文档:
JSP和Servlet中的绝对路径和相对路径问题困扰了我好几天,经过努力之后将其部分心得和大家共享。
前提:假设你的Http地址为http://192.168.0.1/你的web应用为webapp,那么你的web应用URL为http://192.168.0.1/webapp/
web应用的目录结构:
webapp/
web-inf/
......
在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 ......
很多例如登录或者注销登录的地方需要使用到Cookie,今天学到在JSP中如何操作Cookie分享下
4.Cookie类常用的方法
1.setValue()/getValue() —>获取cookie对象的值。
2.getName()—>获取cookie对象的名称,循环的时候可以有选择的使用Cookie
3.setMaxAge()/getMaxAge()—>设置或获取cookie对象有 ......
jsp中的out对象是JspWriter类型的.而JspWriter继承了java.io.Writer . write方法是在在父类Writer中定义的,print方法是在子类JspWriter中定义的.重载的print方法可以将各种类型的数据转换成字符串的形式输出.而重载的write方法只能输出字符/字符数组/字符串等与字符相关的数据.而且如果使用这两种方法输出值为null的 ......
WEB开发中经常用到上传图,在未上传之前要显示所选择的图片
可以用简单的JS 实现:
<html>
<head>
<SCRIPT language=JavaScript>
function showimg(){
var imgpeoper=form1.imgs.value;
form1.img.src=imgpeoper;
......