• Java绘制登陆验证码


    登陆验证码

    界面JSP代码
    <span style="font-family:Comic Sans MS;font-size:18px;"><%@ page language="java" import="java.util.*" 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>
        <base href="<%=basePath%>">
        <title>My JSP 'index.jsp' starting page</title>
    	<meta http-equiv="pragma" content="no-cache">
    	<meta http-equiv="cache-control" content="no-cache">
    	<meta http-equiv="expires" content="0">    
    	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    	<meta http-equiv="description" content="This is my page">
    <!-- <link rel="stylesheet" type="text/css" href="css/style.css"> -->	
    	<style type="text/css">
    	</style>
    	<script  type="text/javascript">
    	function changeCode(){
    		//多次加载同一张图片,浏览器会缓存,这里所以添加一个参数
    		var time=new Date().getTime();
    		document.getElementById("img").src="<%=request.getContextPath()%>/verificationCode?time"+time;	
    	}
    	</script>
      </head>
      <body>
        <div id="myCode">
    		   <form action="Code" method="POST">  
    		    	验证码 <input type="text" id="codeInput" name="codeInput" >
    		    	<img alt="验证码" id="img" src="<%=request.getContextPath()%>/verificationCode">
    		    	<a href="javascript:changeCode();"><img id="reflash" style=" 22px;height: 22px;" src="img/reflash.png"></a>
    				<br/><input type="submit" value="提交"/>
    		 </form>
        </div>
      </body>
    </html></span>
    我的Servlet代码
    <span style="font-family:Comic Sans MS;font-size:18px;">package com.tang.code;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    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 verificationCode extends HttpServlet {
    /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		doPost(request,response);
    	}
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		/* BufferedImage是Image的一个子类,
    		BufferedImage生成的图片在内存里有一个图像缓冲区,
    		利用这个缓冲区我们可以很方便的操作这个图片,通常用来做图片修改操作如大小变换、
    		图片变灰、设置图片透明或不透明等。
    		*/
    		BufferedImage bi=new BufferedImage(68,22,BufferedImage.TYPE_INT_RGB);
    		//Graphics 类是所有图形上下文的抽象基类,允许应用程序在组件(已经在各种设备上实现)以及闭屏图像上进行绘制。
    		Graphics g=bi.getGraphics();
    		Color c=new Color(200,150,255);
    		g.setColor(c);
    		/*fillRect(int x,int y,int width, int height)
    		 * x - 要绘制矩形的 x 坐标。
    		 * y - 要绘制矩形的 y 坐标。
             * width - 要绘制矩形的宽度。
    		 * height - 要绘制矩形的高度。
    		 */
    		g.fillRect(0, 0, 68, 22);
    		char[] ch="ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789".toCharArray();
    		//实例化随机对象
    		Random r=new Random();
    		int len=ch.length,index;
    		StringBuffer sb=new StringBuffer();
    		for(int i=0;i<4;i++){
    			//产生0~len长度的随机数
    			index=r.nextInt(len);
    			System.out.println("index=r.nextInt(len):"+index);
    			//设置字的颜色
    			//new Color(r.nextInt(88),r.nextInt(88),r.nextInt(255))
    			g.setColor(new Color(r.nextInt(88),r.nextInt(88),r.nextInt(255)));
    			//drawString(String str, int x, int y)  使用此图形上下文的当前字体和颜色绘制由指定 string 给定的文本。
    		   g.drawString(ch[index]+"",(i*15)+3,18);
    		   sb.append(ch[index]);
    		}
    		//保存到session中,在服务器中通过获取session对比验证码是否正确
    		request.getSession().setAttribute("pircode", sb.toString());
    		ImageIO.write(bi,"JPG",response.getOutputStream());
    	}
    }</span>

  • 相关阅读:
    解决一起web 页面被劫持的案例
    django rest framwork教程之外键关系和超链接
    django restframwork 教程之authentication权限
    Puppet nginx+passenger模式配置
    Django restframwork教程之类视图(class-based views)
    使用emplace操作
    C++中的显示类型转换
    整数加法
    在不知道学生人数和每个学生课程数量的情况下对学生的平均成绩排序
    树的高度
  • 原文地址:https://www.cnblogs.com/jarrem/p/5910823.html
Copyright © 2020-2023  润新知