<%@ 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. scrapy的简单使用以及相关设置属性的介绍

    0. 楔子(一个最简单的案例) 1.scrapy.Spider scrapy.spiders.Spider name allowed_domains start_urls custom_setting ...

  2. laravel发送邮件

    这里已163为例: 1..env文件配置和mail.php配置(默认使用.env) MAIL_DRIVER=smtp MAIL_HOST=smtp.163.com MAIL_PORT=465 MAIL ...

  3. Thrift之c++实例

    一.c++实例 1.下载与安装thrift工具 http://thrift.apache.org/download/ .服务端代码 1).新建vc工程. 2).将上面的文件拷贝到工程目录下,Test_ ...

  4. Android OpenCSV

    OpenCSV https://sourceforge.net/projects/opencsv/ 使用参考 http://stackoverflow.com/questions/16672074/i ...

  5. Asphyre Sphinx is a cross-platform framework for developing 2D/3D video games and interactive business applications

    Introduction Introduction Asphyre Sphinx is a cross-platform framework for developing 2D/3D video ga ...

  6. Zookeeper在Dubbo中的作用及Zk集群的选举原理

    转自 : https://blog.csdn.net/zh15732621679/article/details/80723358

  7. 【Java】Java学习笔记

    教程 计算机所有的数据信息都是由二进制的0,1组成的,B(Byte)就是字节,1B=8bit(位),2的10次幂是1024,我们所说的硬盘容量是40GB.80GB.160GB,这里的B指是的Byte也 ...

  8. 【ACM非算法部分】STL汇总

    stackqueuepriority_queuemapsetpair ====================================== stack:(栈) 包含:#include<s ...

  9. 【JavsScript高级程序设计】学习笔记[1]

    1. 在HTML中使用javascript 在使用script嵌入脚本时,脚本内容不能包含'</script>'字符串(会被认为是结束的标志).可以通过转义字符解决('\') 默认scri ...

  10. Sublimetext3的下载与安装

    https://www.sublimetext.com/ Sublimetext价格不菲,但是作者允许无限期的免费试用,请不要下载破解版 使用国内汉化版的很有可能感染病毒,请善待电脑 百度搜索找到官网 ...