页面随机验证码的实现
index.jsp:
<%@ page contentType="text/html;charset=utf-8"%><%@ page language="java" import="java.sql.*" errorPage=""%><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>用户登录</title><!-- 此处需要引入JQuery的包-->?<script type='text/javascript' src='js/jquery-latest.js'></script><script language="javascript">var murl="<%=request.getContextPath()%>/ValidateServlet";alert(2);function loadimage(){alert(1);document.getElementById("randImage").src = "image.jsp?"+Math.random();}//从ValidateServlet.java返回验证的结果是否正确function check(){alert("check()");var num=document.getElementById("num").value;alert(num);$.post(murl,{num:num},function(data){alert(data);});}</script></head><body><table width="256" border="0" cellpadding="0" cellspacing="0"><!--DWLayoutTable--><form name="loginForm"><tr><td width="118" height="22" valign="middle" align="center"><input type="text" id="num" size="15"></td><td width="138" valign="middle" align="center"><img name="randImage" id="randImage" src="image.jsp" width="60"height="20" border="1" align="absmiddle"></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><a href="javascript:loadimage();"><font class=pt95>看不清点我</font></a></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><input type="button" onclick="javascript:check()" name="login"value="提交"></td></tr></form></table></body></html>
?
image.jsp:
<%@ page contentType="image/jpeg"import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"pageEncoding="utf-8"%><%@ page import="java.io.OutputStream" %><%!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);}%><%try{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);OutputStream os=response.getOutputStream();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);}// 将认证码存入SESSIONsession.setAttribute("rand",sRand);g.dispose();ImageIO.write(image, "JPEG", os);os.flush();os.close();os=null;response.flushBuffer();out.clear();out = pageContext.pushBody();}catch(IllegalStateException e){e.printStackTrace();}%>
?
ValidateServlet.java:
import java.io.IOException;import java.io.PrintWriter;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 ValidateServlet extends HttpServlet {/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();HttpSession session=request.getSession();String rand = (String)session.getAttribute("rand"); //得到用户输入的验证码? String input = request.getParameter("num");if(rand.equals(input)){out.print("success");} else{out.print("error");}}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doGet(request, response);}}?
?
?
?
?