jsp实现验证码登陆

时间:2022-10-21 13:45:27

login.jsp:

<%@ page language="java" import="java.util.*,com.cn.servlet.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户登录</title>
<script>
function changeImg(){
var imgObj=document.getElementById("img");
imgObj.src="CheckCodeServlet?ran="+Math.random();
}
</script>
</head> <body>
<form action="LoginServlet" method="post">
账 号:&nbsp;&nbsp;<input type="text" name="account" ><br/>
密 码:&nbsp;&nbsp;<input type="password" name="password"><br/>
验证码:<input type="text" name="code" >
<img src="CheckCodeServlet" id="img"><a href="javascript:changeImg()">看不清换一张</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="登录"><br/>
<span><%=request.getAttribute("Msg")%></span>
</form>
</body>
</html>

CheckCodeServlet:(生成验证码)

package com.cn.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random; import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class CheckCodeServlet extends HttpServlet {
//产生随机的字体
private Font getFont(){
//创建random对象,用于生成随机数
Random random = new Random();
//创建字体数组,用于封装不同字体的Font对象
Font font[] = new Font[5];
font[0] = new Font("Ravie", Font.PLAIN, 24);
font[1] = new Font("Antique Olive Compact", Font.PLAIN, 24);
font[2] = new Font("Forte", Font.PLAIN, 24);
font[3] = new Font("Wide Latin", Font.PLAIN, 24);
font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, 24);
return font[random.nextInt(5)];
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg");
OutputStream os = response.getOutputStream();
int width = 83, height = 30;
//建立指定宽、高和BufferedImage对象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics g = image.getGraphics();//将画笔绘制于image上
Color c= g.getColor();//保存当前画笔的颜色
g.fillRect(0, 0, width, height);//填充矩形
char[] ch = "abcdefghigklmnopqrstuvwxyz2345678901".toCharArray();//随机产生字符串
int length = ch.length;//随机字符串长度
String sRand = "";//保存随机产生的字符串
Random random = new Random();
for (int i=0; i<4; i++){
g.setFont(getFont());//设置字体
String rand = new Character(ch[random.nextInt(length)]).toString();//随机生成0~9的数字
sRand += rand;
//设置随机颜色
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
g.drawString(rand, 20*i+6, 25);
}
//产生随机干扰点
for(int i=0; i<20; i++){
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
g.drawOval(x1, y1, 2, 2);
}
g.setColor(c);//重置画笔颜色
g.dispose();//释放此图形的上下文及使用的所有系统资源
request.getSession().setAttribute("safecode", sRand);//验证码记录到session
ImageIO.write(image, "JPEG", os);//输出图像到页面
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response);
}
}

LoginServlet:

package com.cn.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet {
private String account = "Vito-Yan";
private String password = "12345678";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String acc = request.getParameter("account");
String pwd = request.getParameter("password");
String code = request.getParameter("code");
if(acc.equals(account) && pwd.equals(password)){
HttpSession session = request.getSession();
String sessionCode = (String)session.getAttribute("safecode");
if(!(sessionCode.equals(code))){
request.setAttribute("Msg", "对不起验证码不正确");
request.getRequestDispatcher("login.jsp").forward(request, response);
}else{
session.setAttribute("account", account);
request.getRequestDispatcher("main.jsp").forward(request, response);
}
}else{
request.setAttribute("Msg", "对不起账号密码不正确");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
} }

jsp实现验证码登陆的更多相关文章

  1. 使用jsp生成验证码

    在开发中验证码是比较常用到有效防止这种问题对某一个特定注册用户用特定程序暴力破解方式进行不断的登陆尝试的方式. 此演示程序包括三个文件: 1.index.jsp:登录页面 2.image.jsp:生成 ...

  2. Jsp制作验证码

    验证码 验证码(CAPTCHA)是"Completely Automated Public Turing test to tell Computers and Humans Apart&qu ...

  3. web自动化测试中绕开验证码登陆的方式

    web自动化测试中登陆需验证码是很大的一个困扰.现推荐一种简单的避开验证码登陆的方式,先代码进入登录页,人工输入验证码登录后浏览器自动保存cookie,再在新的标签中登录. 具体代码如下: publi ...

  4. Web UI自动化测试中绕开验证码登陆方式浅谈

    web自动化测试中让测试者感到困惑的是登陆验证码,每次都不一样.现在推荐一种绕开验证码登陆的方式,其实就是将web浏览器获取的登陆cookie加载到程序中就可以了,这样程序就会认为你已经登陆,就可以跳 ...

  5. JSP制作简单登陆

    JSP制作简单登陆界面 运行环境 eclipse+tomcat+MySQL 不知道的可以参考Jsp运行环境--Tomcat 项目列表 这里我先把jsp文件先放在Web-INF外面访问 需要建立的几个文 ...

  6. 通过cookies跳过验证码登陆页面,直接访问网站的其它URL

    我每次手动访问去NN网的一家酒店,就不需要登陆,一旦我用脚本打开就会让我登陆,而登陆页面又有验证码,不想识别验证码,所以就想:“通过cookies跳过验证码登陆页面,直接访问网站的其它URL”   转 ...

  7. Spring Cloud OAuth2(二) 扩展登陆方式:账户密码登陆、 手机验证码登陆、 二维码扫码登陆

    概要 基于上文讲解的spring cloud 授权服务的搭建,本文扩展了spring security 的登陆方式,增加手机验证码登陆.二维码登陆. 主要实现方式为使用自定义filter. Authe ...

  8. JavaWeb-SpringSecurity使用短信验证码登陆

    相关博文 JavaWeb-SpringBoot_一个类实现腾讯云SDK发送短信 传送门 系列博文 项目已上传至guthub 传送门 JavaWeb-SpringSecurity初认识 传送门 Java ...

  9. SpringSceurity&lpar;5&rpar;---短信验证码登陆功能

    SpringSceurity(5)---短信验证码登陆功能 有关SpringSceurity系列之前有写文章 1.SpringSecurity(1)---认证+授权代码实现 2.SpringSecur ...

随机推荐

  1. iOS 封装添加按钮的方法

    添加按钮 #pragma mark 添加按钮 - (void)addButtonWithImage:(NSString *)image highImage:(NSString *)highImage ...

  2. POM的配置文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. mysqldump的流程

    发布时间:2013 年 4 月 6 日 发布者: OurMySQL 来源:P.Linux Laboratory      前几天看到群里在讨论mysqldump导致锁表的问题,为什么一个表已经dump ...

  4. 学习面试题Day06

    1.字节流的处理方式 字节流处理的是计算机最基本的单位byte,它可以处理任何数据格式的数据.主要的操作对象就是byte数组,通过read()和write()方法把byte数组中的数据写入或读出. 2 ...

  5. Swift互用性:与 C的API交互(Swift 2&period;0版)-b

    节包含内容: 基本数据类型(Primitive Types) 枚举(Enumerations) 指针(Pointer) 全局常量(Global Constants) 预处理指令(Preprocesso ...

  6. Ajax——ajax调用数据总结

    在做人事系统加入批量改动的功能中,须要将前台中的数据传给后台.后台并运行一系列的操作. 通过查询和学习了解到能够通过ajax将值传入到后台,并在后台对数据进行操作. 说的简单点.就是ajax调用后台的 ...

  7. 为HttpStatusCodeResult加入customErrors

    asp.net mvc的action返回值为HttpStatusCodeResult时的customErrors总是不起作用 (404和exception时的500,因为他们并不是HttpStatus ...

  8. IPC&dollar;横向渗透代码实现

    思路 IPC$横向渗透的方法,也是病毒常用的扩散方法. 1.建立连接 2.复制文件到共享中C$.D$.E$.F$ 3.获取服务器的时间 3.设定计划任务按时间执行 用到的Windows API 建立网 ...

  9. Visual Studio 2012使用NUnit单元测试实践01,安装NUnit并使用

    在Visual Studio 2012中,默认使用Microsoft自带的MS-Test测试框架.但,Visual Studio同样允许使用第三方测试框架,比如NUnit,xUnit,MbUnit,等 ...

  10. Django templates html中进行模板渲染时使用python语法的基本方式

    导包(可以在模板中导入python包进行使用): <%!import urllib%> <%! from ** import **%> 使用if for等python语句: % ...