Simple Maven Web project verification code and mavenweb Verification Code
I. Use Maven commands to create a Web project
The CREATE Command is as follows:
mvn archetype:create -DgroupId=com.gao.web -DartifactId= VerifyCode -DarchetypeArtifactId=maven-archetype-webapp
- First, configure the maven environment. The previous blog mentioned the environment configuration problem. Then, run the preceding command in the command line.
- Run the following command to convert the generated maven project to a project that elipse can recognize.
mvn -Dwtpversion=1.0 eclipse:eclipse
Ii. Start to write the code for generating the verification code.
- Import the project to elipse first
The Servlet code for generating the verification code is as follows:
Package com. niu. gao; import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. image. bufferedImage; import java. io. IOException; import java. util. random; import javax. imageio. imageIO; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse;/*** @ Description: (generate the verification code Servlet) * @ author: relieved * @ date: */public class GenerateCodeServlet extends HttpServlet {/*** serialVersionUID */private static final long serialVersionUID = 1L; @ Override protected void doGet (HttpServletRequest req, javasresp) throws handle, IOException {// The first parameter of the image data buffer constructor is width, the second parameter is height, and the third parameter is image type BufferedImage bi = new BufferedImage (100, 25, BufferedImage. TYPE_INT_RGB); // draw the image Graphics grap = bi. getGraphics (); // set the Font f = new Font ("", Font. ITALIC, 20); grap. setFont (f); // background Color color Color = new Color (200,160,255); // set the background color grap. setColor (color); grap. fillRect (0, 0,100, 25); // coordinate the width and height of the image. // The Verification Code content is char [] content = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ". toCharArray (); Random ran = new Random (); // obtain the content of the combined verification code according to the Random number int len = content. length, index; StringBuffer generateCode = new StringBuffer (); // store the randomly generated Verification Code // generate a 6-digit verification code for (int I = 0; I <6; I ++) {index = ran. nextInt (len); // set the verification code color grap. setColor (new Color (ran. nextInt (100), ran. nextInt (180), ran. nextInt (255); // draw the character on the canvas grap. drawString (content [index] + "", I * 16 + 3, 19); generateCode. append (content [index]);} // place the generated verification code in the session for later verification using req. getSession (). setAttribute ("verifyCode", generateCode. toString (); // output the generated Verification Code image to ImageIO. write (bi, "PNG", resp. getOutputStream ();} @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this. doGet (req, resp );}}
The web. xml file configuration is as follows:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app> <display-name>VerifyCode</display-name> <servlet> <servlet-name>generateCode</servlet-name> <servlet-class>com.niu.gao.GenerateCodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>generateCode</servlet-name> <url-pattern>/generate/verifyCode.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
The jsp page is as follows:
<% @ Page 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">