JSP驗證碼

來源:互聯網
上載者:User

標籤:jsp   驗證碼   

1、login.jsp登陸介面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Login Page</title><script language="javascript">function loadImage(){document.getElementById("randImage").src="image.jsp?"+Math.random();}</script></head><body><form action="validate.jsp" method="post"><table cellspacing="1" cellpadding="3" border="0"><tr><td colspan="2">Please enter your verification code</td></tr><tr><td><input type="text" name="vcode"/></td><td><img src="image.jsp" id="randImage"/></td></tr><tr><td colspan="2"><a href="javascript:loadImage()">Change an image</a></td></tr><tr><td colspan="2"><input type="submit" value="Submit"/></td></tr></table></form></body></html>

2、image.jsp產生驗證碼圖片的jsp檔案

<%@ page language="java"%><%@ page import="java.awt.*,java.awt.image.*,java.util.*" %><%@ page import="java.io.OutputStream,javax.imageio.*"%><%!Color getRandColor(int fc,int bc){if(fc>255){fc=255;}if(bc>255){bc=255;}Random random=new Random();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.addHeader("pragma", "No-cashe");response.addHeader("cashe-control","no-cashe");response.setDateHeader("expires",0);int width=60,height=20;BufferedImage bimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);Graphics g=bimg.getGraphics();g.setColor(getRandColor(200,250));g.fillRect(0, 0, width, height);g.setColor(getRandColor(160,200));Random rand=new Random();for(int i=0;i<200;i++){int x=rand.nextInt(width);int y=rand.nextInt(height);int w=rand.nextInt(12);int h=rand.nextInt(12);g.drawLine(x, y, x+w, y+h);}String srand="";g.setFont(new Font("Times New Roman",Font.PLAIN,18));for(int i=0;i<4;i++){String num=String.valueOf(rand.nextInt(10));srand+=num;g.setColor(new Color(20+rand.nextInt(110),20+rand.nextInt(110),20+rand.nextInt(110)));g.drawString(num, 13*i+6, 16);}session.setAttribute("srand", srand);OutputStream os=response.getOutputStream();ImageIO.write(bimg, "jpeg", os);os.flush();os.close();os=null;g.dispose();//這兩句非常重要,如果沒有會報錯out.clear();out=pageContext.popBody();%>

3、validate.jsp對驗證碼進行驗證

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Validate Page</title></head><body><%String str=(String)session.getAttribute("srand");String incode=request.getParameter("vcode");if(str.equals(incode)){out.println("<font size=\"+3\" color=\"#000\">The verification is right!</font><br/>");out.println("<font size=\"+4\" color=\"#FF0000\">Welcone to the page!</font><br/><hr/>");}else{out.println("<font size=\"+4\" color=\"#FF0000\">"+"Sorry the verification code is not right</font></br><hr>");}out.println("<br/><a href=\"login.jsp\">Back to the login page</a>");%></body></html>

4、結果



JSP驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.