Summary of JSP website development: 13th and website development: 13th

Source: Internet
Author: User

Summary of JSP website development: 13th and website development: 13th

After adding the filter class to the previous article, this article briefly introduces how to implement the verification code. What is the role of the verification code? 1. prevent malicious registration. 2. prevent malicious submission. 3. Prevent account theft. In general, the verification code is used to prevent unauthorized operations, however, it should be noted that the verification code is a function that affects the user experience. Therefore, some websites will prompt users to enter the verification code only after the user fails to perform the first operation by setting parameters, this can be said to be an improvement in the verification code. It is useless. Let's start to implement our verification code.

1. Implementation:

  

2. index. jsp:

Some JavaScript operations are added during the interface design. If you have any questions, leave a message.

<% @ 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"> 

3. Generate the verification code select:

public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        BufferedImage bi = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);        Graphics g = bi.getGraphics();        Color color = new Color(200, 130, 255);        g.setColor(color);        g.fillRect(0, 0, 68, 22);                char [] ch = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890zxcvbnmlkjhgfdsaqwertyuiop".toCharArray();        Random r = new Random();        StringBuffer buf = new StringBuffer();        int len = 0;        for(int i=0; i<4; i++){            len = r.nextInt(ch.length);            g.setColor(new Color(r.nextInt(150), r.nextInt(255), r.nextInt(200)));            g.drawString(ch[len]+"", (i*15)+3, 18);            buf.append(ch[len]);        }        String code = buf.toString();        request.getSession().setAttribute("code", code);        ImageIO.write(bi, "JPG", response.getOutputStream());    }

4. Verify selelct:

Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); response. setContentType ("text/html"); PrintWriter out = response. getWriter (); String code = request. getParameter ("code"); String imgCode = (String) request. getSession (). getAttribute ("code"); if (imgCode. equalsIgnoreCase (code) {out. println ("Logon successful");} else {out. println ("Logon Failed ");}}

Here, we have achieved the verification code effect. For the verification code that generates the Chinese character verification code and the mathematical expression, we will study it on our own. The ideas have been introduced to you, and the summary of JSP is as follows, at present, it is almost impossible to upload and download files. It is estimated that we will share it with you in the following days. Of course, if you can implement it, I hope you can give more advice.

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.