Servlet programming-verification code example, servlet verification code example

Source: Internet
Author: User

Servlet programming-verification code example, servlet verification code example

The basic idea of Servlet programming has been mentioned in the previous article. I will not talk about it here. I would like to share with you an example of using Servlet to implement verification code.

First, we need a Servlet class: ImageServlet. java to generate a verification code and store the verification code in the Session.

Next, we need to configure the web. xml file to add the Servlet

Well, now we can write a login page to display the login. jsp verification code.

Finally, we only need to verify that the verification code in the Session is consistent with the Verification Code submitted by the user, and then create a login_check.jsp to verify the verification code.


Now, a complete Servlet verification code is available. You can test it.

Conclusion: we first need to generate a verification code image through ImageSreevlet on the server side and store the verification code in the session, and then in the web. add the Servlet configuration in xml, and then we can reference the verification code image through the tag on the client, finally, we can compare the verification code entered by the user with the verification code in the session on another page. If you do not understand the code, please discuss it with me.



Compile a Servlet Verification Code

The Code is as follows:
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. io .*;
Import java. awt .*;
Import java. awt. image .*;
Import java. util .*;
Import javax. imageio .*;

/**
* @ Author yeeku. H. lee kongyeeku@163.com
* @ Version 1.0
* <Br> Copyright (C), 2005-2008, yeeku. H. Lee
* <Br> This program is protected by copyright laws.
* <Br> Program Name:
* <Br> Date:
*/
Public class AuthImg extends HttpServlet
{
Private Font mFont = new Font ("Arial Black", Font. PLAIN, 16 );
Public void init () throws ServletException
{
Super. init ();
}
Color getRandColor (int fc, int bc)
{
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 );
}

Public void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
Response. setContentType ("image/jpeg ");

Int width = 100, height = 18;
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );

Graphics g = image. getGraphics ();
Random random =... the remaining full text>

Question about Servlet Verification Code Generation

This is one I made. For your reference, it can run normally:
Package com;

Import java. io. IOException;
Import java. io. PrintWriter;
Import java. io .*;
Import javax. servlet. http .*;
Import javax. servlet .*;
Import java. util. Random;
Import java. awt .*;
Import java. awt. image .*;
Import javax. imageio .*;

Public class ImageServlet extends HttpServlet {

Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("image/jpeg ");
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-control", "no-cache ");
Response. setDateHeader ("Expires", 0 );

OutputStream out = response. getOutputStream ();
Int width = 80, height = 20;
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );

Graphics g = image. getGraphics ();
Random random = new Random ();

G. fillRect (0, 0, width, height );
G. setFont (new Font ("Times New Roman", Font. ITALIC, 18 ));
String sRand = "";
For (int I = 0; I <4; I ++ ){
String rand = String. valueOf (random. nextInt (10 ));
SRand + = rand;
G. setColor (new Color (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110 )));
G. drawString (rand, 20 * I + 6, 16 );
}
Request. getSession (). setAttribute ("valCode", sRand );
G. dispose ();
ImageIO. write (image, "JPEG", out );
}

Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
... The remaining full text>

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.