验证码(转载)
1.前台登陆界面:
- <%@ 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映射路径:
- <?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处理类
- 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 {
- }
- }