登陆验证码(Servlet实现)
2013-01-15 22:40
281 查看
/article/4148273.html
登陆验证码(Servlet实现)
博客分类:
JSP/Servlet
小工具集合
servlet 登陆验证码
登陆验证码(Servlet实现)
1.前台登陆界面:
Java代码
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() +
":" + request.getServerPort() + request.getContextPath() +
"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<base href="<%=basePath%>">
<title>实训管理系统</title>
<link rel="stylesheet" type="text/css" href="css/team.css" />
<script type="text/javascript">
function changeValidateCode(obj) {
/***
* 获取当前的时间作为参数,无具体意义
* 每次请求需要一个不同的参数,否则可能会返回同样的验证码
* 这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。
*/
var timenow = new Date().getTime();
obj.src="LoginCode.servlet?d="+timenow;
}
</script>
</head>
<body>
<div id="top">
<h1>实训管理系统</h1>
</div>
<div id="content">
<form action="admin/Login" method="post">
<p>用 户:<input type="text" name="admin.name">
<p>密 码:<input type="password" name="admin.password">
<p>验证码:<input type="text" name="code" size="11"><img src="LoginCode.servlet" onclick="changeValidateCode(this)"
title="点击图片刷新验证码"/>
<p><input type="submit" value="登录"><input type="reset" value="重置"/></p>
</form>
</div>
</body>
</html>
2.web.xml配置文件配置servlet映射路径:
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>training</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.org.momo.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LoginCodeServlet</servlet-name>
<servlet-class>com.org.momo.servlet.LoginCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginCodeServlet</servlet-name>
<url-pattern>/LoginCode.servlet</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.LoginCodeServlet.java处理类
Java代码
package com.org.momo.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginCodeServlet
extends HttpServlet {
public LoginCodeServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置输出类型和浏览器不保存缓存
response.setContentType("image/jpeg") ;
response.setHeader("Cache-Control",
"no-cache") ;
//创建图片对象
int width = 60,height =
20 ;
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB) ;
Graphics g = image.getGraphics() ;
//生成随机数
Random random = new Random() ;
String s = "" ;
for(int i=0;i<4;i++){
s += random.nextInt(10) ;
}
//把随机数存到Session里面,便于等下比较
HttpSession session = request.getSession() ;
session.setAttribute("code",s) ;
//随机生成颜色 Color color = new Color(255,255,255) ; random.nextInt(256)的值范围是0~255
Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)) ;
//把随机数写到图片上
String a = null ;
Font font = new Font(a,Font.ITALIC,18) ;
g.setColor(color) ;
g.setFont(font);
g.drawString(s,10,height-5) ;
g.dispose() ; //关闭画笔
//把图片送到客户端
ServletOutputStream output = response.getOutputStream() ;
ImageIO.write(image,"JPEG",output) ;
output.flush(); //关闭输出流
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request,response) ;
}
public void init()
throws ServletException {
}
}
大小: 14.1 KB
登陆验证码(Servlet实现)
博客分类:
JSP/Servlet
小工具集合
servlet 登陆验证码
登陆验证码(Servlet实现)
1.前台登陆界面:
Java代码
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%
String basePath = request.getScheme() + "://" + request.getServerName() +
":" + request.getServerPort() + request.getContextPath() +
"/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<base href="<%=basePath%>">
<title>实训管理系统</title>
<link rel="stylesheet" type="text/css" href="css/team.css" />
<script type="text/javascript">
function changeValidateCode(obj) {
/***
* 获取当前的时间作为参数,无具体意义
* 每次请求需要一个不同的参数,否则可能会返回同样的验证码
* 这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。
*/
var timenow = new Date().getTime();
obj.src="LoginCode.servlet?d="+timenow;
}
</script>
</head>
<body>
<div id="top">
<h1>实训管理系统</h1>
</div>
<div id="content">
<form action="admin/Login" method="post">
<p>用 户:<input type="text" name="admin.name">
<p>密 码:<input type="password" name="admin.password">
<p>验证码:<input type="text" name="code" size="11"><img src="LoginCode.servlet" onclick="changeValidateCode(this)"
title="点击图片刷新验证码"/>
<p><input type="submit" value="登录"><input type="reset" value="重置"/></p>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/"; %> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <base href="<%=basePath%>"> <title>实训管理系统</title> <link rel="stylesheet" type="text/css" href="css/team.css" /> <script type="text/javascript"> function changeValidateCode(obj) { /*** * 获取当前的时间作为参数,无具体意义 * 每次请求需要一个不同的参数,否则可能会返回同样的验证码 * 这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。 */ var timenow = new Date().getTime(); obj.src="LoginCode.servlet?d="+timenow; } </script> </head> <body> <div id="top"> <h1>实训管理系统</h1> </div> <div id="content"> <form action="admin/Login" method="post"> <p>用 户:<input type="text" name="admin.name"> <p>密 码:<input type="password" name="admin.password"> <p>验证码:<input type="text" name="code" size="11"><img src="LoginCode.servlet" onclick="changeValidateCode(this)" title="点击图片刷新验证码"/> <p><input type="submit" value="登录"><input type="reset" value="重置"/></p> </form> </div> </body> </html>
2.web.xml配置文件配置servlet映射路径:
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>training</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.org.momo.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>LoginCodeServlet</servlet-name>
<servlet-class>com.org.momo.servlet.LoginCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginCodeServlet</servlet-name>
<url-pattern>/LoginCode.servlet</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>training</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>LoginFilter</filter-name> <filter-class>com.org.momo.filter.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>LoginCodeServlet</servlet-name> <servlet-class>com.org.momo.servlet.LoginCodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginCodeServlet</servlet-name> <url-pattern>/LoginCode.servlet</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
3.LoginCodeServlet.java处理类
Java代码
package com.org.momo.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginCodeServlet
extends HttpServlet {
public LoginCodeServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置输出类型和浏览器不保存缓存
response.setContentType("image/jpeg") ;
response.setHeader("Cache-Control",
"no-cache") ;
//创建图片对象
int width = 60,height =
20 ;
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB) ;
Graphics g = image.getGraphics() ;
//生成随机数
Random random = new Random() ;
String s = "" ;
for(int i=0;i<4;i++){
s += random.nextInt(10) ;
}
//把随机数存到Session里面,便于等下比较
HttpSession session = request.getSession() ;
session.setAttribute("code",s) ;
//随机生成颜色 Color color = new Color(255,255,255) ; random.nextInt(256)的值范围是0~255
Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)) ;
//把随机数写到图片上
String a = null ;
Font font = new Font(a,Font.ITALIC,18) ;
g.setColor(color) ;
g.setFont(font);
g.drawString(s,10,height-5) ;
g.dispose() ; //关闭画笔
//把图片送到客户端
ServletOutputStream output = response.getOutputStream() ;
ImageIO.write(image,"JPEG",output) ;
output.flush(); //关闭输出流
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request,response) ;
}
public void init()
throws ServletException {
}
}
package com.org.momo.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginCodeServlet extends HttpServlet { public LoginCodeServlet() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置输出类型和浏览器不保存缓存 response.setContentType("image/jpeg") ; response.setHeader("Cache-Control", "no-cache") ; //创建图片对象 int width = 60,height = 20 ; BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB) ; Graphics g = image.getGraphics() ; //生成随机数 Random random = new Random() ; String s = "" ; for(int i=0;i<4;i++){ s += random.nextInt(10) ; } //把随机数存到Session里面,便于等下比较 HttpSession session = request.getSession() ; session.setAttribute("code",s) ; //随机生成颜色 Color color = new Color(255,255,255) ; random.nextInt(256)的值范围是0~255 Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)) ; //把随机数写到图片上 String a = null ; Font font = new Font(a,Font.ITALIC,18) ; g.setColor(color) ; g.setFont(font); g.drawString(s,10,height-5) ; g.dispose() ; //关闭画笔 //把图片送到客户端 ServletOutputStream output = response.getOutputStream() ; ImageIO.write(image,"JPEG",output) ; output.flush(); //关闭输出流 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response) ; } public void init() throws ServletException { } }
大小: 14.1 KB
相关文章推荐
- Servlet实现带验证码登陆功能Demo
- jsp servlet 实现登陆验证码
- 最简单的Servlet实现验证码
- JSP_实现Servlet登陆,注册,链接Mysql数据库。
- jsp+servlet+ajax+oracle数据库-实现简单的登陆、注册、找回密码功能(与Oralce数据库连接)
- 使用纯Servlet实现验证码
- python实现带验证码网站的自动登陆
- 采用servlet实现验证码图片生成器
- 使用Servlet制作简单的登陆验证码
- jsoup实现微博登陆及发微博(带验证码)
- Yii2框架实现登陆添加验证码功能示例
- 登陆验证码(struts2实现)
- MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能
- servlet 处理登陆表单数据(数据库)主要代码实现
- Django 登陆验证码和中间件的实现
- java web验证码生成总结(包括servlet、jsp和struts2实现)
- jsp+servlet实现最基本的注册登陆功能
- JAVA_WEB项目(结合Servlet+jsp+ckEditor编辑器+jquery easyui技术)实现新闻发布管理系统第二篇:登陆和注销功能实现
- JSP+Servlet+Tomcat+Mysql实现注册、登陆、修改、添加、查看、分页实例之----查看单个明细
- servlet实现一次性验证码