JavaWeb中验证码的实现

Web程序中,验证码是常常使用的技术之一。Web程序永远面临未知用户和未知程序的探测。为了防止恶意脚本的执行,验证码技术无疑是首选方案之一。本文将讨论如何在JSPServlet中使用验证码技术。 web

验证码的产生思路很简单,在Servlet中随机产生验证码字符序列,并计入session中,JSP中以图片的形式进行显示。当用户在JSP表单中输入验证码并提交时,在相应的Servlet中验证是否与session中保存的验证码一致。下面经过代码,一次演示验证码产生和实现的验证的过程。 浏览器



1.验证码的产生 缓存



咱们须要建立一个名为ValcodeServletservlet并在其doGet()方法中完成验证码的产生。首先经过随机数的产生类Random随机产生一个4位的验证码,并将其存入session;而后使用BufferedImageGraphics类把验证码转为图片,固然为了起到较好的效果,咱们须要添加一些干扰线;最后使用ImageIO将图片输出。详细代码以下: session





 

 

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse
  response)
throwsServletException, IOException { app


 

       // 告知浏览看成图片处理 dom


 

       response.setContentType("image/jpeg"); jsp


 


 

       // 告诉浏览器不缓存 post


 

       response.setHeader("pragma", "no-cache"); this


 

       response.setHeader("cache-control", "no-cache"); url


 

       response.setHeader("expires", "0");


 


 

       // 产生由4位数字构成的验证码


 

       int length = 4;


 

       String valcode
  =
"";


 

       Random rd =
 
new Random();


 

       for(int i=0;
  i<length; i++)


 

           valcode+=rd.nextInt(10);


 


 

       // 把产生的验证码存入到Session


 

       HttpSession
  session = request.getSession();


 

       session.setAttribute("valcode", valcode);


 


 

       // 产生图片


 

       int width = 80;


 

       int height = 25;


 

       BufferedImageimg
  =
newBufferedImage(width, height,BufferedImage.TYPE_INT_RGB);


 


 

       // 获取一个Graphics


 

       Graphics g
  = img.getGraphics();


 


 

       // 填充背景色


 

       g.setColor(Color.WHITE);


 

       g.fillRect(0,
  0, width, height);


 


 

       // 填充干扰线50


 

       for(int i=0;
  i<50; i++){


 

           g.setColor(new
  Color(rd.nextInt(100)+155,rd.nextInt(100)+155,rd.nextInt(100)+155));


 

           g.drawLine(rd.nextInt(width),
  rd.nextInt(height),rd.nextInt(width), rd.nextInt(height));


 

       }


 


 

       // 绘制边框


 

       g.setColor(Color.GRAY);


 

       g.drawRect(0,
  0, width-1, height-1);


 


 

       // 绘制验证码


 

       Font[]
  fonts = {
new Font("隶书",Font.BOLD,18),new Font("楷体",Font.BOLD,18),new Font("宋体",Font.BOLD,18),new Font("幼圆",Font.BOLD,18)};


 

       for(int i=0;
  i<length; i++){


 

           g.setColor(new
  Color(rd.nextInt(150),rd.nextInt(150),rd.nextInt(150)));


 

           g.setFont(fonts[rd.nextInt(fonts.length)]);


 

           g.drawString(valcode.charAt(i)+"", width/valcode.length()*i+2, 18);


 

       }


 


 

       // 输出图像


 

       g.dispose();


 

       ImageIO.write(img,
 
"jpeg", response.getOutputStream());


 

    }


 

上面的代码只是产生了一个常规的验证码,咱们能够根据本身的须要对验证码的产生策略和干扰线进行调整。Servlet编写完毕,别忘了在web.xml中进行配置以便能在JSP中调用,其代码以下:





 

 

<servlet>


 

<description></description>


 

<display-name>ValcodeServlet</display-name>


 

<servlet-name>ValcodeServlet</servlet-name><servlet-class>org.icer.jee.valcode.servlet.ValcodeServlet</servlet-class>


 

</servlet>


 

<servlet-mapping>


 

<servlet-name>ValcodeServlet</servlet-name>


 

<url-pattern>/ValcodeServlet</url-pattern>


 

</servlet-mapping>


 




2.验证码的显示

产生验证码的servlet编写完毕,而且已经web.xml中进行了配置,那么咱们在input.jsp中使用<img />标记以图片的方式调用servlet便可显示验证码。


 


固然为了能起到验证效果,本例中还包含了简单的表单。为了放置验证码没法识别,此处还提供了看不清点击换一张功能,用户点击图片时从新加载验证码图片(问号是为了放置浏览器缓存而不能实现从新请求图片)。JSP中表单部分代码以下:





 

 

<formname="form1"method="post"action="LoginServlet">


 

验证码:


 

<inputname="vcode"type="text"class="input02"id="vcode">


 

<imgsrc="ValcodeServlet"align="absmiddle"title="看不清,点击换一张"onClick="this.src=this.src+'?'"/>


 

<inputtype="submit"name="button"id="button"value="
 
提交 ">


 

</form>


 

3.实现验证功能

  当表单提交到CheckServlet时,对用户填写的验证码和session中存储的验证码进行比对,根据结果给出不一样提示。代码以下:





 

 

protectedvoiddoPost(HttpServletRequest request, HttpServletResponse
  response)
throwsServletException, IOException {


 

       // 获取验证码


 

       String
  valcode = request.getSession().getAttribute(
"valcode").toString();


 

       // 获取用户填写的验证码


 

       String
  vcode = request.getParameter(
"vcode");


 

       // 进行验证


 

       if(!valcode.equals(vcode))


 

           System.out.println(">>>验证码错误!");


 

       else


 

           System.out.println(">>>验证码正确!");


 

    }


 

上面只是根据验证状况在控制台进行了输出,使用时根据实际的业务逻辑需求进行修改便可。

    总起来讲,验证码技术本质上就是利用Java绘图技术把随机产生的验证码字符图形化,并在JSP中以图形调用,最后在用户提交表单后在对应的servlet中进行验证。本文只是提供验证码的基本实现思路,但愿你们能灵活应用。

做者:中软卓越天津ETC

相关文章
相关标签/搜索