JSP + servlet generate verification code and verify

Source: Internet
Author: User

JSP + servlet generate verification code and verify



The basic process for generating a verification code is:

<% @ Page language = java import = java. util. * pageEncoding = UTF-8 %> <% @ page import = java. awt. *, java. awt. image. bufferedImage, javax. imageio. imageIO %> <% response. setHeader (Cache-Control, no-cache); int width = 60, height = 20;
  BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); // obtain the paint brush g Graphics g = image from the bufferdImage object. getGraphics (); // The pen fills the rectangular g. setColor (new Color (200,200,200); g. fillRect (0, 0, width, height); // generate Random number Random rd = new Random (); int rdNum = rd. nextInt (8999) + 1000; String rdstr = String. valueOf (rdNum); // puts the random number into the session. setAttribute (rdStr, rdstr); // paint brush g draws a random number g. setColor (Color. BLUE); g. setFont (new Font (, Font. PLAIN, 20); g. drawString (rdstr, 10, 20); // The coordinates here are the baseline of a character in the lower left corner. // draw 100 random points g. setColor (Color. green); for (int I = 0; I <100; I ++) {int x = rd. nextInt (width); int y = rd. nextInt (height); g. drawOval (x, y, 1, 1);} // use the static function write of ImageIO to output the image ImageIO. write (image, JPEG, response. getOutputStream (); out. clear (); out = pageContext. popBody (); %>

The basic idea of drawing is:


  <% @ Page language = java import = java. util. * pageEncoding = UTF-8 %> <% String path = request. getContextPath (); String basePath = request. getScheme () +: // + request. getServerName () +: + request. getServerPort () + path +/; %>
> <script type=text/javascript> function refresh(){ document.loginform.imgcode.src=http://blog.csdn.net/tuke_tuke/article/details/codes.jsp; } </script>


  Package servlets; import java. io. IOException; import java. io. printWriter; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; public class codeServlet extends HttpServlet {/*** The doPost method of the servlet. ** This method is called when a form Has its tag value method equals to post. ** @ param request the request send by the client to the server * @ param response the response send by the server to the client * @ throws ServletException if an error occurred * @ throws IOException if an error occurred */public void doPost (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {// get the form verification code String cod E = request. getParameter (yanzhengma); // obtain the verification code HttpSession session = request in the session. getSession (); String randstr = (String) session. getAttribute (rdStr); response. setCharacterEncoding (gb2312); // respons is set to the Chinese encoding PrintWriter out = response. getWriter (); // get the output writer if (code. equals (randstr) {// The entered verification code is out of comparison with the verification code saved by the session. println (congratulations, the verification code is correct !);} Else {// response. sendRedirect (/code/back. jsp); out. println (Verification code error !!); }}}

The input result is:

 

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.