注意:使用的包(ValidateCode.jar)
例子:
package com.qf.emp.controller; import cn.dsna.util.images.ValidateCode; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet(value = "/createCode") public class createCodeController extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //配置验证码长,宽,干扰线,大小 ValidateCode code = new ValidateCode(200,30,4,20); String codes = code.getCode(); HttpSession session =req.getSession(); session.setAttribute("codes",codes); //验证码图片相应给客户端 code.write(resp.getOutputStream()); } }