Home>

Two classes are used here,One for verification code implementation,One validates the input for the background servlet:

codeutil.java -------- Verification code implementation:

package util;
import java.awt.color;
import java.awt.font;
import java.awt.graphics;
import java.awt.image.bufferedimage;
import java.io.ioexception;
import java.io.outputstream;
import java.io.printwriter;
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;
import javax.servlet.http.httpsession;
import javax.servlet.jsp.pagecontext;
import javax.servlet.jsp.tagext.bodycontent;
import com.sun.image.codec.jpeg.jpegcodec;
import com.sun.image.codec.jpeg.jpegimagedecoder;
import com.sun.image.codec.jpeg.jpegimageencoder;
public class codeutil extends httpservlet {
 public void service (httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  //system.out.println("hahaha ");
  response.setcontenttype ("image/jpeg");
  //Set page not cached
  response.setheader ("pragma", "no-cache");
  response.setheader ("cache-control", "no-cache");
  response.setdateheader ("expires", 0);
  //Create an image in memory
  int width=90;
  int height=35;
  bufferedimage image=new bufferedimage (width, height, bufferedimage.type_int_rgb);
  //Get graphics context
  graphics g=image.getgraphics ();
  //random class
  random random=new random ();
  //Set the background
  g.setcolor (getrandcolor (200, 250));
  g.fillrect (0, 0, width, height);
  //Set the font
  g.setfont (new font ("times new roman", font.plain, 30));
  //random interference line
  g.setcolor (getrandcolor (160, 200));
  for (int i=0;i<100;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);
  }
  //randomly generated 4 digit verification code
  string [] codes={"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n "," o "," p "," q "," r "," s "," t "," u "," v "," w "," x "," y "," z ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m "," n "," o "," p "," q "," r "," s "," t "," u "," v "," w "," x "," y ", "z"};
  string code="";
  for (int i=0;i&4;i ++) {
   string str=codes [random.nextint (codes.length)];
   code +=str;
   //Display the authentication code in the image
   g.setcolor (new color (50 + random.nextint (110), 20 + random.nextint (110), 30 + random.nextint (110)));
   //The color of the call function is the same,May be because the seeds are too close,So it can only be generated directly
   g.drawstring (str, 18 * i +13, 27);//text spacing * i + distance to the left,Top margin
  }
  httpsession session=request.getsession ();
  //Store the authentication code in the session
  session.setattribute ("code", code);
  //image takes effect
  g.dispose ();
  //output image to page
  imageio.write (image, "jpeg", response.getoutputstream ());
  //plus the following code,Java.lang.illegalstateexception:getoutputstream () has already been called ..........
  response.getoutputstream (). flush ();
  response.getoutputstream (). close ();
  response.flushbuffer ();
 }
 //Get random colors
 private 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);
  }
}

checkcodeutil.java ----------- User input validation function

package util;
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 checkcodeutil extends httpservlet {
 public void service (httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  request.setcharacterencoding ("utf-8");
  response.setcontenttype ("text/html;charset=utf-8");
  printwriter out=response.getwriter ();
  //Get verification code from session
  httpsession session=request.getsession ();
  string code=session.getattribute ("code"). tostring ();
  system.out.println (code);
  //Get user input verification code
  string input=request.getparameter ("code");
  system.out.println (input);
  if (code.equalsignorecase (input)) {
   //forward data
   request.setattribute ("result", "true");
   request.getrequestdispatcher ("test/regist.jsp"). forward (request, response);
//response.sendredirect (request.getcontextpath () + "/regist/regist.jsp");
  } else {
   request.setattribute ("result", "false");
   request.getrequestdispatcher ("test/regist.jsp"). forward (request, response);
  }
 }
}

Test page effect:

Test page code:

<%@page session="true" pageencoding="utf-8" contenttype="text/html;charset=utf-8"%>
<! Doctype html public "-//w3c //dtd html 4.01 transitional //en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <title>insert title here</title>
  <script>
   function showresult1 () {
   alert ("Entered correctly");
   }
   function showresult2 () {
   alert ("Input error");
   }
  </script>
 </head>
 <%
  response.setcontenttype ("text/html;charset=utf-8");
  request.setcharacterencoding ("utf-8");
  string path=request.getcontextpath ();
  if (request.getattribute ("result")!=null) {
  string result=request.getattribute ("result"). tostring ();
  out.print (result);
  if (result.equals ("true")) {
   out.print ("true");
   out.print ("<script>showresult1 ();<script>");
  } else {
   out.print ("<script>showresult2 ();<script>");
  }
  }
 %>
  <body>
  <br>Verification Code Demo
  <form action="<%= path%>/checkcodeutil" method="post">
  <input type="text" size="10" name="code" />
  <img src="<%= path%>/codeutil" />
  <a href="javascript :;" onclick="document.
   getelementbyid ("img"). src="<%= path%>/codeutil"
  <input type="submit" value="submit">
  </form>
 </body>
</html>
jsp
  • Previous Learn with me var pre-parse and function declaration promotion
  • Next Nginx server installation and some basic configuration summary