package com.kettas.action; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; import org.apache.struts.actions.MappingDispatchAction; import com.kettas.biz.UserBiz; import com.kettas.entity.User; import com.kettas.exception.BizException; import com.kettas.exception.LoginFailedException; import com.kettas.exception.SystemException; /* * Title:用户操作相关Action * Author:SecondGroup * Time:2010.1.14 * Version:1.0.0 */ public class UserAction extends MappingDispatchAction{ /* * setter依赖注入 */ private UserBiz ubiz; public UserBiz getUbiz() { return ubiz; } public void setUbiz(UserBiz ubiz) { this.ubiz = ubiz; } /* * 用户登陆 */ public ActionForward login(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){ String name=request.getParameter("name"); String password=request.getParameter("password"); //验证码大小写不敏感 String validateCode=request.getParameter("validateCode").toUpperCase(); //System.out.println("============="+name+"============"+password+"==================="); HttpSession session=request.getSession(true); String vcode=(String) session.getAttribute("validateCode"); if((validateCode.equals(vcode))&&(! vcode.equals(null))){} User user=new User(); try { if((validateCode.equals(vcode))&&(! vcode.equals(null))){ user=ubiz.login(name, password); session.setAttribute("login", user); return mapping.findForward("getAllProductTypes"); }else{ request.setAttribute("validateFailed", "验证码错误"); return mapping.findForward("login"); } } catch (LoginFailedException e) { throw new LoginFailedException(e); } catch (Exception e){ //如果在运行过程中遇到其他未预料到的系统级别异常则定向到错误页面 throw new SystemException(e); } } /* * 用户注册Action */ public ActionForward regist(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){ DynaActionForm daf=(DynaActionForm) form; /* * 从客户端Form获得参数 */ String name=(String) daf.get("name"); String password=(String) daf.get("password"); String address=(String) daf.get("address"); String postCode=(String) daf.get("postCode"); String email=(String) daf.get("email"); String homePhone=(String) daf.get("homePhone"); String cellPhone=(String) daf.get("cellPhone"); String officePhone=(String) daf.get("officePhone"); String question=(String) daf.get("question"); String answer=(String) daf.get("answer"); User user=new User(name,password,address,postCode,email,homePhone,cellPhone,officePhone,question,answer); try { ubiz.regist(user); } catch (Exception e) { throw new BizException(e); } HttpSession session=request.getSession(true); session.setAttribute("login", user); return mapping.findForward("jump");//注册成功之后5秒跳转首页 } /* * 用户修改信息Action */ public ActionForward modifyUser(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){ String idStr=request.getParameter("id"); Integer id=Integer.parseInt(idStr); String name=request.getParameter("name"); String password=request.getParameter("password"); String address=request.getParameter("address"); String postCode=request.getParameter("postCode"); String email=request.getParameter("email"); String homePhone=request.getParameter("homePhone"); String cellPhone=request.getParameter("cellPhone"); String officePhone=request.getParameter("officePhone"); String question=request.getParameter("question"); String answer=request.getParameter("answer"); User user=new User(); try { user = ubiz.getUserById(id); } catch (Exception e) { throw new BizException(e); } user.setName(name); user.setPassword(password); user.setAddress(address); user.setPostCode(postCode); user.setEmail(email); user.setHomePhone(homePhone); user.setCellPhone(cellPhone); user.setOfficePhone(officePhone); user.setQuestion(question); user.setAnswer(answer); //调用biz层方法修改User资料 try { ubiz.modifyUser(user); //提示用户修改成功 request.setAttribute("message", "修改成功!"); } catch (Exception e) { throw new BizException(e); } HttpSession session=request.getSession(true); session.setAttribute("login", user); return mapping.findForward("modifyuser"); } /* * 用户注销登出,销毁session */ public ActionForward logout(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){ String clearsession=request.getParameter("clearsession"); if((clearsession!=null)&&(clearsession.equals("true"))){ HttpSession session=request.getSession(true); session.removeAttribute("login"); session.invalidate(); } return mapping.findForward("getAllProductTypes"); } /* * 找回密码Action */ public ActionForward findPassword(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){ String name=request.getParameter("name"); String answer=request.getParameter("answer"); User user=new User(); String password=""; try { user=ubiz.getUserByName(name); password=ubiz.findPassword(user.getId(), answer); request.setAttribute("password", password); } catch (Exception e) { throw new BizException(e); } return mapping.findForward("findpassword"); } /* * 生成验证码action */ public ActionForward validateCode(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException{ int width=60; int height=20; int codeCount=4; int x=width/(codeCount+1); //int fontHeight=height-2; int codeY=height-4; char[] codeSequence={'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N','O','P','Q','R','S','T', 'U','V','W','X','Y','Z','1','2','3','4','5','6','7','8','9'}; //定义图像缓冲器 BufferedImage bImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics2D graph=bImg.createGraphics(); //创建一个随机数 Random random=new Random(); //设置背景色为灰色 graph.setColor(Color.WHITE); graph.fillRect(0, 0, width, height); //创建几组字体,随机选择 Font font[]=new Font[5]; font[0]=new Font("Ravie",Font.PLAIN,height-2); font[1]=new Font("Antique Olive Compact",Font.PLAIN,height-2); font[2]=new Font("Comic Sans MS",Font.PLAIN,height-2); font[3]=new Font("Forte",Font.PLAIN,height-2); font[4]=new Font("Gill Sans Ultra Bold",Font.PLAIN,height-2); graph.setFont(font[random.nextInt(5)]); //边框 graph.setColor(Color.BLACK); graph.drawRect(0, 0, width-1, height-1); graph.setColor(Color.LIGHT_GRAY); for(int i = 0; i < 100; i++) { int x0 = random.nextInt(width); int y0 = random.nextInt(height); int x1 = random.nextInt(12); int y1 = random.nextInt(12); graph.drawLine(x0, y0, x0 + x1, y0 + y1); } //randomCode用于保存随即产生的验证码,以便之后登陆时进行验证 StringBuffer randomCode=new StringBuffer(); int red=0; int green=0; int blue=0; for(int i=0;i<codeCount;i++){ String str=String.valueOf(codeSequence[random.nextInt(35)]); //red=random.nextInt(255); red=16; //green=random.nextInt(255); green=16; //blue=random.nextInt(255); blue=16; graph.setColor(new Color(red,green,blue)); graph.drawString(str, (i+1)*x, codeY); randomCode.append(str); } HttpSession session=request.getSession(true); session.setAttribute("validateCode", randomCode.toString()); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); ServletOutputStream sos=response.getOutputStream(); ImageIO.write(bImg, "jpeg", sos); sos.close(); return null; } }
最近下载更多
haruto1213 LV1
2023年6月30日
yyyyyyyc LV1
2022年9月21日
qweqqw123 LV2
2021年12月20日
鲁西西 LV1
2021年6月29日
908902058 LV1
2021年6月15日
1973356987 LV13
2021年6月8日
风雨中的孤傲 LV2
2021年5月25日
情绪别致的疯子 LV3
2020年12月20日
嘿嘿嘿123333 LV3
2020年6月26日
林志勇 LV10
2020年5月22日
最近浏览更多
zhangsan_lisi
9月18日
暂无贡献等级
quartz LV8
7月1日
xiao1111 LV3
6月18日
郭宇航裹裹 LV5
6月7日
wangbh1234 LV1
6月6日
2411307984 LV1
5月8日
hhxq0902
4月20日
暂无贡献等级
微信网友_6797846869299200 LV7
1月1日
jiemomo LV12
2023年11月3日
廖业贵 LV18
2023年10月17日