login.jsp:
<%@ page language="java" import="java.util.*,com.cn.servlet.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用户登录</title> <script> function changeImg(){ var imgObj=document.getElementById("img"); imgObj.src="CheckCodeServlet?ran="+Math.random(); } </script> </head> <body> <form action="LoginServlet" method="post"> 账 号: <input type="text" name="account" ><br/> 密 码: <input type="password" name="password"><br/> 验证码:<input type="text" name="code" > <img src="CheckCodeServlet" id="img"><a href="javascript:changeImg()">看不清换一张</a><br/> <input type="submit" value="登录"><br/> <span><%=request.getAttribute("Msg")%></span> </form> </body> </html>
CheckCodeServlet:(生成验证码)
package com.cn.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CheckCodeServlet extends HttpServlet { //产生随机的字体 private Font getFont(){ //创建random对象,用于生成随机数 Random random = new Random(); //创建字体数组,用于封装不同字体的Font对象 Font font[] = new Font[5]; font[0] = new Font("Ravie", Font.PLAIN, 24); font[1] = new Font("Antique Olive Compact", Font.PLAIN, 24); font[2] = new Font("Forte", Font.PLAIN, 24); font[3] = new Font("Wide Latin", Font.PLAIN, 24); font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, 24); return font[random.nextInt(5)]; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); OutputStream os = response.getOutputStream(); int width = 83, height = 30; //建立指定宽、高和BufferedImage对象 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics g = image.getGraphics();//将画笔绘制于image上 Color c= g.getColor();//保存当前画笔的颜色 g.fillRect(0, 0, width, height);//填充矩形 char[] ch = "abcdefghigklmnopqrstuvwxyz2345678901".toCharArray();//随机产生字符串 int length = ch.length;//随机字符串长度 String sRand = "";//保存随机产生的字符串 Random random = new Random(); for (int i=0; i<4; i++){ g.setFont(getFont());//设置字体 String rand = new Character(ch[random.nextInt(length)]).toString();//随机生成0~9的数字 sRand += rand; //设置随机颜色 g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255))); g.drawString(rand, 20*i+6, 25); } //产生随机干扰点 for(int i=0; i<20; i++){ int x1 = random.nextInt(width); int y1 = random.nextInt(height); g.drawOval(x1, y1, 2, 2); } g.setColor(c);//重置画笔颜色 g.dispose();//释放此图形的上下文及使用的所有系统资源 request.getSession().setAttribute("safecode", sRand);//验证码记录到session ImageIO.write(image, "JPEG", os);//输出图像到页面 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
LoginServlet:
package com.cn.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet { private String account = "Vito-Yan"; private String password = "12345678"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String acc = request.getParameter("account"); String pwd = request.getParameter("password"); String code = request.getParameter("code"); if(acc.equals(account) && pwd.equals(password)){ HttpSession session = request.getSession(); String sessionCode = (String)session.getAttribute("safecode"); if(!(sessionCode.equals(code))){ request.setAttribute("Msg", "对不起验证码不正确"); request.getRequestDispatcher("login.jsp").forward(request, response); }else{ session.setAttribute("account", account); request.getRequestDispatcher("main.jsp").forward(request, response); } }else{ request.setAttribute("Msg", "对不起账号密码不正确"); request.getRequestDispatcher("login.jsp").forward(request, response); } } }