• 验证码(转载)




    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.      
    20. obj.src="LoginCode.servlet?d="+timenow;      
    21. }      
    22. </script>   
    23. </head>  
    24. <body>  
    25. <div id="top">  
    26. <h1>实训管理系统</h1>  
    27. </div>  
    28. <div id="content">  
    29. <form action="admin/Login" method="post">  
    30. <p>用&nbsp;&nbsp;户:<input type="text" name="admin.name">  
    31. <p>密&nbsp;&nbsp;码:<input type="password" name="admin.password">  
    32. <p>验证码:<input type="text" name="code" size="11"><img src="LoginCode.servlet" onclick="changeValidateCode(this)" title="点击图片刷新验证码"/>  
    33. <p><input type="submit" value="登录"><input type="reset" value="重置"/></p>  
    34. </form>  
    35. </div>  
    36. </body>  
    37. </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.     
    8.     <filter>  
    9.     <filter-name>LoginFilter</filter-name>  
    10.     <filter-class>com.org.momo.filter.LoginFilter</filter-class>  
    11.   </filter>   
    12.   <filter-mapping>  
    13.     <filter-name>LoginFilter</filter-name>  
    14.     <url-pattern>/*</url-pattern>  
    15.   </filter-mapping>  
    16.   <filter>  
    17.     <filter-name>struts2</filter-name>  
    18.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
    19.   </filter>  
    20.   <filter-mapping>  
    21.     <filter-name>struts2</filter-name>  
    22.     <url-pattern>/*</url-pattern>  
    23.   </filter-mapping>  
    24.   
    25.     
    26.   <servlet>  
    27.     <servlet-name>LoginCodeServlet</servlet-name>  
    28.     <servlet-class>com.org.momo.servlet.LoginCodeServlet</servlet-class>  
    29.   </servlet>  
    30.   <servlet-mapping>  
    31.     <servlet-name>LoginCodeServlet</servlet-name>  
    32.     <url-pattern>/LoginCode.servlet</url-pattern>  
    33.   </servlet-mapping>  
    34.     
    35.     
    36.   <listener>  
    37.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    38.   </listener>  
    39. </web-app>  




    3.LoginCodeServlet.java处理类 

    Java代码  收藏代码
      1. package com.org.momo.servlet;  
      2.   
      3. import java.awt.Color;  
      4. import java.awt.Font;  
      5. import java.awt.Graphics;  
      6. import java.awt.image.BufferedImage;  
      7. import java.io.IOException;  
      8. import java.util.Random;  
      9.   
      10. import javax.imageio.ImageIO;  
      11. import javax.servlet.ServletException;  
      12. import javax.servlet.ServletOutputStream;  
      13. import javax.servlet.http.HttpServlet;  
      14. import javax.servlet.http.HttpServletRequest;  
      15. import javax.servlet.http.HttpServletResponse;  
      16. import javax.servlet.http.HttpSession;  
      17.   
      18. public class LoginCodeServlet extends HttpServlet {  
      19.   
      20.     public LoginCodeServlet() {  
      21.         super();  
      22.     }  
      23.   
      24.     public void destroy() {  
      25.         super.destroy();  
      26.     }  
      27.   
      28.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
      29.             throws ServletException, IOException {    
      30.          //设置输出类型和浏览器不保存缓存  
      31.          response.setContentType("image/jpeg") ;  
      32.          response.setHeader("Cache-Control""no-cache") ;  
      33.            
      34.          //创建图片对象  
      35.          int width = 60,height = 20 ;  
      36.          BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB) ;  
      37.            
      38.          Graphics g = image.getGraphics() ;  
      39.            
      40.          //生成随机数  
      41.          Random random = new Random() ;  
      42.          String s = "" ;  
      43.          for(int i=0;i<4;i++){  
      44.              s += random.nextInt(10) ;  
      45.          }  
      46.            
      47.          //把随机数存到Session里面,便于等下比较  
      48.          HttpSession session = request.getSession() ;  
      49.          session.setAttribute("code",s) ;  
      50.            
      51.          //随机生成颜色  Color color =  new Color(255,255,255) ;     random.nextInt(256)的值范围是0~255  
      52.          Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)) ;  
      53.            
      54.          //把随机数写到图片上  
      55.          String a = null ;  
      56.          Font font = new Font(a,Font.ITALIC,18) ;  
      57.          g.setColor(color) ;  
      58.          g.setFont(font);  
      59.          g.drawString(s,10,height-5) ;  
      60.          g.dispose() ;    //关闭画笔  
      61.            
      62.          //把图片送到客户端  
      63.          ServletOutputStream output = response.getOutputStream() ;  
      64.          ImageIO.write(image,"JPEG",output) ;  
      65.          output.flush();   //关闭输出流  
      66.     }  
      67.   
      68.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
      69.             throws ServletException, IOException {  
      70.          this.doGet(request,response) ;  
      71.     }  
      72.       
      73.     public void init() throws ServletException {  
      74.     }  
      75. }  
  • 相关阅读:
    js 生成 yyyy-mm-dd 格式的逼格姿势
    使用chrome联调不在同一个域的请求
    linux命令收集
    spring component-scan filter
    命令别名alias设置
    vi命令的使用
    Git中的文件状态和使用问题解决
    Git常用命令
    Maven
    MySQL命令
  • 原文地址:https://www.cnblogs.com/cmdl/p/3132804.html
Copyright © 2020-2023  润新知