zoukankan      html  css  js  c++  java
  • 登陆界面验证码实现

    login.jsp

    <form method="post" action="user_login.action">
    				<label class="control-label" for="inputUser"> <span
    					class="icon-user"></span>
    				</label>
    					<input  type="text" id="inputUser"  name="sysuserinfo.userCode" placeholder="学号"
    						required="required" pattern="[0-9]{10}" value="">
    				<label class="control-label" for="inputPassword"> <span
    					class="icon-password"></span>
    				</label>
    
    					<input type="password" id="inputPassword" name="sysuserinfo.password"
    						required="required" placeholder="密码">
    				<label class="control-label" for="inputCode"> <span
    					class="icon-key"></span>
    					<input type="text"  name="chknumber" required="required" placeholder="验证码" maxlength="5" > 
    						<img id="imgCode"
    						title="看不清楚请点击这里" src="<%=basePath%>randomCode.action" onclick="reloadcode(this,'<%=basePath%>')" alt="验证码"
    						/>
    </form>

    RandomAction.java

    public class RandomCodeAction extends ActionSupport implements SessionAware,
    		ServletResponseAware {
    
    	private Map<String, Object> session;
    	private HttpServletResponse response;
    	private static final long serialVersionUID = 1L;
    
    	/**
    	 * 生成验证码
    	 */
    	@Override
    	public String execute() throws Exception {
    		log.error("LOGIN-INFO:RandomCodeAction-execute()");
    		response.setHeader("Cache-Control", "no-cache");
    		int width = 60; // 图片宽度
    		int height = 20; // 图片高度
    		BufferedImage image = new BufferedImage(width, height,
    				BufferedImage.TYPE_INT_RGB);
    		Graphics graphics = image.createGraphics();
    		graphics.setColor(this.getColor()); // 背景颜色
    		graphics.fillRect(0, 0, width, height);
    		graphics.setFont(new Font("Arial", Font.BOLD, 18));
    		graphics.setColor(this.getColor()); // 字的颜色
    		String number = String
    				.valueOf(System.currentTimeMillis() % 9000 + 1000);
    		// 生成四位随机数
    		System.out.println("系统验证码:" + number);
    		if (number == null) {
    			System.out.println("hahah");
    		}
    		session.put("randomCode", number); // 写入session中
    		graphics.drawString(number, (int) (width * 0.1), (int) (height * 0.8));
    		graphics.dispose();
    		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response
    				.getOutputStream());
    		encoder.encode(image);
    		response.getOutputStream().flush(); // 刷新到页面生成图片
    		response.getOutputStream().close(); // 关闭writer
    		return null;
    	}
    
    	private Color getColor() {
    		int red = (int) (Math.random() * 1000 % 256);
    		int green = (int) (Math.random() * 1000 % 256);
    		int blue = (int) (Math.random() * 1000 % 256);
    		return new Color(red, green, blue);
    	}
    
    	public void setSession(Map<String, Object> session) {
    		this.session = session;
    	}
    
    	public void setServletResponse(HttpServletResponse response) {
    		this.response = response;
    	}
    
    }

    struts.xml

    <package name="comAction" namespace="/" extends="struts-default">
    		
    		<action name="randomCodeAction" class="cn.edu.nwsuaf.comAction.RandomCodeAction">
    		</action>
    </package>

    效果:

  • 相关阅读:
    Reactive Extensions (Rx) 入门(5) —— Rx的事件编程
    Reactive Extensions (Rx) 入门(4) —— Rx的事件编程
    Reactive Extensions (Rx) 入门(3) —— Rx的事件编程
    Reactive Extensions (Rx) 入门(2) —— 安装 Reactive Extensions
    Reactive Extensions (Rx) 入门(1) —— Reactive Extensions 概要
    Xamarin NuGet 缓存包导致 already added : Landroid/support/annotation/AnimRes 问题解决方案
    Android 系统Action大全
    Xamarin Forms 实现发送通知点击跳转
    如何理解灰度发布
    推荐一款分布式微服务框架 Surging
  • 原文地址:https://www.cnblogs.com/jasonhaven/p/7355023.html
Copyright © 2011-2022 走看看