验证码文件 p_w_picpath.jsp:
<%@ page contentType="p_w_picpath/JPEG"
import="java.awt.*,java.awt.p_w_picpath.*,java.util.*,javax.p_w_picpathio.*"
pageEncoding="GBK"%><%!Color getRandColor(intfc, intbc) {//给定范围获得随机颜色
Random random = newRandom();
if(fc > 255)
fc = 255;
if(bc > 255)
bc = 255;
intr = fc + random.nextInt(bc - fc);
intg = fc + random.nextInt(bc - fc);
intb = fc + random.nextInt(bc - fc);
returnnewColor(r, g, b);
}%><%
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 在内存中创建图象
intwidth = 60, height = 20;
BufferedImage p_w_picpath = newBufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = p_w_picpath.getGraphics();
//生成随机类
Random random = newRandom();
// 设定背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(newFont("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(inti = 0; i < 100; i++) {
intx = random.nextInt(width);
inty = random.nextInt(height);
intxl = random.nextInt(12);
intyl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取随机产生的认证码(4位数字)
String sRand = "";
for(inti = 0; i < 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 将认证码显示到图象中
g.setColor(newColor(20+ random.nextInt(110), 20+ random
.nextInt(110), 20+ random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand, 13* i + 6, 16);
}
// 将认证码存入SESSION
session.setAttribute("code", sRand);
// 图象生效
g.dispose();
// 输出图象到页面
ImageIO.write(p_w_picpath, "JPEG", response.getOutputStream());
%>
Login.jsp:
<%@ page contentType="text/html;charset=gb2312"%>
<%@page import="java.util.*"%>
<%@page language="java"%>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@ page import="java.util.ArrayList"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String mess = "<li>欢迎登录!</li>";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
<base href="<%=basePath%>">
<link href="css/style.css" rel="stylesheet">
<style type="text/css">
<!--
.STYLE1 {
color: #0000CC
}
.STYLE2 {
color: #0000CC;
font-weight: bold;
}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</head>
<body style="BACKGROUND-IMAGE: url(p_w_picpath/bg.jpg)">
<center>
<form method=post action="verify.jsp">
<table width="407" align="center">
<tr>
<td colspan="10" align="center" bordercolor="#CCCC99">
<h1>
<strong><span class="STYLE8"><%=mess%></span>
</strong>
</h1>
<br>
</td>
</tr>
<tr height="30">
<td height="29" colspan="8">
<h2>
<strong><span class="STYLE7">用户名:</span>
</strong>
</h2>
</td>
<td width="238">
<input type="text" name="userName" style="width: 200"
value="请输入用户名">
</td>
</tr>
<tr height="30">
<td height="28" colspan="8">
<h2>
<span class="STYLE7">密 码:</span>
</h2>
</td>
<td>
<input type="password" name="userPass" style="width: 200">
</td>
</tr>
<tr>
<td colspan="8">
</td>
<td valign="top">
<div align="left">
<img id="img" border=0 src="p_w_picpath.jsp" />
<span class="STYLE2"><a href=""
onClick="this.src='p_w_picpath.jsp'+new Date().getTime()">看不清?</a>
</span>
</div>
</td>
</tr>
<tr>
<td colspan="8">
<h2>
<span class="STYLE7">请输入认证码:</span>
</h2>
</td>
<td>
<input type=text name=rand maxlength=4 value=""
style="width: 200">
</td>
</tr>
<tr>
<td colspan="8"></td>
<td class="STYLE1">
<p>
<font face="黑体"><input type="submit" class="btn_bg"
value="登 录" style="height: 26px;">
</font>
<font face="黑体"><input type="reset" class="btn_bg"
value="重 置" style="height: 26px;">
</font>
</p>
</td>
</tr>
</table>
</form>
<%
request.setCharacterEncoding("gb2312");
String warning = request.getParameter("warning");
if (warning != null) {
//warning = new String(warning.getBytes("iso-8859-1"),"gb2312");
out
.println("<h3><font color=red>对不起,您输入的用户名或密码或验证码有误!<br></font><font color=red>请重新登录 或 </font><a href='index.jsp'>返回首页</a></h3>");
}
%>
</center>
</body>
</html>
转载于:https://blog.51cto.com/hopefu/1214882