java web登录界面中添加验证码

引言:Java web登录界面中添加验证码

1、前端HTML页面部分

<form οnsubmit="return formcheck();" method="post" class="form-horizontal" id="baseForm" role="form" name="baseForm" enctype="multipart/form-data">
     <div style="margin: 0px auto;width: 400px;">
	  <div class="panel panel-default" style="opacity:0.8">
	     <div class="panel-heading">
		<span class="glyphicon glyphicon-lock"></span> 登陆
	      </div>
	  <div class="panel-body">
	  <div class="form-group">
              <label for="username" class="col-sm-3 control-label"> 用户名:</label>
	      <div class="col-sm-9">
	          <input class="form-control" name="username" value="${username!}"  id="username" placeholder="用户名" />
	      </div>
	      </div>
	      <div class="form-group">
	       <label for="loginPass" class="col-sm-3 control-label"> 密   码:</label>
	          <div class="col-sm-9">
		<input class="form-control" name="password" type="password" placeholder="密码" id="loginPass"/>
		  </div>
	       </div>
	      <div class="form-group">
		<label  class="col-sm-3 control-label">验证码:</label>
		  <div class="col-sm-9">
		     <input class="form-control" style="width:163px"name="checkCode" type="text" id="checkCode"/>
			<img id="identifyingValue" alt="验证码" src="/check.do" style="position: absolute; top: 0px; right: 16px;"><!-- 内部自动发送请求,加载验证码 -->
		     </div>
		</div>
		<div class="form-group last">
		<div class="col-sm-offset-3 col-sm-9">
		<button type="submit" class="btn btn-success btn-sm">登入</button>
		<button type="reset" class="btn btn-success btn-sm">重置</button>
		</div>
		</div>
		</div>
		<div class="panel-footer">
		<#if context.hasMessage??>
		<#list context.errors as error>
		${error}
		</#list> 
		<#list context.infos as info>
		${info}
		</#list>
		</#if>
		</div>
		</div>
		</div>
</form>

2、后端controller代码

public void check(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	        	Long time=System.currentTimeMillis();
		        //通知浏览器不要缓存
	            response.setHeader("Content-type", "text/html;charset=UTF-8");  
				response.setHeader("Expires", "-1");
				response.setHeader("Cache-Control", "no-cache");
				response.setHeader("Pragma", "-1");
				response.setDateHeader("Last-Modified", time);
				response.setDateHeader("Date", time);
				response.setDateHeader("Expires", 0);
			//	response.getWriter().write(  
			  //              "恭喜您登录成功,将在3秒后跳转到首页,如果没有跳转,请点击<a href=''>超链接</a>");  
				int height=33;
				int width=90;
				//得到一个内存图像BufferedImage
				BufferedImage img=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
				//得到一个画笔
				Graphics g=img.getGraphics();
				//画边框drawRect绘制指定矩形的边框。
				g.drawRect(0, 0, width, height);
				//填充颜色
				g.setColor(Color.gray);
				g.fillRect(1, 1, width-2, height-2);
				//画干扰线
				g.setColor(Color.BLACK);
				Random r=new Random();
				for(int i=0;i<20;i++)
				g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
				//生成随机数字
				g.setColor(Color.RED);
				g.setFont(new Font("微软雅黑", Font.BOLD|Font.ITALIC, 20));//BOLD加粗,ITALIC斜体
				int d=15;
				StringBuffer sb=new StringBuffer();//可变字符串的利用
				for(int j=0;j<4;j++){
					String code=r.nextInt(10)+"";
					sb.append(code);
					g.drawString(code+"", d, 20);
					d+=20;
				}
				//将验证码输入到session中,用来验证
				request.getSession().setAttribute("checkcode", sb.toString());
				//输出打web页面
				ImageIO.write(img, "jpg", response.getOutputStream());
	}
展示结果



  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伏特加的滋味

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值