Dynamic JSP verification code generation method,
To prevent malicious login, the server often needs to dynamically generate a verification code and store it in the range of the session, and finally return it to the client as an image.
The following code implements the function: Write a JSP page, dynamically generate a verification code, store it within the scope of the session, and return it to the client as an image.
Write another JSP page and reference the verification code generated on this JSP page;
The authen. jsp code is as follows:
<% @ Page import = "java. awt. *, java. awt. image. *, java. util. *, com.sun.image.codec.jpeg. *" %> <%! // Random Color variation range generated based on the provided AB Color getColor (int a, int B) {int n = B-a; Random rd = new Random (); int cr = a + rd. nextInt (n); int cg = a + rd. nextInt (n); int cb = a + rd. nextInt (n); return new Color (cr, cg, cb) ;}%><%// the following three lines cancel the client browser cache verification code function response. setHeader ("Pragma", "No-cache"); response. setHeader ("Cache-Control", "no-cache"); response. setDateHeader ("Expires", 0); int width = 60, height = 20; // generate an image in the memory BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); Graphics g = image. getGraphics (); Random random = new Random (); g. setColor (getColor (200,250); g. fillRect (0, 0, width, height); g. setFont (new Font ("Times New Roman", Font. BOLD, 18); g. setColor (getColor (160,200); for (int I = 0; I <160; I ++) {int x = random. nextInt (width); int y = random. nextInt (height); int xl = random. nextInt (12); int yl = random. nextInt (12); g. drawLine (x, y, x + xl, y + yl);} String number = String. valueOf (1000 + random. nextInt (8999); String name = request. getParameter ("name"); session. setAttribute (name, number); g. setColor (getColor (20,130); int x = (int) (width * 0.2); int y = (int) (height * 0.8); g. drawString (number, x, y); g. dispose (); required imageencoder encoder = required codec. createJPEGEncoder (response. getOutputStream (); encoder. encode (image); out. close (); %>
Create a test. jsp page and call the verification code:
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
The client cache is canceled on both pages. This is because, in some other browsers, for example, when using the IE browser, images are first cached, when a request is made again, the system will check whether there is an existing image in the memory. If yes, the request will not exist. This will cause the failure of refreshing the verification code. Therefore, the browser should not read the cached image, you need to cancel the cache.
The above is all the content of this article, hoping to help you learn.
Articles you may be interested in:
- JSP color Verification Code
- How to generate a page verification code using Jsp [Code]
- Example code of JSP color Verification Code
- Jsp Verification Code Generation
- A simple example of jsp verification code implementation
- Demo of the verification code generated by referencing servlet in jsp
- JSP dynamically generated verification codes are stored within the scope of the session
- Simple JSP Verification Code Generation Method