<%@ 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">记住我一周 &nbsp;&nbsp;&nbsp;&nbsp; </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页面生成验证码功能的更多相关文章

  1. ASP.NET ashx实现无刷新页面生成验证码

    现在大部分网站登陆时都会要求输入验证码,在网上也看了一些范例,现在总结一下如何实现无刷新页面生成验证码. 效果图: 实现方式: 前台: <div> <span>Identify ...

  2. 使用JSP页面生成PDF报表

    转自:http://developer.51cto.com/art/200907/134261.htm 1.iText简介 iText是一个开放源码的Java类库,可以用来方便地生成PDF文件.大家通 ...

  3. JSP页面中验证码的调用方法

    步骤: 1.首先是要生成验证码 2.对验证码类进行调用:主要 实现的是  将验证码图片 输出到response.getOutputStream()这个输出流中 调用时,可以在页面调用,也可以在serv ...

  4. zf-关于查询机把index.jsp换成index_new.jsp页面之后把功能链接都改成新页面的简单方法

    一开始我都是找action 然后一个一个的改 把onmousedown="goURL('index.jsp')" 改成 onmousedown="goURL('index ...

  5. 如何用jsp页面生成随机的验证数字码

    checkNum.jsp <%@ page language="java" import="java.util.*,java.sql.*" pageEnc ...

  6. JSP页面 CTRL+F 功能实现

    .res { color: rgba(255, 0, 0, 1) } .result { background: rgba(255, 255, 0, 1) } --- js 部分 var oldKey ...

  7. jsp页面简单的验证码实现

    前段时间赶着结束毕业设计任务,现在完成了.回来补一下设计毕业设计的过程中遇到的问题和解决方案. 为了使小系统更有模有样,这里尝试在登录页面实现验证码功能.现描述一下我的解决方案. 首先看一下实现后的界 ...

  8. jsp中生成的验证码和存在session里面的验证码不一致的处理

    今天在调试项目的时候发现,在提交表单的时候的验证码有问题,问题是这样的:就是通过debug模式查看得知:jsp页面生成的验证码和表单输入的页面输入的一样,但是到后台执行的时候,你会发现他们是不一样的, ...

  9. 生成验证码JSP【复用代码】

    该JSP可以生成验证码.以后用到的时候就方便了. <%@ page language="java" pageEncoding="UTF-8"%> & ...

随机推荐

  1. java mybatis XML文件中大于号小于号转义(转载)

    因为这个是xml文件不允许出现类似“>”这样的字符 用了转义字符把>和<替换掉,然后就没有问题了. XML转义字符 字段 符号 说明 &lt ; < 小于号 & ...

  2. chm下载地址收集

    Js_chm   http://jjidc.jb51.net:81/201007/books/W3C_javascript.rar jQuery_1.7_chm    http://jjidc.jb5 ...

  3. Electron 使用 Webpack2 打包多入口应用程序

    Electron 使用 Webpack2 打包多入口应用程序 接前面一篇文章,前一篇文章中只有一个页面,并且只有一个js文件,所以打包的时候会把那个js打包成一个bundle.js文件.但是假如我们有 ...

  4. jmeter 查看提取的参数

    需求:查看“传入的参数”或者“正则表达提取的参数”等...... 解决:添加Debug Sampler组件,不需要配置,直接使用默认 1.使用CSV Data Set Config组件“传入的参数”直 ...

  5. VS2017 Product Key

    Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH

  6. GitLab non-standard SSH port

    /***************************************************************************** * GitLab non-standard ...

  7. BZOJ - 3224 Tyvj 1728 普通平衡树 (treap/树状数组)

    题目链接 treap及树状数组模板题. treap版: #include<bits/stdc++.h> using namespace std; typedef long long ll; ...

  8. 对Tornado异步操作Sqlalchemy方法的选定 不错

    使用原因 在一个实时通讯的项目中,由于需要使用Websocket这一协议,便在Python框架中选定了Tornado,也同时使用了Sqlalchemy这一ORM框架. 大家都知道Tornado有异步非 ...

  9. C# 实现程序只启动一次(总结)

    我前面的三篇文章是从网上找到的(如下链接),都说是实现程序只启动一次的功能. C#防止程序多次运行C#检测程序重复运行的函数(可以在多用户登录情况下检测)C# 实现程序只启动一次(多次运行激活第一个实 ...

  10. Angular2配置文件详解

    初学接触Angular2的时候,通过ng new your-project-name 命令生成一个工程空壳,本文来介绍下每个文件的作用及含义. 先来看看src的文件目录: 文件详解 File 文件 P ...