JSP页面生成验证码功能
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.net.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%
if(request.getAttribute("user")==null){
String username=null;
String password=null; Cookie[] cookies=request.getCookies();
for(int i=0;cookies!=null && i<cookies.length;i++){
if(cookies[i].getName().equals("user")){
username=URLDecoder.decode(cookies[i].getValue().split("-")[0],"UTF-8");
password=URLDecoder.decode(cookies[i].getValue().split("-")[1],"UTF-8");
}
if(username==null){
username="";
} if(password==null){
password="";
} pageContext.setAttribute("username", username);
pageContext.setAttribute("password", password);
}
}
%>
<title>登录页</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery/jquery-1.5.1.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/login.js"></script>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript">
function loadImage(){
document.getElementById("randImage").src = "${pageContext.request.contextPath}/image.jsp?"+Math.random(); //Math.random()方法非常重要,它使每次的请求都不同以便重新发送。如果每次的请求都一样,那么不会重新生成页面
}
</script>
<style type="text/css">
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
} body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,p,blockquote,th,td
{
margin: 0;
padding: 0;
} .form-signin {
max-width: 500px;
padding: 19px 29px 29px;
margin: 0 auto 20px;
background-color: #fff;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
-moz-box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
} .form-signin .form-signin-heading,.form-signin .checkbox {
margin-bottom: 15px;
} .form-signin input[type="text"],.form-signin input[type="password"] {
height: auto;
margin-bottom: 15px;
padding: 7px 9px;
} h1 {
color: #fff;
background: #06b;
padding: 10px;
font-size: 200%;
text-align: center;
}
.labelError{line-height: 9px; font-size: 10pt; color: #f40000; border: 1px #ffb8b8 solid; padding: 8px 8px 8px 35px; background: url(/StudentManagerWeb/images/error.png) no-repeat; background-color: #fef2f2;}
</style>
</head>
<body>
<div>
<form class="form-signin" action="${pageContext.request.contextPath}/login" method="post" onSubmit="return check()">
<h2 class="form-signin-heading">管理员登录</h2>
<hr>
<table>
<tr>
<td ></td>
<td><font color="red">${error }</font></td>
<td></td>
</tr> <tr>
<td align="right">用户名:</td>
<td><input type="text" name="username" id="username" class="input" value="${username} "></td>
<td><label class="labelError" id="usernameError"></label></td>
</tr> <tr>
<td align="right">密码:</td>
<td><input type="password" name="password" id="password" class="input" value="${password }"></td>
<td><label class="labelError" id="passwordError"></label></td>
</tr> <tr>
<td align="right">验证码:</td>
<td><input type="text" name="imageValue" size="50px" id="imageValue" class="input" value="${verifyCode }"></td>
<td><label class="labelError" id="imageValueError"></label></td>
</tr> <tr>
<td ></td>
<td>
<img onClick="javascript:loadImage();" title="换一张试试" id="randImage"
src="${pageContext.request.contextPath}/image.jsp" width="100" height="200" border="1" align="absmiddle"></td>
<td> </td>
</tr>
<tr>
<td ></td>
<td colspan="2"><label class="checkbox"><input id="remember" name="remember" type="checkbox" value="remember-me">记住我一周 </label></td>
<td></td>
</tr>
<tr>
<td ></td>
<td><button type="submit" class="btn btn-primary">登陆</button> <input type="reset" class="btn btn-primary" value="重写"/></td>
<td></td>
</tr>
</table>
</form>
</div>
</body>
</html>
以上是 login.jsp
以下是image.jsp生成验证码
<%@ page contentType="image/jpeg"
import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
pageEncoding="utf-8"%>
<%!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);
}%>
<%
out.clear(); /*这句针对resin服务器,如果是tomacat可以不要这句*/ out.clear();
//设置页面不缓存
response.setHeader("Prama","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0); //在内存中创建图象
int width=60;
int 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("Tines Nev Roman",Font.PLAIN,18)); //随机产生干扰线
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;
//将认证码显示到图象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}
// 将认证码存入SESSION
session.setAttribute("sRand", sRand);
//图像生效
g.dispose();
//输出图像到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
JSP页面生成验证码功能的更多相关文章
- ASP.NET ashx实现无刷新页面生成验证码
现在大部分网站登陆时都会要求输入验证码,在网上也看了一些范例,现在总结一下如何实现无刷新页面生成验证码. 效果图: 实现方式: 前台: <div> <span>Identify ...
- 使用JSP页面生成PDF报表
转自:http://developer.51cto.com/art/200907/134261.htm 1.iText简介 iText是一个开放源码的Java类库,可以用来方便地生成PDF文件.大家通 ...
- JSP页面中验证码的调用方法
步骤: 1.首先是要生成验证码 2.对验证码类进行调用:主要 实现的是 将验证码图片 输出到response.getOutputStream()这个输出流中 调用时,可以在页面调用,也可以在serv ...
- zf-关于查询机把index.jsp换成index_new.jsp页面之后把功能链接都改成新页面的简单方法
一开始我都是找action 然后一个一个的改 把onmousedown="goURL('index.jsp')" 改成 onmousedown="goURL('index ...
- 如何用jsp页面生成随机的验证数字码
checkNum.jsp <%@ page language="java" import="java.util.*,java.sql.*" pageEnc ...
- JSP页面 CTRL+F 功能实现
.res { color: rgba(255, 0, 0, 1) } .result { background: rgba(255, 255, 0, 1) } --- js 部分 var oldKey ...
- jsp页面简单的验证码实现
前段时间赶着结束毕业设计任务,现在完成了.回来补一下设计毕业设计的过程中遇到的问题和解决方案. 为了使小系统更有模有样,这里尝试在登录页面实现验证码功能.现描述一下我的解决方案. 首先看一下实现后的界 ...
- jsp中生成的验证码和存在session里面的验证码不一致的处理
今天在调试项目的时候发现,在提交表单的时候的验证码有问题,问题是这样的:就是通过debug模式查看得知:jsp页面生成的验证码和表单输入的页面输入的一样,但是到后台执行的时候,你会发现他们是不一样的, ...
- 生成验证码JSP【复用代码】
该JSP可以生成验证码.以后用到的时候就方便了. <%@ page language="java" pageEncoding="UTF-8"%> & ...
随机推荐
- php压缩文件帮助类
<?php /* File name: /include/zip.php Author: Horace 2009/04/15 */ class PHPZip{ var $dirInfo = ar ...
- 毕业生、程序猿转岗该如何选择Java、大数据和VR?答案在这里!
许久不见的朋友请我吃饭,期间给我介绍他一个弟弟,说明年要毕业了,还不知道找啥工作,说有培训机构让他学VR.大数据什么的,不知道前景咋样,想咨询一下我.相信很多朋友面临毕业,都不知道该从事哪个行业,自己 ...
- 自己搭建MVC时遇到的一些问题及解决办法
错误1 The view 'Index' or its master was not found or no view engine supports the searched locations. ...
- mysql锁之Next-Key Locks
一个Next-key锁结合了行锁和gap锁. InnoDB执行一个行级别锁在这样的一个途径,那就是它搜索或者扫描一个表索引时,它设置共享或者独占锁在它遭遇的索引记录上.于是,行级锁是真实的行记录锁.一 ...
- LeetCode 621. Task Scheduler
原题链接在这里:https://leetcode.com/problems/task-scheduler/description/ 题目: Given a char array representin ...
- webpack新版本4.12应用九(配置文件之模块(module))
这些选项决定了如何处理项目中的不同类型的模块. module.noParse RegExp | [RegExp] RegExp | [RegExp] | function(从 webpack 3.0. ...
- bzoj 1000 A+B Problem (And also my first experience of Emacs)
problem:https://www.lydsy.com/JudgeOnline/problem.php?id=1000 This is my first code under Emacs! #in ...
- A SQLiteConnection object for database '/data/data/.../databases/....db' was leaked!
详细异常: A SQLiteConnection object for database '/data/data/.../databases/....db' was leaked! Please ...
- FPGA中的仿真
在进行FPGA工程开发中,都会接触到仿真这个环节.FPGA开发一定要仿真,要养成仿真的习惯. 很多初学者或者学艺不精的工程师都比较排斥仿真. 但是,仿真真的很重要! 仿真可以让设计者能够很快知道模块输 ...
- POJ2226(最小顶点覆盖)
Muddy Fields Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10044 Accepted: 3743 Des ...