Servelt-使用ValidateCode.jar实现验证码功能

1、将ValidateCode.jar放到WebRoot/lib目录下。
2、Servlet编写:

public class ServletDemo4 extends HttpServlet {

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

        //生成一个宽为110,高为25,4个数字,5条干扰线的验证码
        ValidateCode validateCode = new ValidateCode(110, 25, 4, 5);
        validateCode.write(response.getOutputStream());

    }
}

3、Html编写:


<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
<script type="text/javascript">
    function changeCode(){
        var img = document.getElementsByTagName("img")[0];
        img.src = "demo4?time="+new Date().getTime();
    }
script>
head>
<body>
    <form actioin="#">

        username:<input type="text" name="username"/><br/>

        password:<input type="text" name="password"/><br/>

        code:<input type="text" name="code"/>
        <img src="demo4" onclick="changeCode()"><a href="javascript:changeCode()">看不清,换一张a>

        <br/>

    form>
body>
html>

ValidateCode.jar下载地址:
http://download.csdn.net/detail/csdn_lqr/9654506

你可能感兴趣的:(JavaEE)