jsp页面验证码完整实例
YPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
ç°å¨å¨servletä¸ç»å¶ä¸ä¸ªéªè¯ä»£ç çå¾çï¼è®©å®æ¾ç¤ºå¨é¡µé¢ä¸ï¼ç¶å使ç¨jsæ¹æ³å·æ°éªè¯ä»£ç ï¼ä¸ææ¯ç±ç«ææ¯é¢éå°ç¼ä¸ºå¤§å®¶å¸¦æ¥çjsp页é¢éªè¯ç å®æ´å®ä¾ï¼æå ´è¶£çæåå¯ä»¥åèä¸æã
项ç®ç»æå¦ä¸ï¼MyEclipseä¸æ°å»ºä¸ä¸ªWeb Projectï¼ååservlet
1ãsrcä¸newä¸ä¸ªservletç±»
package com.servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class IdentityServlet extends HttpServlet { public static final char[] chars={'2','3','4','5','6','7','8','9','A'};//èªå®ä¹éªè¯ç æ± public static Random random=new Random();ãã//éæºæ° public static String getRandomString(){ãã//è·å6ä½éæºæ°ï¼æ¾å¨å¾çé StringBuffer buffer=new StringBuffer(); for(int i=0;i */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * 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("image/jpeg");ãã//设置è¾åºç±»å String randomString = getRandomString();ãã//éæºå符串 request.getSession(true).setAttribute("randomString", randomString);//æ¾å°sessioné int width=100;ãã//å¾ç宽度 int height=30;ãã//å¾çé«åº¦ Color color=getRandomColor();ãã//éæºé¢è²ï¼ç¨äºèæ¯è² Color reverse=getReverseColor(color);//åè²ï¼ç¨äºåæ¯è² //å建ä¸ä¸ªå½©è²å¾ç BufferedImage bi=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g=bi.createGraphics();ãã//ç»å¾å¯¹è±¡ g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));//设置åä½ g.setColor(color);//设置é¢è² g.fillRect(0, 0, width, height);//ç»å¶èæ¯ g.setColor(reverse); g.drawString(randomString, 18, 20);//ç»å¶éæºå符 for(int i=0,n=random.nextInt(100);i* * 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 { doGet(request, response); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
2ãweb.xmlï¼ä¼èªå¨çæservletåservlet-mappingçé ç½®
Â
IdentityServlet com.servlet.IdentityServlet IdentityServlet /servlet/IdentityServlet index.jsp
3ãWebRootä¸æ°å»ºä¸ä¸ªhtmlï¼å±ç¤ºéªè¯ç
Insert title here
å¯å¨Tomcatï¼è¾å ¥ç½åï¼http://localhost:8080/servlet/identity.htmlï¼ææå¦ä¸ï¼
ç¹å»âæ¢ä¸ªå¾ç'ï¼ä¼çææ°çéªè¯ç ã
以ä¸è¿äºæ¯ç±ç«ææ¯é¢éå°ç¼ç»å¤§å®¶ä»ç»çjsp页é¢éªè¯ç å®æ´å®ä¾ï¼çå®äºè¿ç¯æç« ï¼ç¸ä¿¡å¤§å®¶é½äºè§£äºï¼æ´å¤çä¸ä¸èµè®¯å°½å¨js.aizhan.comå§ï¼