package com.hisoft.ems.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class RandomCodeServlet extends HttpServlet { private static final long serialVersionUID = 1L; // 验证码图片高度 private int width = 60; // 验证码图片宽度 private int height = 20; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); // 设置白色背景 g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // 创建字体,字体的大小应该根据图片的高度来确定 Font font = new Font("Times New Roman", Font.PLAIN, 18); g.setFont(font); // 画边框 g.setColor(Color.black); g.drawRect(0, 0, height - 1, height - 1); // 创建一个随机数生成器 Random random = new Random(); // 随机产生160条干扰线,使图像中的验证码不易被其他程序探测到 g.setColor(Color.gray); for (int i = 0; i < 160; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int x1 = random.nextInt(12); int y1 = random.nextInt(12); g.drawLine(x, y, x + x1, y + y1); } // randomCode用于保存随机产生的验证码,以便于用户登录后进行验证。 StringBuffer randomCode = new StringBuffer(); int red = 0, green = 0, blue = 0; // 随机产生4位数字的验证码 for (int i = 0; i < 4; i++) { // 随机得到产生的验证码数字 String strRand = String.valueOf(random.nextInt(10)); // 随机获得验证码字符,包括0~9,a~z,A~Z // String strRand = this.getRandomChar(); // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。 red = random.nextInt(110); green = random.nextInt(5); blue = random.nextInt(50); // 用随机产生的颜色将验证码绘制到图像中 g.setColor(new Color(red, green, blue)); g.drawString(strRand, 13 * i + 6, 16); // 将4位随机数组合在一起。 randomCode.append(strRand); } // 将4位数字的验证码保存在Session中。 HttpSession session = req.getSession(); session.setAttribute("randomCode", randomCode.toString()); // 禁止图像缓存。 resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setDateHeader("Expires", 0); // 将图像输出到Servlet输出流中。 resp.setContentType("image/jpeg"); ServletOutputStream sos = resp.getOutputStream(); ImageIO.write(buffImg, "jpeg", sos); sos.close(); } // 随机获得字符,包括数字0~9,a~z,A~Z public String getRandomChar() { Random random = new Random(); ArrayList<String> list = new ArrayList<String>(); for (int i = 0; i < 10; i++) {// 0-9 list.add(String.valueOf(48 + i)); } for (int i = 0; i < 26; i++) {// A-Z list.add(String.valueOf(65 + i)); } for (int i = 0; i < 26; i++) {// a-z list.add(String.valueOf(97 + i)); } int index = random.nextInt(list.size()); return list.get(index); } }
/** * 不带验证码的登录框 */ Ext.onReady(function() { // 使用表单提示 Ext.QuickTips.init(); // turn on validation errors beside the field globally Ext.form.Field.prototype.msgTarget = "side"; // 定义一个输入表单 var simple = new Ext.FormPanel({ baseCls : "x-plain", defaultType : "textfield", buttonAlign : 'center', layout:'table', layoutConfig: {columns:3}, items : [{ xtype : 'label', html : '<span style="font-size:12px;line-height:30px;">帐 号:</span>', width: 45, colspan:1 },{ name : "user.loginName", allowBlank : false, blankText : "帐号不能为空", width : 160, colspan: 2 },{ xtype : 'label', html : '<span style="font-size:12px;line-height:30px;">密 码:</span>', width: 45, colspan:1 }, { inputType : "password", name : "user.password", allowBlank : false, blankText : "密码不能为空", width : 160, colspan:2 },{ xtype : 'label', html : '<span style="font-size:12px;line-height:30px;">验证码:</span>', width: 45, colspan:1 }, { name : "user.loginName", allowBlank : false, blankText : "验证码不能为空", width : 50, colspan:1 }, { xtype : 'panel', html : '<a href="login.jsp"><img src="imgcode" alt="看不清?点击换一个"/></a>', colspan:1, width:60 }], buttons : [{ width : 50, text : "<span style='font-size:12px;'>登录</span>", type : "submit", handler : function() { if (simple.form.isValid()) { Ext.MessageBox.show({ title : "请稍等", msg : "正在加载.....", progressText : "", width : 300, progress : true, closable : false, animEl : "loding" }); var f = function(v) { return function() { var i = v / 11; Ext.MessageBox.updateProgress(i, ''); } } for (var i = 1; i < 13; i++) { setTimeout(f(i), i * 150); } // 提交到服务器操作 simple.form.doAction("submit", { url : "Login.action", method : "post", params : "", success : function(form, action) { document.location = 'index.jsp'; Ext.Msg.alert("登录成功!", action.result.message); }, failure : function(form, action) { Ext.Msg .alert('登陆失败', action.result.message); } }); } } }, { width : 50, text : "<span style='font-size:12px;'>重置</span>", handler : function() { // 重置表单 simple.form.reset(); } }] }); // 定义窗体 var _window = new Ext.Window({ title : "<span style='font-size:14px;'>用户登录</span>", layout : "fit", width : 250, height : 170, plain : true, bodyStyle : "padding:10px;padding-bottom:0px;", maximizable : false, resizable : false, closeAction : "close", closable : false, collapsible : true, plain : true, buttonAlign : "center", items : simple }); _window.show() });
package com.hisoft.ems.action; import com.hisoft.ems.model.User; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; private boolean success; private String message; private User user; public String login() { if ("admin".equals(user.getLoginName().trim()) && "admin".equals(user.getPassword().trim())) { this.success = true; this.message = "您的帐号是"+user.getLoginName()+",密码是"+user.getPassword(); } else { this.success = false; this.message = "对不起,未授权用户不能使用本系统"; } return SUCCESS; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }