81,092
社区成员
发帖
与我相关
我的任务
分享
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;
}
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>
session.removeAttribute(attributeName);
session.setAttribute("scode",“密码”);
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);
}
<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里面的值进行对比
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);
我这个是有的,这个是输入的吧 怎么去获取它生成的那个验证码呢