Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

绿色网站无广告
会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

servlet实现验证码

来源: chenguandong 分享至:

servlet生成验证码

 

package com.dr;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class YZM extends HttpServlet {

   
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("image/jpeg;chrset=utf-8");
        //PrintWriter out = response.getWriter();
        BufferedImage bi = new BufferedImage(100, 25,BufferedImage.TYPE_INT_ARGB );//????
        Graphics gg = bi.getGraphics();//????
        gg.setColor(Color.cyan);//????
        gg.fill3DRect(0, 0, 500, 500, true);//????
        gg.setFont(new Font("????", 0, 26));//????//????
        gg.setColor(Color.BLACK);    //????
        String ss="";
        Dr d = new Dr();
        ss= d.drow();
        Date da = new Date();
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
        String dd=s.format(da);
        gg.drawString(ss, 20, 22);//????
        ImageIO.write(bi, "jpeg", response.getOutputStream());
    }

}

 

java  dr产生随机数

 

package com.dr;

public class Dr {
   
    public String drow(){
        String s = "";
        String shu ="1234567890poiuytrewqasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM";
        for (int i = 0; i < 4; i++) {
            s=s+(shu.charAt(((int) (Math.random()*62))));           
        }

        return s;

    }
}

 

JSP端显示验证码

 

<%@ page language="java" import="java.util.*,com.dr.*"
    pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    </head>
<script type="text/javascript">
    function shua(){
        document.getElementByIdx_x("im").src="YZM?"+Math.random();

    }

//此函数实现了调用的时候生成不同的验证码

</script>
    <body bgcolor='778899'>
        <center>
            <%
                Dr d = new Dr();
                String s = d.drow();
            %>
            <%
                if (request.getParameter("login") != null) {
                    out.print("登录失败,请重新登录!!");
                }
            %>
            <form action="UserSer" method="post" name="loginForm">
                用户名:
                <input type="text" name="username" />
                <br />
                密码:
                <input type="password" name="password" />
                <br />
                请输入验证码:
                <input type="text" name="rr" size="4"/> <img src="YZM" />
                <br />
                <input type="submit" value="快速登录" />
            </form>
            <hr />
            <%
                if (request.getParameter("zhuce") != null) {
                    out.print("对不起注册失败,重新注册!");
                }
            %>
            <form action="LoginSer" method="post">
                用户名:
                <input type="text" name="lusername" />
                <br />
                密码:
                <input type="password" name="lpassword" />
                <br />
                确认密码
                <input type="password" name="llpassword" />
                <br />
                请输入验证码:
                <input type="text" name="lrr" size="4"/><img src="YZM" id="im"/><b onmouseover="shua()">刷新</b>
                <br />
                <input type="submit" value="快速注册" />

            </form>

        </center>
    </body>
</html>


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史