`

Java Struts2 图片验证码 示例

阅读更多

This Java Captcha Example demonstrates you how to develop a captcha Servlet and use in your Struts 2 application.

Developing Struts 2 Captcha Application
In this section we are going to develop Captcha Servlet and then use the same to validate Struts 2 based web applications. We have developed a Servlet that generates Captcha image. 

Let's see how our Captcha application works. 

Working of the application

  1. captcha.jsp:  This page contains the captcha image and one text field
  2. User reads the image text and then enters in the "Code" text field. After entering the value in the text field, user clicks on the "Verify"  command button.
  3. Then our Struts 2 action validates the user input and the session Captcha value.  If user enters the correct image code in the text filed, then action  forward to the "success.jsp" page. Otherwise, it gives an error and go to again the "captcha.jsp".
  4. 1. Index page
    In the  index.html we have added a link to captcha.jsp. User can click on this link to go to the captcha form.
  5. <html>
    <head>
    <title>Capcha Validation</title>
    <style>
    a:link{ color:red; text-decoration:underline; }
    a:visited{ color:red; text-decoration:underline; }
    a:hover{ color:green; text-decoration:none; }
    a:active{ color:red; text-decoration:underline; }
    </style>
    </head>
    <body>
    <table>
    <tr><td><a href="captcha.jsp">Captcha Application</a></td></tr>
    </table>
    </body>
    </html>
     2. Captcha Page 
    The GUI of the application consists of  Captcha page  (captcha.jsp). 
    The captcha.jsp is used to display the valid code and a text box. 
  6. <%@ taglib prefix="s" uri="/struts-tags" %>
    <html>
    <head>
    <title>Capcha Validation</title>
    <style>
    .errorMessage {
    color: red;
    font-size: 0.8em;
    } 
    
    .label {
    color:#000000;
    }
    </style>
    </head>
    <body>
    <table>
    <tr>
    <td>
    <h1>Captcha Validation</h1>
    <s:form action="doCaptcha" method="POST">
    <s:actionerror /> 
    <img src="Captcha.jpg" border="0">
    <s:textfield label="Code" name="j_captcha_response" size="20" maxlength="10"/>
    <s:submit value="Verify" align="center" />
    </s:form>
    </td>
    </tr>
    </table>
    </body>
    </html>
     

The code :
<s:actionerror />
displays action errors and field validation errors.

The code <s:form action="doLogin" method="POST"> generates the html form for the application.

The code :

<img src="Captcha.jpg" border="0">
<s:textfield label="Code" name="j_captcha_response" size="20" maxlength="10"/>

The submit button is generated through <s:submit value="Verify" align="center" /> code.

3. Servlet Class
Develop a Servlet class "RoseIndiaCaptcha.java" to create a Captcha image. Here is the code of RoseIndiaCaptcha.java Servlet class:

package com.roseindiaCaptcha.servlet;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.font.TextAttribute;
 
public class RoseIndiaCaptcha extends HttpServlet {

  private int height=0;
  private int width=0;
    
  public static final String CAPTCHA_KEY = "captcha_key_name";

  public void init(ServletConfig config) throws ServletException {
    super.init(config);
   height=Integer.parseInt(getServletConfig().getInitParameter("height"));
     width=Integer.parseInt(getServletConfig().getInitParameter("width"));
  }

 
 protected void doGet(HttpServletRequest req, HttpServletResponse response) 
   throws IOException, ServletException {
    //Expire response
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Max-Age", 0);
    
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
    Graphics2D graphics2D = image.createGraphics();
    Hashtable<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
    Random r = new Random();
    String token = Long.toString(Math.abs(r.nextLong()), 36);
    String ch = token.substring(0,6);
    Color c = new Color(0.6662f, 0.4569f, 0.3232f);
    GradientPaint gp = new GradientPaint(30, 30, c, 15, 25, Color.white, true);
    graphics2D.setPaint(gp);
    Font font=new Font("Verdana", Font.CENTER_BASELINE , 26);
    graphics2D.setFont(font);
    graphics2D.drawString(ch,2,20);
    graphics2D.dispose();
    
    HttpSession session = req.getSession(true);
    session.setAttribute(CAPTCHA_KEY,ch);

    OutputStream outputStream = response.getOutputStream();
    ImageIO.write(image, "jpeg", outputStream);
    outputStream.close();



 }

}

 4.Mapping  for Servlet class in web.xml

<servlet>
<servlet-name>Captcha</servlet-name>
     <display-name>Captcha</display-name>
    <servlet-class>com.roseindiaCaptcha.servlet.RoseIndiaCaptcha</servlet-class>
<init-param> 
<description>passing height</description> 
<param-name>height</param-name> 
<param-value>30</param-value> 
</init-param> 
<init-param> 
<description>passing height</description> 
<param-name>width</param-name> 
<param-value>120</param-value> 
</init-param> 
</servlet>

<servlet-mapping>
    <servlet-name>Captcha</servlet-name>
   <url-pattern>/Captcha.jpg</url-pattern>
</servlet-mapping>
 5.Success page

After successful validation page  transfer to the "success.jsp"

<html>
<head>
<title>Capcha Validation</title>
</head>
<body>
<table><tr><td><h1>You are Succesfully Validate</h1></td></tr></table>
</body>
</html>
 6.Developing Action Class

Now let's develop the action class to handle the Captcha validation request. 

The code  String c= (String)session.getAttribute(RoseIndiaCaptcha.CAPTCHA_KEY) is used to get the Captcha value stored in session and then validate it with the user input. Here is the code of the Action class.

package com.roseindiaCaptcha.action;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import javax.servlet.http.*;
import javax.servlet.*;
import com.opensymphony.xwork2.ActionContext;
import com.roseindiaCaptcha.servlet.*;

public  class CaptchaAction  extends ActionSupport {
  public String execute() throws Exception {
      HttpServletRequest request  = (HttpServletRequest)
 ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
      Boolean isResponseCorrect = Boolean.FALSE;
      javax.servlet.http.HttpSession session = request.getSession();
      String parm = request.getParameter("j_captcha_response");
      String c= (String)session.getAttribute(RoseIndiaCaptcha.CAPTCHA_KEY) ;
      if(!parm.equals(c) ){
      addActionError("Invalid Code! Please try again!");
      return ERROR;
    }else{
    return SUCCESS;
    }
  }
}
 7.Configuring action mapping (in struts.xml)
Now we will create action mapping in the struts.xml file. Here is the code to be added in the struts.xml:
<action name="doCaptcha" class="com.roseindiaCaptcha.action.CaptchaAction">
<result name="error">captcha.jsp</result>
<result>success.jsp</result>
</action>
 In the above mapping the action "doCaptcha" is used to validates the user using action class (CaptchaAction.java).

Output : 


If invalid code the error message is display.

 


After successfully validation message :

分享到:
评论

相关推荐

    验证码实现详细步骤

    java实现验证码的完整示例,使用servlet发送验证码到前端,整合到struts中,请修改您的过滤器

    java开源包5

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    JAVA上百实例源码以及开源项目

     util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...

    JAVA上百实例源码以及开源项目源代码

     util实现Java图片水印添加功能,有添加图片水印和文字水印,可以设置水印位置,透明度、设置对线段锯齿状边缘处理、水印图片的路径,水印一般格式是gif,png,这种图片可以设置透明度、水印旋转等,可以参考代码...

    java开源包2

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    java开源包4

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    java开源包101

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    java开源包11

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    java开源包6

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    java开源包9

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    java开源包8

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    java开源包10

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    java开源包1

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    java开源包3

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    java-springmvc-mybatis-easyui

    java-springmvc-mybatis-easyuiSpringMvc3.2.x+mybatis3.1.x+EasyUI1.3.4+Maven架构的示例程序根据 修改而来,在此基础上添加jcaptcha图形验证码。SSHE项目为Struts2.3.x+Spring3.2.x+Hibernate4.2.x+CXF2.7.x+...

    java开源包7

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    Java资源包01

    Struts验证码插件 JCaptcha4Struts2 JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (&lt;jcaptcha:image label="Type the text "/&gt; ) 即可,直接在 struts.xml 中进行...

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

     国内知名的高端IT技术作家,已出版《Spring 2.0宝典》、《基于J2EE的Ajax宝典》、《轻量级J2EE企业应用实战》、《Struts 2权威指南》、《Ruby On Rails敏捷开发最佳实践》等著作。 目录: 第0章 学习Java...

    Java学习笔记-个人整理的

    \contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...

Global site tag (gtag.js) - Google Analytics