如何正确显示servlet中的验证码?

时间:2018-10-02 08:32:10

标签: java maven jsp servlets

使用servlet时,我遇到一个问题,我试图在验证码servlet中绘制一个验证码,然后使用自定义标签显示它。但是我有一个问题,我的验证码标记正在向后退,它没有获取我的验证码的实际ID,因此我无法检查输入的参数进行验证,因为ID不匹配。我当时正在考虑为注册servlet生成一个验证码,将其放入会话中,然后将其转移到验证码servlet并在那里绘制。以这种方式byte [] gg = req.getSession().getAtribute("captchaId);,但是在那种情况下,我得到了ClassCastException。如何正确显示ID并将其放入标签?

    Registration servlet:
        protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
            startTime = System.currentTimeMillis();
    //        CaptchaGenerator captchaGenerator = new CaptchaGenerator(captchaService);
    //        ByteArrayOutputStream baos = new ByteArrayOutputStream();
    //        captchaGenerator.generateCaptcha(httpServletResponse, baos);
    //        httpServletRequest.getSession().setAttribute(captchaService.getId(), baos.t

oByteArray());
//        httpServletRequest.getSession().setAttribute("captchaId", captchaService.getId());
        httpServletRequest.getRequestDispatcher(ConstantApp.JSPPages.REGISTRATION_PAGE).forward(httpServletRequest, httpServletResponse);
    }

CaptchaTag:

public class CaptchaCustomTag extends SimpleTagSupport {

    private static final Logger LOG = Logger.getLogger(CaptchaCustomTag.class);

    private String captchaId;

    public void setCaptchaId(String captchaId) {
        this.captchaId = captchaId;
    }

    private String getCaptchaPNG() {
        return "<img src=\"captcha?id=" + captchaId + "\">";
    }

    private String getHidden() {
        return "<input type=\"hidden\" name=\"captchaID\" value=\"" + captchaId + "\">\n";

    }

    @Override
    public void doTag() throws JspException, IOException {
        getJspContext().getOut().write(getCaptchaPNG() + getHidden());
        LOG.debug("Captcha id: " + captchaId);
    }
}

CaptchaServlet:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    CaptchaTypeManager captchaTypeManager = new CaptchaTypeManager(req, resp, captchaService);
    CaptchaGenerator captchaGenerator = new CaptchaGenerator(captchaService);

    captchaTypeManager.getType(getServletContext().getInitParameter(ConstantApp.ContextType.CONTEXT_TYPE));
    byte[] captcha = (byte[]) req.getSession().getAttribute(req.getParameter("captchaId"));
    if (captcha == null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        captchaGenerator.generateCaptcha(resp, baos);
        captcha = baos.toByteArray();
        req.getSession().setAttribute(captchaService.getId(), baos.toByteArray());
        req.getSession().setAttribute("captchaId", captchaService.getId());
    }
    IOUtils.copy(new ByteArrayInputStream(captcha), resp.getOutputStream());

    LOG.debug(getServletContext().getInitParameter(ConstantApp.ContextType.CONTEXT_TYPE));
}

验证码标签tld:

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">
    <tlib-version>2.1</tlib-version>
    <short-name>MyTags</short-name>
    <uri>http://journaldev.com/jsp/tlds/MyTags</uri>
    <tag>
        <name>captcha</name>
        <tag-class>com.epam.preproduction.linnyk.customtag.CaptchaCustomTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>captchaId</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

registration.jsp:

<form action="registration" method="post" id="registration_form">
                    <ul>
                        <h2><div align="center" style="color: #ff0000">${requestScope.error_time}</div></h2>
                    <ul>
                        <div id="error_first_name" style="color: #ff0000">${requestScope.error_first_name}</div>
                        <li class="text-info">First Name:</li>
                        <li><input type="text" id="fName" name="fName" placeholder="Your first name" value="${sessionScope.fName}"></li>
                    </ul>
                    <ul>
                        <div id="error_last_name" style="color: #ff0000">${requestScope.error_last_name}</div>
                        <li class="text-info">Last Name:</li>
                        <li><input type="text" id="lName" name="lName" placeholder="Your last name" value="${sessionScope.lName}"></li>
                    </ul>
                    <ul>
                        <div id="error_login" style="color: #ff0000">${requestScope.error_login}</div>
                        <li class="text-info">Login:</li>
                        <li><input type="text" id="login" name="login" placeholder="Your login" value="${sessionScope.login}"></li>
                    </ul>
                    <ul>
                        <div id="error_email" style="color: #ff0000">${requestScope.error_email}</div>
                        <li class="text-info">Email:</li>
                        <li><input type="text" id="email" name="email" placeholder="Your email" value="${sessionScope.email}"></li>
                    </ul>
                    <ul>
                        <div id="error_password" style="color: #ff0000">${requestScope.error_password}</div>
                        <li class="text-info">Password:</li>
                        <li><input type="password" id="pass" name="pass" placeholder="Your password"></li>
                    </ul>
                    <ul>
                        <div id="error_confirmPassword" style="color: #ff0000">${requestScope.error_confirmPassword}</div>
                        <li class="text-info">Re-enter Password:</li>
                        <li><input type="password" id="confPass" name="confPass" placeholder="Confirm your password"></li>
                    </ul>


                     <MyTags:captcha captchaId = "${captchaId}"/>
                     <br></br>
                     <div style="color: #ff0000" id="error_captcha">${requestScope.error_captcha}</div>
                     <input type="text" placeholder="Enter the code from the image" name="captchaCode" id="captchaCode"/>


                    <input type="submit" id="submit" value="Register Now">
                    <p class="click">By clicking this button, you agree to my modern style <a href="terms.jsp">Policy Terms and
                        Conditions</a> to Use</p>
                </form>

由于我的代码的加载时间早于验证码Servlet的加载时间,因此ID会在稍后的步骤到达那里。如何正确实施?

如果我取消了代码在regestant servlet中的注释,然后在验证码servlet中注释了代码,那么我得到了一个种姓类,该如何实现呢?

byte[] captcha = (byte[]) req.getSession().getAttribute("captchaId");

0 个答案:

没有答案