验证码
验证码:
验证码显示----img.jsp
<%@ page contentType="image/jpeg" import="java.awt.*,
java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="UTF-8"%>
<%!
Color getRandColor(int fc,int bc)
{
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}
session.setAttribute("rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
out.clear();
out = pageContext.pushBody();
%>
记住密码 获取reme的值设置自动登录 cookie 的保存时间
记住密码
/**
*
* @Title: active
* @Description: TODO(激活的方法)
* @param: @param request
* @param: @return
* @return: String
* @throws
*/
private String active(HttpServletRequest request) {
// 注册对象
Member vo = new Member();
// 执行提示信息的数据
String msg = null;
String url = null;
String mid = request.getParameter("mid");
String code = request.getParameter("code");
// 判断用户和激活码的值是否匹配
if (VakidateUtil.VakidateNoEmpty(mid) && VakidateUtil.VakidateNoEmpty(code)) {
vo.setMid(mid);
vo.setCode(code);
try {
// 激活地址为:
// http://ip:端口/项目名称/pages/MemberServletfront/active?mid=用户名&code=激活码
// 通过邮件点击以上地址激活
if (ServletFrontFactory.getIMemberServletfrontInstance().active(vo)) {
msg = "激活成功,请登录";
url = "/pages/member_login.jsp";
System.out.println("激活成功");
} else {
msg = "激活失败,请重新激活";
url = "index.jsp";
System.out.println("激活失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
msg = "错误的激活操作 !!!!";
url = "index.jsp";
System.out.println("错误激活");
}
request.setAttribute("msg", msg);
request.setAttribute("url", url);
return "/pages/forward.jsp";
}
/**
*
* @Title: login
* @Description: TODO(登录的方法)
* @param: @param request
* @param: @return
* @return: String
* @throws
*/
public String login(HttpServletRequest request, HttpServletResponse response) {
try {
Member vo = new Member();
// 执行提示信息的数据
String msg = null;
String url = null;
String mid = request.getParameter("mid");
String password = request.getParameter("password");
String code = request.getParameter("code");// 验证码
String rand = (String) request.getSession().getAttribute("rand");
System.out.println(1);
if (VakidateUtil.VakidateNoEmpty(mid) && VakidateUtil.VakidateNoEmpty(code)
&& VakidateUtil.VakidateNoEmpty(password)) {
if (VakidateUtil.Vakidatesame(code, rand)) {
vo.setMid(mid);
vo.setPassword(new MD5Code().MD5Encode(password));
System.out.println(mid + "==" + password + "==" + code + rand);
if (ServletFrontFactory.getIMemberServletfrontInstance().login(vo)) {
request.getSession().setAttribute("mid", mid);
System.out.println(vo.getPhoto() + 2);
request.getSession().setAttribute("photo", vo.getPhoto());
msg = "登陆成功";
url = "index.jsp";
// 存储cookie
if (request.getParameter("reme") != null) {// 选择了复选框(复选框设置value值
// 表示自动保存时间)
int expiry = Integer.parseInt(request.getParameter("reme"));
CookieUtil.save(response, request, "mid", mid + "#" + vo.getPassword(), expiry);
}
} else {
msg = "登录失败未激活,错误的密码或用户名";
url = "/pages/member_login.jsp";
}
} else {
msg = "验证码输入错误,请重新输入";
url = "/pages/member_login.jsp";
}
} else {
msg = "信息输入错误,请重新登录";
url = "/pages/member_login.jsp";
}
request.setAttribute("msg", msg);
request.setAttribute("url", url);
return "/pages/forward.jsp";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "/pages/errors.jsp";
}
package utils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @ClassName: CookieUtil
* @Description:TODO(cookie工具类)
* @author: kiko
* @date: 2018年6月21日 下午6:10:48
*
* @Copyright: 2018
*/
public class CookieUtil {
/**
*
* @Title: findCookie
* @Description: TODO(在cookie集合转换成map集合cookie)
* @param: @param request
* @param: @return
* @return: Cookie
* @throws
*/
public static Map findCookie(HttpServletRequest request) {
Map map = new HashMap();
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
map.put(cookie.getName(), cookie.getValue());
}
}
return map;
}
/**
*
* @Title: save
* @Description: TODO(保存cookie 设置cookie保存时间和名字)
* @param: @param response
* @param: @param name
* @param: @param value
* @param: @param expiry
* @return: void
* @throws
*/
public static void save(HttpServletResponse response, HttpServletRequest request, String name,
String value, int expiry) {
Cookie c = new Cookie(name, value);
c.setMaxAge(expiry);
c.setPath("/");// 设置保存路径
response.addCookie(c);
}
/**
*
* @Title: clear
* @Description: TODO(清除cookie)
* @param: @param response
* @param: @param request
* @return: void
* @throws
*/
public static void clear(HttpServletResponse response, HttpServletRequest request) {
Map map = findCookie(request);
Iterator> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry me = iter.next();
Cookie c = new Cookie(me.getKey(), "");
c.setMaxAge(0);
response.addCookie(c);
}
}
}
PS:
Iterator> iter = map.entrySet().iterator();
//获得map的迭代器,用作遍历map中的每一个键值对