在 JSF 2.0 页面中集成验证码时出现问题

发布于 2024-10-21 04:12:24 字数 2603 浏览 1 评论 0原文

我正在遵循本页中找到的教程 http://javahunter.wordpress.com/2010/09/25/integrating-captcha-in-jsf-2-0/ 将验证码集成到我用 JSF 2.0 制作的用户注册表单中,但我有一些问题,也许有人可以帮助我。这就是我到目前为止所做的:

JSF 页面:

<h:graphicImage id="capimg" value="#{facesContext.externalContext.requestContextPath}/../Captcha.jpg"/>
    <h:inputText id="captchaUserInput" value="#{registrationController.captchaUserInput}"/>
<br />
    <h:commandButton value="Register"
        action="#{registrationController.doRegisterBuyer}">
    </h:commandButton>

托管 bean:

@ManagedBean
@RequestScoped
public class RegistrationController {
...
private String captchaUserInput;
...

public String doRegisterBuyer() throws Exception {
...<code for setting the values of the user>
HttpServletRequest request = (HttpServletRequest) FacesContext
            .getCurrentInstance().getExternalContext().getRequest();
    Boolean isResponseCorrect = Boolean.FALSE;
    javax.servlet.http.HttpSession session = request.getSession();
    String parm = captchaUserInput;
    String c = (String) session.getAttribute(MyCaptcha.CAPTCHA_KEY);
    if (parm.equals(c)) {

        buyersRegistratorEJB.createBuyer(buyer);

    } else {

        return "failed";
    }

    return "registrationSucceded.xhtml";
}

MyCaptcha.java 类(源代码可以在上面的链接中找到)是一个 servlet,我将其添加到我的项目

The web 中名为 other 的包中。 xml 配置:

    <servlet>
    <servlet-name>Captcha</servlet-name>
    <servlet-class>other.MyCaptcha</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>

我认为这是问题的一部分,因为当我导航到注册页面时,我在控制台中看到了这一点:

WARNING: StandardWrapperValve[Captcha]: PWC1382: Allocate exception for servlet Captcha
java.lang.ClassNotFoundException: other.MyCaptcha

我认为我无法正确实现这一点的原因之一是因为 Web 中 MyCaptcha.java 的路径.xml 不正确。我该如何解决这个问题?我还想提一下,我不想添加更改图像选项,只有图像适合我。

I am following the tutorial i found in this page http://javahunter.wordpress.com/2010/09/25/integrating-captcha-in-jsf-2-0/ to integrate a captcha to my user registration form made in JSF 2.0 but i am having some problems, maybe somebody can help me. This is what i have done so far:

The JSF page:

<h:graphicImage id="capimg" value="#{facesContext.externalContext.requestContextPath}/../Captcha.jpg"/>
    <h:inputText id="captchaUserInput" value="#{registrationController.captchaUserInput}"/>
<br />
    <h:commandButton value="Register"
        action="#{registrationController.doRegisterBuyer}">
    </h:commandButton>

The Managed bean:

@ManagedBean
@RequestScoped
public class RegistrationController {
...
private String captchaUserInput;
...

public String doRegisterBuyer() throws Exception {
...<code for setting the values of the user>
HttpServletRequest request = (HttpServletRequest) FacesContext
            .getCurrentInstance().getExternalContext().getRequest();
    Boolean isResponseCorrect = Boolean.FALSE;
    javax.servlet.http.HttpSession session = request.getSession();
    String parm = captchaUserInput;
    String c = (String) session.getAttribute(MyCaptcha.CAPTCHA_KEY);
    if (parm.equals(c)) {

        buyersRegistratorEJB.createBuyer(buyer);

    } else {

        return "failed";
    }

    return "registrationSucceded.xhtml";
}

The class MyCaptcha.java(The source can be found at the link above) is a servlet that i added to a package called other inside my project

The web.xml configuration:

    <servlet>
    <servlet-name>Captcha</servlet-name>
    <servlet-class>other.MyCaptcha</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>

I think here is part of the problem, because when i navigate to my registration page i see this in the console:

WARNING: StandardWrapperValve[Captcha]: PWC1382: Allocate exception for servlet Captcha
java.lang.ClassNotFoundException: other.MyCaptcha

I think one of the reasons i cant implement this correctly is because the path to the MyCaptcha.java in the web.xml is not correct. How could i fix that? Also i wanted to mention, i dont want to add the change image option, only the image is fine for me.

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

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

发布评论

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

评论(2

半窗疏影 2024-10-28 04:12:24

类路径中缺少 servlet。根据这个评论

它位于我的项目中,位于 C:\jee6workspace\BBS\src\other\MyCaptcha.java

您似乎正在使用 IDE。重建项目,将项目重新发布到服务器,重新启动服务器。另请检查服务器部署文件夹,并确保 servlet 已编译并以 BBS/WEB-INF/classes/other/MyCaptcha.class 文件形式呈现。

如果是,并且您仍然遇到此问题,则 servlet 的构造/初始化已失败(它引发了未捕获的异常)。阅读服务器日志以了解详细信息并相应地修复根本原因。

The servlet is missing in the classpath. As per this comment

It is on my project at C:\jee6workspace\BBS\src\other\MyCaptcha.java

You seem to be using an IDE. Rebuild your project, republish the project to server, restart the server. Also check the server deploy folder and make sure that the servlet is been compiled and present as BBS/WEB-INF/classes/other/MyCaptcha.class file.

If it is and you still have this problem, then construction/initialization of the servlet has failed (it threw an uncaught exception). Read the server logs for details and fix the root cause accordingly.

一杆小烟枪 2024-10-28 04:12:24

也许您可以尝试 Primefaces jsf 组件库,其中包含验证码组件。它非常容易使用(只需放入您的页面即可)。

www.primefaces.org

Maybe you could just try the Primefaces jsf components library, which includes a captcha component. It's quite easy to use (just put in your page).

www.primefaces.org

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文