写好了验证码的生成了怎么调用啊

Jwenr 2014-10-23 03:00:07
写好了验证码的生成了怎么调用去验证啊,网上有好多其他的实例,但使用的是etmvc,还不太了解,在线等大神,这是写好的,不知道怎么在另一个Controller里怎么调用验证,可以的话jsp的调用也说明下,谢谢
public void execute(HttpServletResponse response,HttpSession session) throws Exception{
//0.创建空白图片
BufferedImage image = new BufferedImage(100,30,BufferedImage.TYPE_INT_RGB);
//1.获取图片画笔
Graphics g = image.getGraphics();
Random r = new Random();
//2.设置画笔颜色
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
//3.绘制矩形的背景
g.fillRect(0, 0, 100, 30);
//4.调用自定义的方法,获取长度为5的字母数字组合的字符串
String number = getNumber(5);
//将图片字符存入session,用于验证码检查使用
session.setAttribute("scode", number);
g.setColor(new Color(0,0,0));
g.setFont(new Font(null,Font.BOLD,24));
//5.设置颜色字体后,绘制字符串
g.drawString(number, 5, 25);
//6.绘制8条干扰线
for(int i=0;i<8;i++){
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.drawLine(r.nextInt(100), r.nextInt(30), r.nextInt(100), r.nextInt(30));
}
response.setContentType("image/jpeg");
OutputStream ops = response.getOutputStream();
ImageIO.write(image, "jpeg", ops);
ops.close();
}

private String getNumber(int size){
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
String number = "";
Random r = new Random();
for(int i=0;i<size;i++){
number+=str.charAt(r.nextInt(str.length()));
}
return number;
}
...全文
215 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jwenr 2014-10-24
  • 打赏
  • 举报
回复
引用 12 楼 littlebrain4solving 的回复:
[quote=引用 11 楼 qq_20995417 的回复:] [quote=引用 10 楼 littlebrain4solving 的回复:] <input name="code" type="text" class="width70" src="getCode.jsp" /> src里面的这个可以填写你Controller的execute路径就可以了。
额,这貌似只是个输入框吧,我的完整路径是src="${ctx}/sys/GetCode/execute",其他页面调用方法都是可以的,路径应该没问题[/quote] 不好意思,刚才弄错了;应该是这样写,把验证码显示出来! <img alt="看不清楚,换一张" src="${ctx}/sys/GetCode/execute" onclick="reloadcode();" id="code" style="position:relative;top:1px;left:0px;">[/quote] 试过了。显示不出来,是我生成验证码的方式有问题吗
  • 打赏
  • 举报
回复
引用 11 楼 qq_20995417 的回复:
[quote=引用 10 楼 littlebrain4solving 的回复:] <input name="code" type="text" class="width70" src="getCode.jsp" /> src里面的这个可以填写你Controller的execute路径就可以了。
额,这貌似只是个输入框吧,我的完整路径是src="${ctx}/sys/GetCode/execute",其他页面调用方法都是可以的,路径应该没问题[/quote] 不好意思,刚才弄错了;应该是这样写,把验证码显示出来! <img alt="看不清楚,换一张" src="${ctx}/sys/GetCode/execute" onclick="reloadcode();" id="code" style="position:relative;top:1px;left:0px;">
Jwenr 2014-10-24
  • 打赏
  • 举报
回复
引用 10 楼 littlebrain4solving 的回复:
<input name="code" type="text" class="width70" src="getCode.jsp" /> src里面的这个可以填写你Controller的execute路径就可以了。
额,这貌似只是个输入框吧,我的完整路径是src="${ctx}/sys/GetCode/execute",其他页面调用方法都是可以的,路径应该没问题
  • 打赏
  • 举报
回复
<input name="code" type="text" class="width70" src="getCode.jsp" /> src里面的这个可以填写你Controller的execute路径就可以了。
Jwenr 2014-10-24
  • 打赏
  • 举报
回复
引用 2 楼 littlebrain4solving 的回复:
另外一个Controller直接session.getAttribute("scode")获取验证码与输入对比!
我在jsp里怎么去显示图片呀?不用xml配置和注解
function reloadcode()
       {
		
	      var verify=document.getElementById('code');
          verify.setAttribute('src','getCode.jsp?it='+Math.random());
          window.location.href="${ctx}/sys/home/login";
       }
<tr>
                    <td class="login_info" colspan="2">验证码:</td>
                    <td class="width70" ><input name="code" type="text" class="width70" /></td>
                    <td width="92"  style="text-align:center"><img alt="看不清楚,换一张" src="getCode.jsp" onclick="reloadcode();" id="code" style="position:relative;top:1px;left:0px;"></td>
                    
                </tr>
异常异长 2014-10-23
  • 打赏
  • 举报
回复
每次生成新的验证码 都需要 进行一次set set之前建议先清空
session.removeAttribute(attributeName);
  session.setAttribute("scode",“密码”);
放纵的青春 2014-10-23
  • 打赏
  • 举报
回复
·· 你是不是要把验证码生成图片? 你生成图片的时候把验证码存入session 然后把这个存入session的跟用户填写的对比不就行了吗!
Jwenr 2014-10-23
  • 打赏
  • 举报
回复
引用 5 楼 littlebrain4solving 的回复:
session.getAttribute("scode"); 是获取生成的验证码 request.getParameter("scode"); 这个才是获取页面输入的验证码参数 一个是Session一个是request
谢谢你,我给你看下完整的登陆验证方法吧,不是我写的不太懂这些,你能帮忙看下怎么改吗
public JsonView login(UserVO user) throws Exception{
    	Map<String,Object> result = new HashMap<String,Object>();
    	//request.setCharacterEncoding("UTF-8");    	
    	String loginname = getRequest().getParameter("loginname");
    	String loginpwd = getRequest().getParameter("password");
    	
    	//查看验证码
		String scode = (String) session.getAttribute("scode");
		System.out.println(scode);
		
    	System.out.println(loginname);    	
    	System.out.println(loginpwd);
    	Map param=new HashMap();
    	param.put("loginname", loginname);
    	QueryParam qp=QueryParam.getInstance("from UserVO where loginname=:loginname",param);
    	UserVO vo=(UserVO)this.getBasBS().queryOne(qp);
    	
    	if(!user.getCode().toUpperCase().equals(scode.toUpperCase())){
			//验证码错误
	    	result.put("code_error","验证码错误");
			result.put("failure",true);
		}
    	if(vo==null){
    		result.put("msg", "用户名称不存在!");
        	result.put("failure", true);
    	}else{
    	    if (vo.getState()==1){
    	    	result.put("msg", "用户已禁用!");
            	result.put("failure", true);
    	    }
    		if(MD5Util.md5(loginpwd).equals(vo.getPassword())){
    			SessionInfo sessionInfo = new SessionInfo();
    			sessionInfo.setId(vo.getId());
    			sessionInfo.setName(vo.getName());
    			sessionInfo.setLoginname(vo.getLoginname());
    			sessionInfo.setOrgID(Long.parseLong(vo.getOrgid().toString()));
    			sessionInfo.setUsertype(vo.getUsertype());
    			
    			//sessionInfo.setMenus(resourceBS.treeByIBatis(vo.getId(),null));
    			sessionInfo.setResourceList(resourceBS.resourceListByIBatis(vo.getId()));
    			session.setAttribute(GlobalConstant.SESSION_INFO, sessionInfo);
    			
    			result.put("success", true);  
        	}else{
        		result.put("failure", true);
        		result.put("msg", "密码错误!");
        	}
    	}
    	/*
    	for(int i=0;i<list.size();i++){
    		UserVO vo = (UserVO)list.get(i);
    		System.out.println(vo.getOprid());
    		System.out.println(vo.getOprname());
    	}
    	*/
    	
    	/*
    	Iterator it = list.iterator();
    	while(it.hasNext()){					
			UserVO vo = (UserVO)it.next();
			System.out.println(vo.getOprid());
    		System.out.println(vo.getOprname());
		}*/
    	
    	
    		
    	JsonView v = new JsonView(result);
    	v.setContentType("text/html;charset=utf-8");
    	return  v;  //new JsonView(result);
    }
  • 打赏
  • 举报
回复
session.getAttribute("scode"); 是获取生成的验证码 request.getParameter("scode"); 这个才是获取页面输入的验证码参数 一个是Session一个是request
张自强 2014-10-23
  • 打赏
  • 举报
回复
<table style="text-align: center;">
	    			<tr><td colspan="2" style="height: 36px;"><span id="alert_msg" style="color: red"></span></td></tr>
	    			<tr><td style="text-align: right;width: 40%">用户名</td><td style="text-align: left;width: 60%"><input id="username" type="text" size="22" style="border: 1px solid #09c;"/></td></tr>
	    			<tr><td style="text-align: right;width: 40%">密码</td><td style="text-align: left;width: 60%"><input id="password" type="password" size="22" style="border: 1px solid #09c;" /></td></tr>
	    			<tr><td style="text-align: right;width: 40%">验证码</td><td style="text-align: left;width: 60%"> <input id="checkCode" type="text" size="10" style="border: 1px solid #09c;" /> <img style="width: 60px;height: 24px;vertical-align: middle;border: 1px solid #09c;padding: 0px;" src="http://www.tools138.com/checkCode.htm" onclick="this.src='http://www.tools138.com/checkCode.htm?' + new Date().getTime();" alt="点击更换"/> </td></tr>
	    			<tr><td colspan="2"><input style="margin-left: 200px;" type="button" value="登录" onclick="goSubmit();"/></td></tr>
	    		</table>
参考下这个html ,checkcode.htm 这个是Action请求 ,验证的时候跟存在session里面的值进行对比
Jwenr 2014-10-23
  • 打赏
  • 举报
回复
引用 2 楼 littlebrain4solving 的回复:
另外一个Controller直接session.getAttribute("scode")获取验证码与输入对比!
Map<String,Object> result = new HashMap<String,Object>();
    	//request.setCharacterEncoding("UTF-8");    	
    	String loginname = getRequest().getParameter("loginname");
    	String loginpwd = getRequest().getParameter("password");
    	
    	//查看验证码
		String scode = (String) session.getAttribute("scode");
		System.out.println(scode);
		
    	System.out.println(loginname);    	
    	System.out.println(loginpwd);
我这个是有的,这个是输入的吧 怎么去获取它生成的那个验证码呢
  • 打赏
  • 举报
回复
另外一个Controller直接session.getAttribute("scode")获取验证码与输入对比!
  • 打赏
  • 举报
回复
肯定要先存到SESSION里面呀,当他提交的时候你就再从SESSION里面与他输入的验证码进行对比呀!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧