JSP login verification code and jsp Verification Code

Source: Internet
Author: User

JSP login verification code and jsp Verification Code

1. Set the input option and verification code on the JSP page

<Form action = login. do "method =" post ">
<Div class = "line_1">
<Span class = "line_0000span"> member logon </span>
<Input type = "text" class = "form-control line_2_input" id = "exampleInputAmount" placeholder = "mobile phone number" name = "username">
</Div>
<Div class = "line_3">
<Input type = "password" class = "form-control line_3_input" id = "exampleInputPassword3" placeholder = "password" name = "password">
</Div>
<Div class = "line_4">
<Input type = "text" placeholder = "Verification code" name = "code">

</Div>
</Form>

2. Set JS methods in login. jsp

<Script type = "text/javascript">

Function refreshCode (){
Var codeImg = document. getElementById ("code ");
Var rand = Math. random ();
CodeImg. src = "code. do? Random = "+ rand;
}

</Script>

3. Configure the XML file

 

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>D5.10_yanzhengma</display-name>  <servlet>      <servlet-name>code.do</servlet-name>      <servlet-class>com.phone1000.lq.yanzheng.CodeServlet</servlet-class>  </servlet>  <servlet-mapping>      <servlet-name> code.do</servlet-name>      <url-pattern>/code.do</url-pattern>  </servlet-mapping>  </web-app>
View Code

 

4. The servlet code is as follows:

 

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;/***** @ author SCSI **/public class CodeServlet extends HttpServlet {public static final int NUMBER = 5; //, l, it is easy to confuse, so remove these Characters String strs = "Jun June 11th, 1234"; @ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// create a template size BufferedImage bufferImage = new BufferedImage (100, 40, BufferedImage. TYPE_INT_RGB); // creates a brush Graphics graphics = bufferImage. getGraphics (); // adjust the paint brush color graphics. setColor (Color. green); // set the initial position of the paint brush and the size of the printable area graphics. fillRect (0, 0,100, 40); int size = strs. length (); char [] codes = new char [NUMBER]; Random random = new Random (); graphics. setFont (new Font ("", Font. PLAIN, 16); for (int I = 0; I <NUMBER; I ++) {int index = random. nextInt (size); Character c = strs. charAt (index); codes [I] = c; graphics. setColor (Color. BLUE); // draw a single character size Position graphics. drawString (c. toString (), I * 20, 30);} // Save the generated verification code string in the session req. getSession (). setAttribute ("code", new String (codes); // reclaim graphics resources. dispose (); // display the image and the ImageIO format. write (bufferImage, "png", resp. getOutputStream ();} @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet (req, resp );}}
View Code

In actual use, import the tomcat server. The test version is Apache Tomcat v8.0.

 

 

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.