Manually create Java verification code, the specific content is as follows
Composition of web application verification code:
(1) Input box
(2) Picture showing verification code
The process of making verification code:
The container that generates the verification code uses j2ee's servlet
Classes required to generate images:
(1) BufferedImage image data buffer
(2) Graphic drawing pictures
(3) Color gets color
(4) Random generates random numbers
(5) ImageIO input image
Specific implementation process:
(1) Define a Servlet to generate verification code
(2) Determine the BufferedImage object, the main function is to create an image buffer as a temporary container for the image.
(3) Obtain the Graphic object, the "background" of the drawing, and understand it as "canvas"
(4) Generate random numbers through Random to create verification information
(5) Through Graphic operations, draw specific drawings
(6) The information is stored in the session
(7) Use ImageIO to output the generated image, and pass the image to the foreground by setting the out parameter in the write() method of ImageIO, response.getOutputStream().
(8) Make a servlet for verification and extract the data in the session for verification (the ajax asynchronous method is used here)
Specific code implementation (Web-side verification code example)
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>java verification code</title><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script></head><body> Verification code input: <input type="text" name="checkcode"> <img id="codeimg" src="ImageServlet"> <a href="javascript:reloadCode();">Can't see clearly? </a><br/> <input type="submit" value="submit"> <div id="info" style="color:red;"></div> <script type="text/javascript"> $(function(){ //ajax asynchronously transfer verification code to the background $("input[type=submit]").click(function(){ $.post("CheckCode", {"code":$("input[name=checkcode]").val()}, function(data,textStatus) { console.log(textStatus); $("#info").html(data); },"text"); }); }) //js refresh, re-request the page, and obtain a new verification code function reloadCode(){ var time = new Date().getTime();//Create different times $("#codeimg").attr("src","ImageServlet?time="+time);//Request re-} </script> </body></html>ImageServlet class (verification code generation part)
@WebServlet("/ImageServlet")public class ImageServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ImageServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub BufferedImage bimg = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB); Graphics g = bimg.getGraphics(); Color color = new Color(200,151,255);//Color generation g.setColor(color); g.fillRect(0, 0, 68, 22); char[] ch="ABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789".toCharArray();//Get an array containing letters and numbers, and then randomly get the characters from it Random random = new Random(); //Create a random number int len = ch.length,index; StringBuffer sBuffer = new StringBuffer(); //Loop generates 4 random characters for(int i = 0 ;i<4;i++) { index = random.nextInt(len);//Create a random letter and number g.setColor(new Color(random.nextInt(88),random.nextInt(120),random.nextInt(90)));//Create a random color g.drawString(ch[index]+"", (i)*15+3, 18); sBuffer.append(ch[index]);//Put the randomly obtained characters in the buffer series} request.getSession().setAttribute("newCode", sBuffer.toString());//Save in session for subsequent verification ImageIO.write(bimg, "JPG", response.getOutputStream());//Output the image through ImageIO and pass it to the foreground} /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}CheckCode (verification code verification servlet)
@WebServlet("/CheckCode")public class CheckCode extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CheckCode() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setCharacterEncoding("utf-8"); String code = request.getParameter("code").toUpperCase();//Get the verification code and perform case conversion String result=null; if(request.getSession().getAttribute("newCode").equals(code)) { result="Verification successful!"; response.getWriter().append(result); } else { result="Verification code error!"; response.getWriter().append(result); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.