You can use the url rewriting mechanism to change the verification code.

Source: Internet
Author: User

You can use the url rewriting mechanism to change the verification code.

URL rewriting mechanism: to ensure that the address of a url is unique, you can transmit different parameters to the server each time.

The header information of a data request can be analyzed as follows: the header information includes the http protocol, IP address, port number, project name, and request parameter list. The resources to be accessed do not change, you can only change the parameter connection table.

Here, the verification code is refreshed by changing the value of the parameter list.

Detailed code implementation:

<% @ Page import = "javax. imageio. imageIO "%> <% @ page import =" java. awt. font "%> <% @ page import =" java. awt. color "%> <% @ page import =" java. awt. graphics "%> <% @ page import =" java. awt. image. bufferedImage "%> <% @ page contentType =" image/jpeg "language =" java "import =" java. util. * "pageEncoding =" UTF-8 "%> <% int w = 100; int h = 30; BufferedImage bi = new BufferedImage (w, h, BufferedImage. TYPE_INT_RGB); Graphics g = bi. getGraphics (); Color c = g. getColor (); Font f = g. getFont (); Random r = new Random (); Color bg = new Color (150 + r. nextInt (100), 150 + r. nextInt (100), 150 + r. nextInt (100); g. setColor (bg); g. fillRect (0, 0, w, h); String code = ""; for (int I = 1; I <= 4; I ++) {int num = r. nextInt (10); code = code + num; Color num_c = new Color (r. nextInt (1, 150), r. nextInt (1, 150), r. nextInt (150); g. setColor (num_c); g. drawString (String. valueOf (num), 20 * I, h/2);} request. getSession (). setAttribute ("code", code); // clear the cache response. setHeader ("pragma", "bo-cache"); response. setHeader ("cache-control", "bo-cache"); response. addDateHeader ("expires", 0); ImageIO. write (bi, "jpeg", response. getOutputStream (); out. close (); %>

Add logon page:

<% @ Page contentType = "text/html; charset = UTF-8" language = "java" import = "java. util. * "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"> 

Using Time changes, a timestamp is generated each time, and parameters are passed to the requested url to rewrite the url, thus implementing the refresh function of another one.

Summary

The above section describes how to use the rewrite url mechanism to change the verification code. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.