Servlet Case 3: verification code function, servlet case 3 Verification Code

Source: Internet
Author: User

Servlet Case 3: verification code function, servlet case 3 Verification Code

Here we will introduce the simple verification code function.

Dynamic Image Generation

 

A simple page:

<! DOCTYPE html> View Code

Simple JS Code to achieve click Verification Code image refresh

 

Verification Code function:

Package demo; import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. graphics2D; import java. awt. image. bufferedImage; import java. io. bufferedReader; import java. io. fileReader; import java. io. IOException; import java. util. arrayList; import java. util. list; import java. util. random; import javax. imageio. imageIO; import javax. servlet. servletException; import javax. servlet. http. httpServ Let; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; /*** Verification Code Generation Program ***/public class CheckImgServlet extends HttpServlet {// Save All idioms in the set private List <String> words = new ArrayList <String> (); @ Override public void init () throws ServletException {// reading the stage, read new_words.txt // read the file from the web project, and use the absolute disk path String path = getServletContext (). getRealPath ("/WEB-INF/new _Words.txt "); try {BufferedReader reader = new BufferedReader (new FileReader (path); String line; while (line = reader. readLine ())! = Null) {words. add (line);} reader. close ();} catch (IOException e) {e. printStackTrace () ;}} public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// disable caching // response. setHeader ("Cache-Control", "no-cache"); // response. setHeader ("Pragma", "no-cache"); // response. setDateHeader ("Expires",-1); int width = 120; int height = 30; // Step 1: Draw a buffer image BufferedImage bufferedImage = new BufferedImage (width, height, bufferedImage. TYPE_INT_RGB); // Step 2. Draw the background color through the Drawing Object Graphics graphics = bufferedImage. getGraphics (); // get the Drawing Object --- paint brush // you must specify a color graphics before drawing any image. setColor (getRandColor (200,250); graphics. fillRect (0, 0, width, height); // draw the border graphics in step 3. setColor (Color. WHITE); graphics. drawRect (0, 0, width-1, height-1); // Step 4: Graphics2D graphics2d = (Graphics2D) graphics; // set the output font graphics2d. setFont (new Font ("", Font. BOLD, 18); Random random = new Random (); // generate random number int index = Random. nextInt (words. size (); String word = words. get (index); // get the idiom // define the x coordinate int x = 10; for (int I = 0; I <word. length (); I ++) {// random color graphics2d. setColor (new Color (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110); // rotate-30 --- 30 degrees int jiaodu = random. nextInt (60)-30; // converts the radian double theta = jiaodu * Math. PI/180; // obtain the alphanumeric char c = word. charAt (I); // Output c to graphics2d. rotate (theta, x, 20); graphics2d. drawString (String. valueOf (c), x, 20); graphics2d. rotate (-theta, x, 20); x + = 30;} // Save the verification code to the session request. getSession (). setAttribute ("checkcode_session", word); // draw the interference Line graphics in step 5. setColor (getRandColor (160,200); int x1; int x2; int y1; int y2; for (int I = 0; I <30; I ++) {x1 = random. nextInt (width); x2 = random. nextInt (12); y1 = random. nextInt (height); y2 = random. nextInt (12); graphics. drawLine (x1, y1, x1 + x2, x2 + y2);} // output the image above to the browser ImageIO graphics. dispose (); // release the resource // write the image to response. imageIO in getOutputStream. write (bufferedImage, "jpg", response. getOutputStream ();} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );} /*** obtain the color of a specific range ** @ param fc * int range parameter 1 * @ param bc * int range parameter 2 * @ return Color */private Color getRandColor (int fc, int bc) {// obtain the Random color 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 );}}
View Code

 

Here is the idiom verification code, will be a lot of idioms stored in the WEB-INF directory of the next text, each line of one can be

 

Add the web. xml configuration file

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>WEB4</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <servlet>    <description></description>    <display-name>CheckImgServlet</display-name>    <servlet-name>CheckImgServlet</servlet-name>    <servlet-class>demo.CheckImgServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>CheckImgServlet</servlet-name>    <url-pattern>/checkImg</url-pattern>  </servlet-mapping></web-app>
View Code

 

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.