我们帮大家精选了JavaWeb相关的编程文章,网友潘林帆根据主题投稿了本篇教程内容,涉及到java、weeb、session、java、验证码相关内容,已被767网友关注,相关难点技巧可以阅读下方的电子资料。
表单
<form action="loginServlet" method="post"> 请输入验证码:<input type="text" name="code" /> <img src="getCodeServlet" /><br /> <button type="submit">提交</button> </form>
载入页面时,会自动请求getCodeServlet,获取图片(验证码)。
getCodeServlet,产生验证码
@WebServlet("/getCodeServlet") public class GetCodeServlet extends HttpServlet { //验证码的宽、高 private static int WIDTH=80; private static int HEIGHT=25; //绘制背景 private void drawBg(Graphics g){ //rgb g.setColor(new Color(128, 128, 128)); //绘制矩形。x,y,wigth,height g.fillRect(0,0,WIDTH,HEIGHT); //随机绘制100个干扰点 Random random=new Random(); for (int i=0;i<100;i++){ //产生(0,1)上的小数,*WIDTH|HEIGHT,再取整也行 int x=random.nextInt(WIDTH); int y=random.nextInt(HEIGHT); g.drawOval(x,y,1,1); //干扰点的颜色也可以随机,随机产生red,green,blue即可 //g.setColor(new Color(red,green,blue)); } } //绘制验证码 private void drawCode(Graphics g,char[] code){ g.setColor(Color.BLACK); //字体、样式(多个时竖线分隔)、字号 g.setFont(new Font("serif",Font.ITALIC|Font.BOLD,18)); //在不同位置绘制验证码字符,参数:要绘制的String、横、纵坐标。+""是为了char转String。 g.drawString(code[0]+"",1,17); g.drawString(code[1]+"",16,15); g.drawString(code[2]+"",31,18); g.drawString(code[3]+"",46,16); } //随机产生4位验证码 private char[] getCode(){ String chars="0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"; char[] code=new char[4]; Random random=new Random(); for (int i=0;i<4;i++){ //[0,62) int index= random.nextInt(62); code[i]=chars.charAt(index); } return code; } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { HttpSession session = request.getSession(); ServletOutputStream sos = response.getOutputStream(); response.setContentType("image/jpeg"); //设置浏览器不缓存此图片 response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires",0); //创建内存图片 BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, TYPE_INT_RGB); Graphics g= bufferedImage.getGraphics(); char[] code=getCode(); //将验证码放到session域中。session对象要在提交响应之前获得 session.setAttribute("code",new String(code)); drawBg(g); drawCode(g,code); g.dispose(); //将图片输出到浏览器 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage,"JPEG",baos); baos.writeTo(sos); baos.close(); sos.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { doPost(request,response); } }
loginServlet,处理表单
@WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); HttpSession session = request.getSession(); String trueCode= (String) session.getAttribute("code"); String code=request.getParameter("code"); if (code.equals(trueCode)){ response.getWriter().write("验证码正确"); } else { response.getWriter().write("验证码错误"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
上面的处理方式要区分验证码的大小写。
不区分大小写:
//先转换为全大写|全小写,再判断 trueCode=trueCode.toLowerCase(); code=code.toLowerCase(); //trueCode=trueCode.toUpperCase(); //code=trueCode.toUpperCase();
总结
以上所述是小编给大家介绍的JavaWeb 使用Session实现一次性验证码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对码农之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!