验证码文件 p_w_picpath.jsp:

  1. <%@ page contentType="p_w_picpath/JPEG"

  2. import="java.awt.*,java.awt.p_w_picpath.*,java.util.*,javax.p_w_picpathio.*"

  3. pageEncoding="GBK"%><%!Color getRandColor(intfc, intbc) {//给定范围获得随机颜色

  4. Random random = newRandom();

  5. if(fc > 255)

  6. fc = 255;

  7. if(bc > 255)

  8. bc = 255;

  9. intr = fc + random.nextInt(bc - fc);

  10. intg = fc + random.nextInt(bc - fc);

  11. intb = fc + random.nextInt(bc - fc);

  12. returnnewColor(r, g, b);

  13. }%><%

  14. //设置页面不缓存

  15. response.setHeader("Pragma", "No-cache");

  16. response.setHeader("Cache-Control", "no-cache");

  17. response.setDateHeader("Expires", 0);

  18. // 在内存中创建图象

  19. intwidth = 60, height = 20;

  20. BufferedImage p_w_picpath = newBufferedImage(width, height,

  21. BufferedImage.TYPE_INT_RGB);

  22. // 获取图形上下文

  23. Graphics g = p_w_picpath.getGraphics();

  24. //生成随机类

  25. Random random = newRandom();

  26. // 设定背景色

  27. g.setColor(getRandColor(200, 250));

  28. g.fillRect(0, 0, width, height);

  29. //设定字体

  30. g.setFont(newFont("Times New Roman", Font.PLAIN, 18));

  31. //画边框

  32. //g.setColor(new Color());

  33. //g.drawRect(0,0,width-1,height-1);

  34. // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到

  35. g.setColor(getRandColor(160, 200));

  36. for(inti = 0; i < 100; i++) {

  37. intx = random.nextInt(width);

  38. inty = random.nextInt(height);

  39. intxl = random.nextInt(12);

  40. intyl = random.nextInt(12);

  41. g.drawLine(x, y, x + xl, y + yl);

  42. }

  43. // 取随机产生的认证码(4位数字)

  44. String sRand = "";

  45. for(inti = 0; i < 4; i++) {

  46. String rand = String.valueOf(random.nextInt(10));

  47. sRand += rand;

  48. // 将认证码显示到图象中

  49. g.setColor(newColor(20+ random.nextInt(110), 20+ random

  50. .nextInt(110), 20+ random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成

  51. g.drawString(rand, 13* i + 6, 16);

  52. }

  53. // 将认证码存入SESSION

  54. session.setAttribute("code", sRand);

  55. // 图象生效

  56. g.dispose();

  57. // 输出图象到页面

  58. ImageIO.write(p_w_picpath, "JPEG", response.getOutputStream());

  59. %>  

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">密&nbsp;&nbsp;码:</span>
</h2>
</td>
<td>
<input type="password" name="userPass" style="width: 200">
</td>
</tr>


<tr>
<td colspan="8">
&nbsp;
</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>