在 GWT-Ext 2.0.3 中实现验证码

发布于 2024-11-18 09:48:46 字数 1909 浏览 6 评论 0原文

大家好,
我正在使用 GWT-Ext 2.0.3。我已经在我的项目中实现了 SimpleCaptcha。当我重新加载面板时,我无法刷新验证码图像。我正在使用 simplecaptcha-1.2.1.jar 文件来实现验证码。

  • 我创建了一个 servlet 来显示验证码图像。
  • import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import nl.captcha.Captcha;
    import nl.captcha.backgrounds.GradiatedBackgroundProducer;
    import nl.captcha.servlet.CaptchaServletUtil;
    import nl.captcha.servlet.SimpleCaptchaServlet;
    
    public class ExtendedCaptchaServlet  extends SimpleCaptchaServlet  {
    
        private static final long serialVersionUID = -3280477565969758716L;
    
        //@Override
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
    
        throws ServletException, IOException {
    
            HttpSession session = req.getSession();
    
            Captcha captcha = new Captcha.Builder(200, 50)
    
            .addText()
    
            .addBackground(new GradiatedBackgroundProducer()).addNoise()
    
            .addBorder()
    
            .build();
    
            session.setAttribute("captcha", captcha);
            CaptchaServletUtil.writeImage(resp, captcha.getImage());
    
        }
    }
    

  • web.xml 中的条目
  • <servlet>
        <servlet-name>SimpleCaptcha</servlet-name>
        <servlet-class> com.server.test.ExtendedCaptchaServlet 
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SimpleCaptcha</servlet-name>
        <url-pattern>/SimpleCaptcha.jpg</url-pattern>
    </servlet-mapping>
    

  • 我的 GWT 面板
  • FormPanel formPanel = new FormPanel();
    Image captchaImage = new Image("/SimpleCaptcha.jpg");
    formPanel.add(captchaImage);
    

    每次当我从浏览器刷新页面时,它都会显示新的验证码图像。但当我重新加载表单面板时,它不显示图像。


    有人可以帮助我吗?

    Hello All,
    I am using GWT-Ext 2.0.3. I have implemented SimpleCaptcha in my project.I am not able to refresh the captcha image when I reload the panel.I am using simplecaptcha-1.2.1.jar file to implemet captcha.

  • I have created a servlet to display captcha image.
  • import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import nl.captcha.Captcha;
    import nl.captcha.backgrounds.GradiatedBackgroundProducer;
    import nl.captcha.servlet.CaptchaServletUtil;
    import nl.captcha.servlet.SimpleCaptchaServlet;
    
    public class ExtendedCaptchaServlet  extends SimpleCaptchaServlet  {
    
        private static final long serialVersionUID = -3280477565969758716L;
    
        //@Override
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
    
        throws ServletException, IOException {
    
            HttpSession session = req.getSession();
    
            Captcha captcha = new Captcha.Builder(200, 50)
    
            .addText()
    
            .addBackground(new GradiatedBackgroundProducer()).addNoise()
    
            .addBorder()
    
            .build();
    
            session.setAttribute("captcha", captcha);
            CaptchaServletUtil.writeImage(resp, captcha.getImage());
    
        }
    }
    
  • Entry in web.xml
  • <servlet>
        <servlet-name>SimpleCaptcha</servlet-name>
        <servlet-class> com.server.test.ExtendedCaptchaServlet 
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SimpleCaptcha</servlet-name>
        <url-pattern>/SimpleCaptcha.jpg</url-pattern>
    </servlet-mapping>
    
  • My GWT Panel
  • FormPanel formPanel = new FormPanel();
    Image captchaImage = new Image("/SimpleCaptcha.jpg");
    formPanel.add(captchaImage);
    

    It is displays the new captcha image each time when I refresh the page from browser. But it does not display the image when I reload the form panel.

    Can anyone help me?

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    内心荒芜 2024-11-25 09:48:46

    尝试添加“时间戳”

    Image captchaImage = new Image("/SimpleCaptcha.jpg?" + Math.floor(Math.random()*100));
    

    try to add "timestamp"

    Image captchaImage = new Image("/SimpleCaptcha.jpg?" + Math.floor(Math.random()*100));
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文