Joke Collection Website - Blessing messages - How to write the JSP verification code?
How to write the JSP verification code?
<%@pagecontentType=\"image/jpeg\"import=\"java.awt.*,java.awt.image.*,java.util.*,javax.imageio.* \" %> <%! //createbysmallnest //email:smallnest@gmail.com //website: www.kuaff.com //Generate random colors ColorgetRandColor(Randomrandom,intfc,intbc)...{ if(fc>255)fc =255; if(bc>255)bc=255; intr=fc+random.nextInt(bc-fc); intg=fc+random.nextInt(bc-fc); intb=fc+random.nextInt(bc-fc ); returnnewColor(r,g,b); } %> <% //Set the page not to cache response.setHeader(\"Pragma\",\"No-cache\"); response.setHeader(\"Cache-Control \",\"no-cache\"); response.setDateHeader(\"Expires\",0); //Set the length and width of the picture inwidth=176,height=30; //Set alternative Chinese characters and eliminate some inappropriate ones Ya's Chinese character Stringbase=\"One of the things is that I am not here. People have come here to go to Dali and say just go. It's going to go down and watch the weather. It's going to be small. You've got it right. There's nothing wrong with it." But the home school only thinks that the master will want to be born with the old and middle school students from the front and then walk very much to see the two uses of her country to move back to what side to do the opposite and some of the people in the mountains are waiting for the development of the work. The thing is to give Chang Shui Jiyi three words to the master Zhili Yanzhi dim sum battle two questions, but the body is really eating and called to live and listen to Ge Da? The real all-rounder is the most light production path of the four enemies, the general vernacular east The seats are as close as being blown away by flowers. The fifth envoy writes about the army. How can we determine the future of cultural fortune? 9. You laugh with me every time. Wan Shao Zhiyi is more serious than the steps at night, even the chariots are heavier, so he fights with horses. He turns into a Taizhi and becomes a society. He is like a scholar. He fights with stones all over the sun. He takes the group to study each six books and thinks about the eight difficulties in Lihe Village. Have you discussed it early? Gen *** Let Xiangyan Jin's book sit and connect Guan Xin Jue Bu Ni Chu Ji Qian Xin Zheng Ling or the teacher agglomerate and run who has the word "Cao Yue" plus feet tight love and other practice formations afraid of the moon green half fire question The seven women who are in charge of singing in the sea are sure to feel accurate. Liang Qing talks about agriculture, ancient times, black advertising, and attracting names. Earth Qingyang follows the changes in history, changes in history, and creates mouths. This treatment must be taken in the north, and the rain wears the inner knowledge, the industry, vegetables, crawls, sleeps, and the shape of the body. Dushu Fangong next room Jinan gun read Sha Sui line Ye Jiankong closed to Zhengcheng Laoluo Qian Tewei Di Shengjiao hot exhibition package song category crescendo several rural calls Xingyin answer Geji old god throne chapter help to receive The order is not to jump into the shore, but to dare to lose it. The top is in a hurry, the forest is in a hurry, the clothes are like newspaper leaves, and the uncle's back is thin\"; [Page] //The length of the alternative Chinese characters intlength=base.length(); //Create a memory image BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //Get the graphics context Graphicsg=image.getGraphics(); //Create an instance of the random class Randomrandom=newRandom(); //Set the image background Color (because it is used as a background, so it is lighter) g.setColor(getRandColor(random,200,250)); g.fillRect(0,0,width,height); //Alternative font String[]fontTypes=...{ \"Song style\",
\"New Song Ti\",\"Hei Ti\",\"Kaiti\",\"official script\"}; intfontTypesLength=fontTypes.length; //Add noise to the background of the picture g.setColor(getRandColor(random,160,200) ); g.setFont(newFont(\"TimesNewRoman\",Font.PLAIN,14)); for(inti=0;i<6;i++)...{ g.drawString(\"****** ***************************************\",0,5*(i+2 )); } //Get the randomly generated authentication code (6 Chinese characters) //Save the generated Chinese character string StringsRand=\"\"; for(inti=0;i<6;i++)...{ intstart= random.nextInt(length); Stringrand=base.substring(start,start+1); sRand+=rand; //Set the font color g.setColor(getRandColor(random,10,150)); //Set the font g.setFont( newFont(fontTypes[random.nextInt(fontTypesLength)],Font.BOLD,18+random.nextInt(6))); ///////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////// Many newbies don’t know much about image verification codes. , today we will take a look at an example. First, write a Servlet class to generate dynamic pictures, as follows: package org.yuanliang; import java.awt.Color; import java.awt.Font; import java.awt.Graphics ; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet. ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class RandomCode extends HttpServlet { private static final long serialVersionUID = 1L; private static int WIDTH = 65;//Set the width of the picture private static int HEIGHT = 22;//Set the height of the picture p
ublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); response.setContentType(\"image/jpeg\"); ServletOutputStream sos = response.getOutputStream(); response.setHeader( \"Pragma\", \"No-cache\"); response.setHeader(\"Cache-Control\", \"no-cache\"); response.setDateHeader(\"Expires\", 0); BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); char[] rands = generateCheckCode(); drawBackground(g); drawRands(g, rands); g.dispose(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, \"JPEG\", bos); byte[] buf = bos.toByteArray(); response.setContentLength(buf.length); sos.write(buf); bos.close(); sos.close(); session.setAttribute(\"randomCode\", new String(rands)); } private void drawBackground(Graphics g) { g.setColor(new Color(0xDCDCDC)); g .fillRect(0, 0, WIDTH, HEIGHT); for (int i = 0; i < 120; i++) { int x = (int) (Math.random() * WIDTH); int y = (int) (Math .random() * HEIGHT); int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random () * 255); g.setColor(new Color(red, green, blue)); g.drawOval(x, y, 1, 0);[Page] } } private void drawRands(Graphics g, char[] rands ) { // g.setColor(Color.BLUE); Random random = new Random(); int red = random.nextInt(110); int green = random.nextInt(50); int blue = random.nextInt(50) ; g.setColor(new C
olor(red, green, blue)); g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18)); g.drawString(\"\" + rands[0], 1, 17); g.drawString(\"\" + rands[1], 16, 15); g.drawString(\"\" + rands[2], 31, 18); g.drawString(\"\" + rands[3 ], 46, 16); } private char[] generateCheckCode() { String chars = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"; char[] rands = new char[4]; for (int i = 0; i < 4; i++) { int rand = (int) (Math.random() * 36); rands[i] = chars.charAt(rand); } return rands; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this. doGet(request, response); } } OK!!! Save it as RandomCode.java, then compile it, generate the RandomCode.class file, and put it in the WEB-INF/classes/org/yuanliang directory; and then in the WEB-INF directory Create web.xml below and add Servlet configuration: jsp verification code generation and refresh About jsp verification code generation, display, verification and refresh: 1: Verification image generation of four-digit data, implemented with servlet 2: Configure servlet in web.xmlli 3 : The picture generated by the servlet is displayed on the page 4: Verification of the verification code input data 5: Refresh of the verification code——————————————————————– 1: four digits The verification picture of the data is generated using a servlet. The servlet code is as follows: import java.io.*; import javax.servlet.http.*; import javax.servlet.*; import java.util.*; import java.awt.* ; import java.awt.image.*; import javax.imageio.*; public class vImage extends HttpServlet { public void init(ServletConfig conf) throws ServletException { super.init(conf); } public void doGet(HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { res.setContentType("image/jpeg"); res.setHeader("Pragma", "No-cache"); res.setHeader("Cache-Control", "no-cache"); res .setDateHeader("Expires", 0); HttpSession ses
sion = req.getSession(); // Create an image in memory int width = 60, height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Get the graphics context Graphics g = image. getGraphics(); // Generate a random class Random random = new Random(); // Set the background color g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // Set the font g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); //Draw a border//g.setColor(new Color()); //g.drawRect(0,0, width-1,height-1); // Randomly generate 155 interference lines to make the authentication code in the image difficult to detect by other programs g.setColor(getRandColor(160, 200)); for (int i = 0; i 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); } } 2: Configure the servlet in web.xmlli: vImage /vimage Note: Pay attention to the position of the element in the web.xml file 3: On the page Display the image generated by the servlet: Note: Add this sentence where the verification code needs to be displayed. The name will be used when refreshing the verification code. 4: Regarding the verification of the input data of the verification code: When the verification code is generated, it is stored in the session, which is the sentence.setAttribute("post_validate_code", sRand) in the servlet; I won’t go into details about the specific verification. 5: Refresh the verification code Reload the verification code function reloadImage(url) { document.form name.pic.src = url; } The first two have time reference ///////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ////////////////////////////////////////////////
- Previous article:I sent 100 1 to 65438, and there was no reply.
- Next article:What anti-drug propaganda copybooks are there in primary schools?
- Related articles
- What is the significance of China's domestic flow and what is the domestic voice?
- Why can't traffic messages remind you?
- Sentences to recover after a breakup
- Oppor9plus suddenly lost its sound. What should I do?
- The first-grade English teacher introduced herself in class.
- Maanshan 2.0 Pre-registration of nucleic acid detection for all employees
- Mobile phones often can't receive information. What happened?
- How to send a message to a circle of friends
- Unicom inquired about the short messages sent by this service.
- How to turn off Xiao Ai's classmates?