jsp or servlet验证码

文件一LoginCheckServlet.java
package Random;


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 LoginCheckServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public LoginCheckServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out
                .println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the GET method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

     HttpSession session=request.getSession();
     String randomCode=(String)session.getAttribute("randomCode");
     if(randomCode==null){
         response.sendRedirect("http://localhost:8080/code/MyHtml.html");
         return;
     }
     String reqRandom=request.getParameter("random").trim();
     request.setCharacterEncoding("GBK");
     response.setContentType("text/html;charset=GBK");
     PrintWriter out=response.getWriter();
     if(randomCode.equals(reqRandom)){
         out.print("验证匹配!");
         
         
         
     }
     else{
    out.print(randomCode);
         
    out.print("验证码效验错误!");
   
         
         
     }
     out.close();
     
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occure
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
文件二RandomCodeServlet .java
package Random;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.*;
import java.io.*;

import javax.servlet.http.*;

public class RandomCodeServlet extends HttpServlet {
   
private int width=60;
private int height=20;
protected void service(HttpServletRequest rsq,HttpServletResponse resp) throws ServletException ,java.io.IOException{
  
    BufferedImage buffimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    Graphics2D g=buffimg.createGraphics();
    Random random=new Random();
    g.setColor(Color.WHITE);
    g.fillRect(0,0,width,height);
   Font font=new Font("Times New Roman",Font.PLAIN,18);
   g.setFont(font);
   g.setColor(Color.BLACK);
   g.drawRect(0, 0, width-1, height-1);
   g.setColor(Color.GRAY);
   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, xl, yl);
   }
   StringBuffer randomCode=new StringBuffer();
   int red=0,green=0,blue=0;
   for (int i=0;i<4;i++){
    String strRand=String.valueOf(random.nextInt(10));
    red=random.nextInt(110);
    green=random.nextInt(50);
    blue=random.nextInt(50);
    g.setColor(new Color(red,green,blue));
    g.drawString(strRand,13*i,16);
    randomCode.append(strRand);  
      
   }
   HttpSession session=rsq.getSession();
   session.setAttribute("randomCode",randomCode.toString());
   resp.setHeader("Pragma", "no-cache");
   resp.setHeader("Cache-Control", "no-cache");
   resp.setDateHeader("Expires", 0);
   resp.setContentType("image/jpeg");
   ServletOutputStream sos =resp.getOutputStream();
   ImageIO.write(buffimg, "jpeg", sos);
  
   sos.println("a"+buffimg.toString());
   sos.close();
}
   
   
}
文件三MyHtml.html

<html>
  <head>
    <title>MyHtml.html</title>
   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script langeage="javascript">
<!--

function show(o){
    //重载验证码
        var timenow = new Date().getTime();
   
    o.src="http://localhost:8080/code/servlet/RandomCodeServlet?d="+timenow
   
   
    }
       

-->
</script>
  </head>
 
  <body >
  <form method="post" action="servlet/LoginCheckServlet" >
  <p>&nbsp;&nbsp;用户名:
    <input name="username" type="text" size="20">
  </p>
  <p>&nbsp; 密&nbsp; &nbsp;&nbsp;码:
    <input name="password" type="password" size="20">
  <p>&nbsp; 验证码: <input name="random" type="text" size="20" maxlength="4">
    <img id="image" src="http://localhost:8080/code/servlet/RandomCodeServlet" onClick="show(document.getElementById('image'))">  </p>
  <p>
  <div id=txtHint>
  <a onClick="show(document.getElementById('image'))">看不清</a>
 
  </div>
  <input type="submit" name="submit" value="submit"> &nbsp;&nbsp; &nbsp;<input type="reset" value="reset" name="Reset" onClick="change()">
  <br>
<p>&nbsp;</p><p>&nbsp;</p></form>
 
  </body>
</html>



效果如下

 用户名:

  密    码:

  验证码:

看不清     

 

 




1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值