itcast-tools-1.4.2.jar是实现jsp servlet验证码等功能必不可少的一款jar工具包,如果缺失了itcast-tools-1.4.2.jar包许多重要的功能将报错,马上下载此jar吧。 使用实例 ① <servlet> <servlet-name>VerifyCodeServlet</servlet-name> <servlet-class>cn.itcast.vcode.servlet.VerifyCodeServlet</servlet-class> </servlet> ② <servlet-mapping> <servlet-name>VerifyCodeServlet</servlet-name> <url-pattern>/VerifyCodeServlet</url-pattern> </servlet-mapping> ③ 利用itcast-tool包实现jsp+servlet验证码 ④ 需要的包名:itcast-tools.jar ⑤ 新建LoginServlet.Java package cn.itcast.test.web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.itcast.servlet.BaseServlet; public class LoginServlet extends BaseServlet { public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 校验验证码 * 1. 获取表单中的验证码 * 2. 获取图片上的文字 */ String verifyCode = request.getParameter("verifyCode"); // VerifyCodeServlet会把真正的验证码保存到session中 String vcode = (String) request.getSession().getAttribute("vCode"); System.out.println(verifyCode.equalsIgnoreCase(vcode)); return null; } } ⑥ 新建form.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'form.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"> --> <script type="text/javascript"> function _hyz() { /* 1. 获取<img>元素 2. 给它的src指向为/tools/VerifyCodeServlet */ var img = document.getElementById("imgVerifyCode"); // 需要给出一个参数,这个参数每次都不同,这样才能干掉浏览器缓存! img.src = "/tools/VerifyCodeServlet?a=" + new Date().getTime(); } </script> </head> <body> <%-- 1. 写表单,其中包含图片(验证码) 2. 让图片显示出来: 把<img>的src指向VerifyCodeServlet,你需要在web.xml中部署VerfiyCodeServlet 3. 换一张 --%> <form action="/tools/LoginServlet" method="post"> <%-- 添加一个参数:method=login --%> <input type="hidden" name="method" value="login"> 用户名:<input type="text" name="name"/><br/> 密 码:<input type="password" name="pwd"/><br/> 验证码:<input type="text" name="verifyCode"/><br/> <img src="/tools/VerifyCodeServlet" id="imgVerifyCode"/> <a href="javascript:_hyz()">换一张</a> <br/> <input type="submit" value="提交"/> </form> </body> </html> 文件预览