public class LoginServlet extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //后台记录的验证码(均为大写字母) String verifyPic = (String) req.getSession().getAttribute("verifyPicture"); //前台提交过来的验证码(用户不一定输入大写字母) String verifyCode = req.getParameter("verifyCode"); System.out.println("verifyPicture为:"+verifyPic); //前台输入的全部转换为大写字母 verifyCode = verifyCode.toUpperCase(); System.out.println("verifyCode为:"+verifyCode); resp.setContentType("text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); if(verifyCode.equals(verifyPic)){ out.print("验证码输入正确!"); }else{ out.print("验证码输入错误!"); } out.flush(); out.close(); } }
public class ImageServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1、定义一个BufferedImage对象 BufferedImage bfi = new BufferedImage(65, 23, BufferedImage.TYPE_INT_RGB); //2、获取Graphics对象 Graphics g = bfi.getGraphics(); Color c = new Color(180,200,255);//Color(int r, int g, int b) 指定红色、绿色和蓝色值的不透明的 sRGB颜色,最后一个值默认为255 g.setColor(c); g.fillRect(0, 0, 65, 23); //3、通过Random随机产生验证码信息 char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); Random random = new Random(); int len = ch.length; int index;//索引 StringBuffer sb = new StringBuffer(); for(int i=0;i<4;i++){//生成4位验证码 index = random.nextInt(len);//在ch数组中随机字符 //4、使用Graphics绘制图片 g.setColor(new Color(random.nextInt(88), random.nextInt(188), random.nextInt(255))); g.drawString(ch[index]+"", (i*15)+3, 18); sb.append(ch[index]); } //5、记录验证码信息到session中 req.getSession().setAttribute("verifyPicture", sb.toString()); //6、使用ImageIO输出图片 ImageIO.write(bfi, "JPG", resp.getOutputStream()); } }
index.jsp页面:
<%@ page language="java" import="java.util.*" 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>验证码</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 reloadCode(){ var time = new Date().getTime(); document.getElementById("picCode").src = "<%=request.getContextPath()%>/myServlet/ImageServlet?d="+time; } </script> </head> <body> <form action="<%=request.getContextPath()%>/myServlet/LoginServlet" method="get"> 验证码:<input type="text" name="verifyCode"> <img alt="验证码" id="picCode" src="<%=request.getContextPath()%>/myServlet/ImageServlet"> <a href="javascript:reloadCode();">看不清楚</a> <br> <input type="submit" value="提交"> <input type="reset" value="重置"> </form> </body> </html>
第一次执行每次都有问题,如果先刷新再填写验证码就没问题了,第一次获取不到session中的值,为何?求大神指导!
我也是 取不到session的值 都是null
我也是同样的问题,第一次总是空指针异常,刷新下就能正常运行
解决了吧
我执行了一下你的代码,没有你说的这个问题,一开始就成功了