博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jsp验证码
阅读量:5847 次
发布时间:2019-06-18

本文共 6581 字,大约阅读时间需要 21 分钟。

Servlet部分:
 
package
servlet;
 
import
java.awt.Color;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.image.BufferedImage;
import
java.io.IOException;
import
java.io.OutputStream;
import
java.util.Random;
 
import
javax.imageio.ImageIO;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
 
public
class
LoginServlet
extends
HttpServlet {
     
private
static
final
long
serialVersionUID
= 1L;
 
     
// 产生随即的字体
     
private
Font getFont() {
           Random
random
=
new
Random();
           Font
font
[] =
new
Font[5];
           
font
[0] =
new
Font(
"Ravie"
, Font.
PLAIN
, 24);
           
font
[1] =
new
Font(
"Antique Olive Compact"
, Font.
PLAIN
, 24);
           
font
[2] =
new
Font(
"Forte"
, Font.
PLAIN
, 24);
           
font
[3] =
new
Font(
"Wide Latin"
, Font.
PLAIN
, 24);
           
font
[4] =
new
Font(
"Gill Sans Ultra Bold"
, Font.
PLAIN
, 24);
           
return
font
[
random
.nextInt(5)];
     }
 
     
@Override
     
protected
void
doGet(HttpServletRequest
request
, HttpServletResponse
response
)
                
throws
ServletException, IOException {
           doPost(
request
,
response
);
     }
 
     
@Override
     
protected
void
doPost(HttpServletRequest
request
, HttpServletResponse
response
)
                
throws
ServletException, IOException {
           
// 设置响应头 Content-type类型
           
response
.setContentType(
"image/jpeg"
);
           
// 以下三句是用于设置页面不缓存
           
response
.setHeader(
"Pragma"
,
"No-cache"
);
           
response
.setHeader(
"Cache-Control"
,
"No-cache"
);
           
response
.setDateHeader(
"Expires"
, 0);
 
           OutputStream
os
=
response
.getOutputStream();
           
int
width
= 83,
height
= 30;
           
// 建立指定宽、高和BufferedImage对象
           BufferedImage
image
=
new
BufferedImage(
width
,
height
, BufferedImage.
TYPE_INT_RGB
);
 
           Graphics
g
=
image
.getGraphics();
// 该画笔画在image上
           Color
c
=
g
.getColor();
// 保存当前画笔的颜色,用完画笔后要回复现场
           
g
.fillRect(0, 0,
width
,
height
);
 
           
char
[]
ch
=
"abcdefghjkmnpqrstuvwxyz23456789"
.toCharArray();
// 随即产生的字符串
                                                                                                
// 不包括 i
                                                                                                
// l(小写L)
                                                                                                
// o(小写O)
                                                                                                
// 1(数字1)0(数字0)
           
int
length
=
ch
.
length
;
// 随即字符串的长度
           String
sRand
=
""
;
// 保存随即产生的字符串
           Random
random
=
new
Random();
           
for
(
int
i
= 0;
i
< 4;
i
++) {
                
// 设置字体
                
g
.setFont(getFont());
                
// 随即生成0-9的数字
                String
rand
=
new
Character(
ch
[
random
.nextInt(
length
)]).toString();
                
sRand
+=
rand
;
                
// 设置随机颜色
                
g
.setColor(
new
Color(
random
.nextInt(255),
random
.nextInt(255),
random
.nextInt(255)));
                
g
.drawString(
rand
, 20 *
i
+ 6, 25);
           }
           
// 产生随即干扰点
           
for
(
int
i
= 0;
i
< 20;
i
++) {
                
int
x1
=
random
.nextInt(
width
);
                
int
y1
=
random
.nextInt(
height
);
                
g
.drawOval(
x1
,
y1
, 2, 2);
           }
           
g
.setColor(
c
);
// 将画笔的颜色再设置回去
           
g
.dispose();
 
           
// 将验证码记录到session
           
request
.getSession().setAttribute(
"safecode"
,
sRand
);
           
// 输出图像到页面
           ImageIO. write(
image
,
"JPEG"
,
os
);
 
     }
 
}
 
前端Jsp:
 
<%
 
    String action = request.getParameter(
"action"
); 
    String safecodeText = request.getParameter(
"safecodeTest"
); 
   
if
(
"action"
.equals(action)){ 
        String safecode = (String)session.getAttribute(
"safecode"
); 
       
if
(safecode.equals(safecodeText)){ 
            out.print(
"验证码正确!"
); 
        }
else
            out.print(
"验证码错误!请重新输入!"
); 
        } 
    } 
%>
 
 
<
head
>
 
   
<
title
>
验证码测试
</
title
>
 
 
</
head
>
 
 
<
body
>
 
   
<
form
action
=
"index.jsp"
method
=
"post"
>
 
   
<
input
type
=
"hidden"
name
=
"action"
value
=
"action"
/>
 
       
<
img
alt
=
"验证码"
src
=
"login"
>
 
       
<
input
type
=
"text"
name
=
"safecodeTest"
>
 
       
<
input
type
=
"submit"
value
=
"go"
>
 
   
</
form
>
 
 
</
body
>
 
</
html
>
 
Servlet部分:
 
package
servlet;
 
import
java.awt.Color;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.image.BufferedImage;
import
java.io.IOException;
import
java.io.OutputStream;
import
java.util.Random;
 
import
javax.imageio.ImageIO;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
 
public
class
LoginServlet
extends
HttpServlet {
     
private
static
final
long
serialVersionUID
= 1L;
 
     
// 产生随即的字体
     
private
Font getFont() {
           Random
random
=
new
Random();
           Font
font
[] =
new
Font[5];
           
font
[0] =
new
Font(
"Ravie"
, Font.
PLAIN
, 24);
           
font
[1] =
new
Font(
"Antique Olive Compact"
, Font.
PLAIN
, 24);
           
font
[2] =
new
Font(
"Forte"
, Font.
PLAIN
, 24);
           
font
[3] =
new
Font(
"Wide Latin"
, Font.
PLAIN
, 24);
           
font
[4] =
new
Font(
"Gill Sans Ultra Bold"
, Font.
PLAIN
, 24);
           
return
font
[
random
.nextInt(5)];
     }
 
     
@Override
     
protected
void
doGet(HttpServletRequest
request
, HttpServletResponse
response
)
                
throws
ServletException, IOException {
           doPost(
request
,
response
);
     }
 
     
@Override
     
protected
void
doPost(HttpServletRequest
request
, HttpServletResponse
response
)
                
throws
ServletException, IOException {
           
// 设置响应头 Content-type类型
           
response
.setContentType(
"image/jpeg"
);
           
// 以下三句是用于设置页面不缓存
           
response
.setHeader(
"Pragma"
,
"No-cache"
);
           
response
.setHeader(
"Cache-Control"
,
"No-cache"
);
           
response
.setDateHeader(
"Expires"
, 0);
 
           OutputStream
os
=
response
.getOutputStream();
           
int
width
= 83,
height
= 30;
           
// 建立指定宽、高和BufferedImage对象
           BufferedImage
image
=
new
BufferedImage(
width
,
height
, BufferedImage.
TYPE_INT_RGB
);
 
           Graphics
g
=
image
.getGraphics();
// 该画笔画在image上
           Color
c
=
g
.getColor();
// 保存当前画笔的颜色,用完画笔后要回复现场
           
g
.fillRect(0, 0,
width
,
height
);
 
           
char
[]
ch
=
"abcdefghjkmnpqrstuvwxyz23456789"
.toCharArray();
// 随即产生的字符串
                                                                                                
// 不包括 i
                                                                                                
// l(小写L)
                                                                                                
// o(小写O)
                                                                                                
// 1(数字1)0(数字0)
           
int
length
=
ch
.
length
;
// 随即字符串的长度
           String
sRand
=
""
;
// 保存随即产生的字符串
           Random
random
=
new
Random();
           
for
(
int
i
= 0;
i
< 4;
i
++) {
                
// 设置字体
                
g
.setFont(getFont());
                
// 随即生成0-9的数字
                String
rand
=
new
Character(
ch
[
random
.nextInt(
length
)]).toString();
                
sRand
+=
rand
;
                
// 设置随机颜色
                
g
.setColor(
new
Color(
random
.nextInt(255),
random
.nextInt(255),
random
.nextInt(255)));
                
g
.drawString(
rand
, 20 *
i
+ 6, 25);
           }
           
// 产生随即干扰点
           
for
(
int
i
= 0;
i
< 20;
i
++) {
                
int
x1
=
random
.nextInt(
width
);
                
int
y1
=
random
.nextInt(
height
);
                
g
.drawOval(
x1
,
y1
, 2, 2);
           }
           
g
.setColor(
c
);
// 将画笔的颜色再设置回去
           
g
.dispose();
 
           
// 将验证码记录到session
           
request
.getSession().setAttribute(
"safecode"
,
sRand
);
           
// 输出图像到页面
           ImageIO. write(
image
,
"JPEG"
,
os
);
 
     }
 
}
 
前端Jsp:
 
<%
 
    String action = request.getParameter(
"action"
); 
    String safecodeText = request.getParameter(
"safecodeTest"
); 
   
if
(
"action"
.equals(action)){ 
        String safecode = (String)session.getAttribute(
"safecode"
); 
       
if
(safecode.equals(safecodeText)){ 
            out.print(
"验证码正确!"
); 
        }
else
            out.print(
"验证码错误!请重新输入!"
); 
        } 
    } 
%>
 
 
<
head
>
 
   
<
title
>
验证码测试
</
title
>
 
 
</
head
>
 
 
<
body
>
 
   
<
form
action
=
"index.jsp"
method
=
"post"
>
 
   
<
input
type
=
"hidden"
name
=
"action"
value
=
"action"
/>
 
       
<
img
alt
=
"验证码"
src
=
"login"
>
 
       
<
input
type
=
"text"
name
=
"safecodeTest"
>
 
       
<
input
type
=
"submit"
value
=
"go"
>
 
   
</
form
>
 
 
</
body
>
 
</
html
>
 

转载于:https://www.cnblogs.com/phantomfjh/p/4959496.html

你可能感兴趣的文章
国际化支持
查看>>
Windows Server 2012 托管服务账号
查看>>
mysql 服务器服务重启自动关闭,且拷贝mysql系统文件I/O设备错误(无法拷走)
查看>>
java.lang.OutOfMemoryError: PermGen space及其解决方法
查看>>
intelij idea插件下载失败
查看>>
jquery判断对象是否存在
查看>>
puppet故障分析Host is missing hostname and/or domain
查看>>
iOS第三方开源类库 -- 视图切换 HMGLTransitions
查看>>
Linux常用命令整合及解释(1)
查看>>
linux 监控进程是否存在
查看>>
python基本数据类型
查看>>
值得注意的软件更新服务
查看>>
Nginx配置error_page 404 500等自定义的错误页面
查看>>
属性与所引器
查看>>
Linux C的select函数的使用
查看>>
swoft-基于swoole协程2.x的高性能PHP微服务框架
查看>>
db2 v10.1 Express-C 在ubuntu-12.04-desktop-amd64上安装、启动、关闭及卸载(二)
查看>>
【CentOS 7笔记39】,监控系统状态2#171128
查看>>
由一个数据库恢复案例而想到的
查看>>
MS SQL SERVER批量恢复BAK文件
查看>>