登陆界面

<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>带图形验证码登录</title>
</head>
<body>
<form name="form1" method="post">
用户名:<input type="text" name="userid" onclick="mes.innerHTML=''" value="${param.userid }"><br/>
密码:<input type="password" name="userpwd" value="${param.userpwd }"><br/>
验证码:<input type="text" name="checkcode"/>
<input type="submit" value="换一张" onclick="form1.action='/servlet/changecheckcode'"/><br/> <input type="submit" value="登陆" onclick="form1action='/servlet/logincheck'"/>
<input type="reset" value="重置"/>
<div id="mes">${info}</div>
</form>
</body>
</html>

验证码的写入

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.imageio.*; public class AuthImg extends HttpServlet
{
private Font mFont = new Font("Arial Black", Font.PLAIN, 16);
public void init() throws ServletException
{
super.init();
}
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);
} public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg"); int width=100, height=18;
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(1, 1, width-1, height-1);
g.setColor(new Color(102,102,102));
g.drawRect(0, 0, width-1, height-1);
g.setFont(mFont); g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x,y,x + xl,y + yl);
}
for (int i = 0;i < 70;i++)
{
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x,y,x - xl,y - yl);
} String sRand="";
for (int i=0;i<6;i++)
{
String tmp = getRandomChar();
sRand += tmp;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(tmp,15*i+10,15);
} HttpSession session = request.getSession(true);
session.setAttribute("rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}
private String getRandomChar()
{
int rand = (int)Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '\u0000';
switch (rand)
{
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char)itmp;
return String.valueOf(ctmp);
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char)itmp;
return String.valueOf(ctmp);
default :
itmp = Math.round(Math.random() * 9);
return String.valueOf(itmp);
}
}
}

其中这是加了干扰线的了,并且用session来保存验证码了,

在登陆的jsp页面中,应该这样设置

验证码如图:

<img src="authImg" id="authImg"/>看不清?<a href="#" onClick="refresh()">单击此处刷新</a>

然后refesh()函数如下写

<script>
function refresh()
{
document.getElementById("authImg").src='authImg?now=' + new Date();
}
</script>

之所以在最后加now,是防止浏览器的缓存

最后再在web.xml里配置下servlet

<servlet>
<servlet-name>img</servlet-name>
<servlet-class>org.yeeku.web.AuthImg</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>img</servlet-name>
<url-pattern>/authImg</url-pattern>
</servlet-mapping>

2017.11.27 用Servlet在JSP中加入验证码的更多相关文章

  1. Servlet和JSP中的过滤器都是Java类

    JSP 过滤器 Servlet和JSP中的过滤器都是Java类,它们存在的目的如下: 在请求访问后端资源时拦截它 管理从服务器返回给客户端的响应 下面列出了多种常用的过滤器类型: 认证过滤器 数据压缩 ...

  2. 2017.11.4 JavaWeb-----基于JavaBean+JSP求任意两数代数和(改进的在JSP页面中无JSP脚本代码的)+网页计数器JavaBean的设计与使用

    修改后的JSP中不含有JSP脚本代码这使得JSP程序的清晰性.简单 1.设计JavaBean 的Add.java 类 package beans; public class Add { private ...

  3. Servlet,JSP 中的中文乱码问题以及解决方案

    问题描述: 在Servlet,JSP 传递数据中,英文无影响,而中文有时候就会出现乱码. 解决方案: 相同的编码: 同一个项目中的每个文件应当设置和保存相同的编码方式,如: html中 <met ...

  4. Servlet、JSP中页面跳转的方式

    一.Servlet:当然,在servlet中,一般跳转都发生在doGet, doPost等方法里面.1)  redirect 方式response.sendRedirect("success ...

  5. 从servlet向jsp中传数据用Java接收js调用

    servlet: response.sendRedirect("showMessage.jsp?ValueA=1"); jsp: var a=<%=request.getPa ...

  6. 2017.11.27 变量进阶与LED矩阵

    局部变量:函数内部声明的变量,只在函数内部有效. 全部变量:在函数外部声明的变量,全局都有效,直到程序执行完毕. 全局变量负作用: 1.降低函数的独立性 2.降低函数的通用性,不利于函数的重复调用. ...

  7. 2017.11.27 stm8 low power-consumption debugging

    1 STM8L+LCD The STM8L-DISCOVERY helps you to discover the STM8L ultralow power features and todevelo ...

  8. Servlet获取JSP中的汉字乱码问题解决方案

    1.String customerName=request.getParameter("customer_name");这样会出现乱码 解决方案很简单: String custom ...

  9. java web中servlet、jsp、html 互相访问的路径问题

    java web中servlet.jsp.html 互相访问的路径问题 在java web种经常出现 404找不到网页的错误,究其原因,一般是访问的路径不对. java web中的路径使用按我的分法可 ...

随机推荐

  1. Windows 环境下安装MongoDB

    mongoDB下载地址 https://www.mongodb.org/ 在mongoDB官网下载windows版本的mongoDB后解压出来(本文以解压到D盘为例) 在解压出来的MongoDB文件夹 ...

  2. python 读取文件使用chunksize后逐块迭代操作

    chunkers=pd.read_csv('dd.csv',chunksize=10000) tot=pd.Series([]) for piece in chunkers: tot=tot.add( ...

  3. 3DSMAX安装失败如何完全卸载

    安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).AUTODESK系列软件着实令人头疼,有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  4. LeanTouch控制移动

    Lean_Touch控制移动 using UnityEngine; using System.Collections; using System.Collections.Generic; using ...

  5. Fixed Function Shader

    Fixed function shader(固定管线着色器) Shader "Custom/Text01" {                        //shader名称 ...

  6. Tomcat WEB搭建+Nginx负载均衡动静分离+DNS解析的实验

    实验拓扑图: 实验环境: 在VMware workstation搭建虚拟环境,利用网络适配器的Nat和桥接模式模拟内网和外网环境. 实验过程中需要安装的工具包包括:vim unzip lrzsz ls ...

  7. vue-cli生成的重要代码详解

    安装好vue-cli之后,我们可以在package.json中看到下面所示: { // 项目名称 "name": "myvue", // 项目版本 " ...

  8. express中connect-flash中间件的使用

    在学习node的时候,flash是困扰我最久的一个中间件,之前一直都没有很好的理解,这里做一个总结. 参考自:http://yunkus.com/connect-flash-usage/ 什么是fla ...

  9. ErlangC 最佳人力效益指标

    以平均服务时间(AHT)180秒,顾客来电量每15分钟150通以及服务目标时间在20秒内为例子说明最佳人力效益指标.此假设条件下由Erlang C模拟器的结果如下图, 假设我希望客服中心的期望服务水准 ...

  10. 深入学习hbase:表,列族,列标识,版本和cell

    HBase是面向列的分布式的数据库,和传统的关系型数据库有很大的不同:物理模型和逻辑模型.这里我们要首先讲一下HBase数据库相关的区别于关系型数据库的几个基本概念:          表:HBase ...