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>

    效果:

  • 相关阅读:
    使用submit异步提交,阻止表单默认提交
    EasyDarwin流媒体服务器实现关键帧推送功能
    EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器
    EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器
    EasyDarwin流媒体云平台架构
    EasyDarwin流媒体云平台架构
    EasyDarwin流媒体服务器高性能优化方向
    EasyDarwin流媒体服务器高性能优化方向
    我们将要建立的EasyDarwin开源社区
    我们将要建立的EasyDarwin开源社区
  • 原文地址:https://www.cnblogs.com/jasonhaven/p/7355023.html
Copyright © 2011-2022 走看看