public class CheckcodeServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); //图片路径 String tip = "images/MsgError.gif"; String checkcode = req.getParameter("checkcode"); //测试 System.out.println(checkcode); //获取session域中的数字 String checkcodeService = (String) req.getSession().getAttribute("CHECKNUM"); //判断 if (checkcode.equals(checkcodeService)) { tip = "images/MsgSent.gif"; } //输出路径 PrintWriter pw = resp.getWriter(); pw.write(tip); pw.flush(); pw.close(); } }
当输入第4个数字的时候就会出现提示
运行结果: