0

我正在为我的注册表单实施验证码。当我的页面加载时,我不知道为什么我的验证码没有加载(显示)。

我正在使用 img tag:-> 显示我的验证码 <img src="captcha.jsp"/>。但是当我通过facebox访问它时,同样的验证码就会出现。我知道为什么会这样。

如果可以,请你帮助我。

-谢谢

索引.jsp

 <html:form action="/addUserAction?method=addUser" method="post" onsubmit="return validateAddUserForm(this);">
        <html:javascript formName="addUserForm"></html:javascript>
        <table border="0" width="100%" align="center" height="100%">


            <tr>
                <td align="center" bgcolor="#d0d67f" colspan="2" ><font size="5" face="Broadway" color="#4d4207">Registration Form</font>&nbsp;&nbsp;</td>
            </tr>
            <tr>
                <td align="center" colspan="2"><div id="ashutosh" style="background:#ffebe8;border:0px solid #dd3c10;background-position: top"> </div> <div id="ansuman"  > </div>   </td>
            </tr>
            <tr>
                <td align="right"><img alt=""  src="images/validation/backRequiredGray.gif"><font size="3" face="Broadway" color="#4d4207">Name&nbsp;&nbsp;</font></td>
                <td><html:text tabindex="3" style="height:27px;font-size:16pt;"  property="name" maxlength="240"></html:text></td>
            </tr>
            <tr>
                <td align="right"><img alt=""  src="images/validation/backRequiredGray.gif"><font size="3" face="Broadway" color="#4d4207">Email&nbsp;&nbsp;</font></td>
                <td>
                    <html:text tabindex="4" style="height:27px;font-size:16pt;" onkeyup="getEmailCheckResult(this.value);"   property="email"></html:text>

                </td>
            </tr>
            <tr>
                <td align="right"><font size="3" face="Broadway" color="#4d4207">Phone Number&nbsp;&nbsp;</font></td>
                <td><html:text tabindex="5" style="height:27px;font-size:16pt;" maxlength="15"  onkeypress="return ValidateNum(this,event)" property="contactNumber"></html:text></td>
            </tr>



            <tr>
                <td height="30" align="right" ><font size="3" face="Broadway" color="#4d4207">Code&nbsp;&nbsp;</font></td>
                <td height="30" align="left"><img alt=""  src="captcha.jsp" width="208"/></td>
            </tr>

            <tr>
                <td height="30" align="right" valign=""><img src="images/validation/backRequiredGray.gif"><font size="3" face="Broadway" color="#4d4207">Enter Code&nbsp;&nbsp;</font></td>
                <td height="30" align="left" >
                    <html:text tabindex="7" property="code"  style="height:27px;font-size:16pt;"  onkeyup="getCapchaCheckResult(this.value);"   size="16" maxlength="13"></html:text>
                 <div id="ansuman"> </div>
                </td>
            </tr>
            <tr>
                <td height="30" align="center" valign="middle" colspan="2"><img alt=""  ><font color="#4d4207">By clicking Sign Up, you agree and understand  our</font> <a href="tc.do" target="blank"><font color="#4d4207">Terms & Conditions.</font></a> </td>
            </tr>

            <tr>
                <td height="30" align="right" valign="middle" colspan="2"><img src="images/validation/backRequiredGray.gif"> Fields Required</td>
            </tr>
            <tr>
                <td colspan="2" align="center" ><input type="submit" style="height:37px;font-size:16pt;color:#4d4207;" value="Sign Up"></td>
            </tr>
            <tr>
                <td height="30" align="center" valign="middle" colspan="2" bgcolor="#d0d67f"><font color="#4d4207">Please Note: The Password will be emailed to the mail Id provided.</font> </td>
            </tr>
        </table>
    </html:form>

验证码.jsp

<%--
        Document   : captcha
        Created on : 23 Jan, 2012, 2:23:50 PM
        Author     : Ashutosh
    --%>


    <%@ page import="java.util.*, java.io.*,java.awt.*,java.awt.image.*,javax.imageio.*,java.awt.geom.*"%>
    <%
     response.setContentType("image/jpg");
      /* Define number characters contains the captcha image, declare global */
      int iTotalChars= 6;

      /* Size image iHeight and iWidth, declare globl */
      int iHeight=40;
      int iWidth=150;

      /* font style */
      Font fntStyle1 = new Font("Arial", Font.BOLD, 30);
      Font fntStyle2 = new Font("Verdana", Font.BOLD, 20);

      /* Possible random characters in the image */
      Random randChars = new Random();
      String sImageCode = (Long.toString(Math.abs(randChars.nextLong()), 36)).substring(0,iTotalChars);

      /* BufferedImage is used to create a create new image*/
      /* TYPE_INT_RGB - does not support transpatency, TYPE_INT_ARGB - support transpatency*/
      BufferedImage biImage = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2dImage = (Graphics2D) biImage.getGraphics();

      // Draw background rectangle and noisey filled round rectangles
      int iCircle = 15;
      //g2dImage.fillRect(0, 0, iWidth, iHeight);
      for ( int i = 0; i < iCircle; i++ )
      {
        g2dImage.setColor(new Color(randChars.nextInt(255),randChars.nextInt(255),randChars.nextInt(255)));
        int iRadius = (int) (Math.random() * iHeight / 2.0);
        int iX = (int) (Math.random() * iWidth - iRadius);
        int iY = (int) (Math.random() * iHeight - iRadius);
        //g2dImage.fillRoundRect(iX, iY, iRadius * 2, iRadius * 2,100,100);
      }
      g2dImage.setFont(fntStyle1);
      for ( int i = 0; i < iTotalChars; i++ )
      {
        g2dImage.setColor(new Color(randChars.nextInt(255),randChars.nextInt(255),randChars.nextInt(255)));
        if (i%2==0)
          g2dImage.drawString(sImageCode.substring(i,i+1),25*i,24);
        else
          g2dImage.drawString(sImageCode.substring(i,i+1),25*i,35);
      }

      /* create jpeg image and display on the screen*/
      OutputStream osImage = response.getOutputStream();
      ImageIO.write(biImage, "jpeg", osImage);
      //osImage.close();

      /* Dispose function is used destory an image object */
      g2dImage.dispose();

      session.setAttribute("dns_security_code", sImageCode);
      //System.out.println("Captcha Page :"+session.getAttribute("dns_security_code"));


    %>
4

1 回答 1

0

把你的jsp变成一个servlet!

jsp 是创建标记文件的好选择,如果您在文件中添加不必要的空格,这些文件不会中断。

您的验证码在 servlet 的 doGet(...) 方法中无需任何更改即可工作。

package test.captcha;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CaptchaServlet extends HttpServlet {

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {


    response.setContentType("image/jpg");
    /*
     * Define number characters contains the captcha image, declare global
     */
    int iTotalChars = 6;

    /*
     * Size image iHeight and iWidth, declare globl
     */
    int iHeight = 40;
    int iWidth = 150;

    /*
     * font style
     */
    Font fntStyle1 = new Font("Arial", Font.BOLD, 30);
    Font fntStyle2 = new Font("Verdana", Font.BOLD, 20);

    /*
     * Possible random characters in the image
     */
    Random randChars = new Random();
    String sImageCode = (Long.toString(Math.abs(randChars.nextLong()), 36)).substring(0, iTotalChars);

    /*
     * BufferedImage is used to create a create new image
     */
    /*
     * TYPE_INT_RGB - does not support transpatency, TYPE_INT_ARGB - support transpatency
     */
    BufferedImage biImage = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2dImage = (Graphics2D) biImage.getGraphics();

    // Draw background rectangle and noisey filled round rectangles
    int iCircle = 15;
    //g2dImage.fillRect(0, 0, iWidth, iHeight);
    for (int i = 0; i < iCircle; i++) {
        g2dImage.setColor(new Color(randChars.nextInt(255), randChars.nextInt(255), randChars.nextInt(255)));
        int iRadius = (int) (Math.random() * iHeight / 2.0);
        int iX = (int) (Math.random() * iWidth - iRadius);
        int iY = (int) (Math.random() * iHeight - iRadius);
        //g2dImage.fillRoundRect(iX, iY, iRadius * 2, iRadius * 2,100,100);
    }
    g2dImage.setFont(fntStyle1);
    for (int i = 0; i < iTotalChars; i++) {
        g2dImage.setColor(new Color(randChars.nextInt(255), randChars.nextInt(255), randChars.nextInt(255)));
        if (i % 2 == 0) {
            g2dImage.drawString(sImageCode.substring(i, i + 1), 25 * i, 24);
        } else {
            g2dImage.drawString(sImageCode.substring(i, i + 1), 25 * i, 35);
        }
    }

    /*
     * create jpeg image and display on the screen
     */
    OutputStream osImage = response.getOutputStream();
    ImageIO.write(biImage, "jpeg", osImage);
    //osImage.close();

    /*
     * Dispose function is used destory an image object
     */
    g2dImage.dispose();

    HttpSession session = request.getSession();
    session.setAttribute("dns_security_code", sImageCode);
    //System.out.println("Captcha Page :"+session.getAttribute("dns_security_code"));

}



protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


}

如果不存在则创建一个 Web 应用程序描述符:/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<servlet>
        <servlet-name>CaptchaServlet</servlet-name>
        <servlet-class>test.captcha.CaptchaServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>CaptchaServlet</servlet-name>
    <url-pattern>/captcha-image.jpg</url-pattern>
</servlet-mapping>
</web-app>
于 2012-10-11T12:14:48.193 回答