登陆验证码(Servlet实现)

http://ewf-momo.iteye.com/blog/1678955

登陆验证码(Servlet实现)

登陆验证码(Servlet实现)


1.前台登陆界面:
Java代码 复制代码 收藏代码
  1. <%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
  2. <%
  3. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";
  4. %>
  5. <%@ taglib prefix="s" uri="/struts-tags"%>
  6. <html>
  7. <head>
  8. <base href="<%=basePath%>">
  9. <title>实训管理系统</title>
  10. <link rel="stylesheet" type="text/css" href="css/team.css" />
  11. <script type="text/javascript">
  12. function changeValidateCode(obj) {
  13. /***
  14. * 获取当前的时间作为参数,无具体意义
  15. * 每次请求需要一个不同的参数,否则可能会返回同样的验证码
  16. * 这和浏览器的缓存机制有关系,也可以把页面设置为不缓存,这样就不用这个参数了。
  17. */
  18. var timenow = new Date().getTime();
  19. obj.src="LoginCode.servlet?d="+timenow;
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <div id="top">
  25. <h1>实训管理系统</h1>
  26. </div>
  27. <div id="content">
  28. <form action="admin/Login" method="post">
  29. <p>用&nbsp;&nbsp;户:<input type="text" name="admin.name">
  30. <p>密&nbsp;&nbsp;码:<input type="password" name="admin.password">
  31. <p>验证码:<input type="text" name="code" size="11"><img src="LoginCode.servlet" οnclick="changeValidateCode(this)" title="点击图片刷新验证码"/>
  32. <p><input type="submit" value="登录"><input type="reset" value="重置"/></p>
  33. </form>
  34. </div>
  35. </body>
  36. </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>用&nbsp;&nbsp;户:<input type="text" name="admin.name">
<p>密&nbsp;&nbsp;码:<input type="password" name="admin.password">
<p>验证码:<input type="text" name="code" size="11"><img src="LoginCode.servlet" οnclick="changeValidateCode(this)" title="点击图片刷新验证码"/>
<p><input type="submit" value="登录"><input type="reset" value="重置"/></p>
</form>
</div>
</body>
</html>


2.web.xml配置文件配置servlet映射路径:
Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>training</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.jsp</welcome-file>
  6. </welcome-file-list>
  7. <filter>
  8. <filter-name>LoginFilter</filter-name>
  9. <filter-class>com.org.momo.filter.LoginFilter</filter-class>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>LoginFilter</filter-name>
  13. <url-pattern>/*</url-pattern>
  14. </filter-mapping>
  15. <filter>
  16. <filter-name>struts2</filter-name>
  17. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  18. </filter>
  19. <filter-mapping>
  20. <filter-name>struts2</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>
  23. <servlet>
  24. <servlet-name>LoginCodeServlet</servlet-name>
  25. <servlet-class>com.org.momo.servlet.LoginCodeServlet</servlet-class>
  26. </servlet>
  27. <servlet-mapping>
  28. <servlet-name>LoginCodeServlet</servlet-name>
  29. <url-pattern>/LoginCode.servlet</url-pattern>
  30. </servlet-mapping>
  31. <listener>
  32. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  33. </listener>
  34. </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代码 复制代码 收藏代码
  1. package com.org.momo.servlet;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6. import java.io.IOException;
  7. import java.util.Random;
  8. import javax.imageio.ImageIO;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletOutputStream;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.http.HttpSession;
  15. public class LoginCodeServlet extends HttpServlet {
  16. public LoginCodeServlet() {
  17. super();
  18. }
  19. public void destroy() {
  20. super.destroy();
  21. }
  22. public void doGet(HttpServletRequest request, HttpServletResponse response)
  23. throws ServletException, IOException {
  24. //设置输出类型和浏览器不保存缓存
  25. response.setContentType("image/jpeg") ;
  26. response.setHeader("Cache-Control", "no-cache") ;
  27. //创建图片对象
  28. int width = 60,height = 20 ;
  29. BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB) ;
  30. Graphics g = image.getGraphics() ;
  31. //生成随机数
  32. Random random = new Random() ;
  33. String s = "" ;
  34. for(int i=0;i<4;i++){
  35. s += random.nextInt(10) ;
  36. }
  37. //把随机数存到Session里面,便于等下比较
  38. HttpSession session = request.getSession() ;
  39. session.setAttribute("code",s) ;
  40. //随机生成颜色 Color color = new Color(255,255,255) ; random.nextInt(256)的值范围是0~255
  41. Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)) ;
  42. //把随机数写到图片上
  43. String a = null ;
  44. Font font = new Font(a,Font.ITALIC,18) ;
  45. g.setColor(color) ;
  46. g.setFont(font);
  47. g.drawString(s,10,height-5) ;
  48. g.dispose() ; //关闭画笔
  49. //把图片送到客户端
  50. ServletOutputStream output = response.getOutputStream() ;
  51. ImageIO.write(image,"JPEG",output) ;
  52. output.flush(); //关闭输出流
  53. }
  54. public void doPost(HttpServletRequest request, HttpServletResponse response)
  55. throws ServletException, IOException {
  56. this.doGet(request,response) ;
  57. }
  58. public void init() throws ServletException {
  59. }
  60. }
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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值