servlet 验证码

来源:互联网 发布:河西学院网络选修 编辑:程序博客网 时间:2024/05/17 22:31

手工实现

import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.IOException;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 cn.dsna.util.images.ValidateCode;public class Demo3 extends HttpServlet{    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        ValidateCode vc = new ValidateCode(110,50);        vc.write(resp.getOutputStream());        int width = 110;        int height = 25;        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);        //创建画笔        Graphics g = img.getGraphics();        //上背景色        g.setColor(Color.PINK);        g.fillRect(1, 1, width-2, height-2);        //给边框颜色        g.setColor(Color.RED);        g.drawRect(1, 1, width, height);        //设置文字        g.setColor(Color.BLUE);        g.setFont(new Font("宋体", Font.PLAIN,15));        Random rand = new Random();        int cnt = 20;        for (int i=0; i<4; i++){            g.drawString(rand.nextInt(10)+"", cnt, 20);            cnt+=20;        }        for (int i=0; i<4; i++){            g.drawLine(rand.nextInt(width), rand.nextInt(height),rand.nextInt(width), rand.nextInt(height));        }        //将图片以流对象输出到客户端        ImageIO.write(img, "jpg", resp.getOutputStream());    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        // TODO 自动生成的方法存根        super.doPost(req, resp);    }}

html显示验证码

<!DOCTYPE html><html>  <head>    <title>Login.html</title>    <meta name="keywords" content="keyword1,keyword2,keyword3">    <meta name="description" content="this is my page">    <meta name="content-type" content="text/html; charset=UTF-8">    <!--<link rel="stylesheet" type="text/css" href="./styles.css">--><script type="text/javascript">    function changeCode(){        var img = document.getElementsByTagName("img")[0];        img.src="/day1/demo1?time="+new Date().getTime();    }</script>  </head>  <body>    <form action="#" method="post">        用户名:<input type="text" ><br>        密码:<input type="password" ><br>        验证码:<input type="text" >        <img src="/day1/demo1" onclick="changeCode()"/><a href="javascript:changeCode()">看不清 换一张</a><br>        <input type="submit" value="登入"/><br>    </form>  </body></html>

通过jar包实现

就换成两行代码即可,都实现好了直接用

package demo2;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.IOException;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 cn.dsna.util.images.ValidateCode;public class Demo3 extends HttpServlet{    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        ValidateCode vc = new ValidateCode(110,50);        vc.write(resp.getOutputStream());    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        // TODO 自动生成的方法存根        super.doPost(req, resp);    }}
原创粉丝点击