struts-config.xml

    <form-bean name="checkForm" type="org.apache.struts.validator.DynaValidatorForm">
      <form-property name="rand" type="java.lang.String" />
    </form-bean>
    <action
      attribute="checkForm"
      input="/check/check.jsp"
      name="checkForm"
      path="/check"
      scope="request"
      validate="true"
      type="com.yourcompany.struts.action.CheckAction">
      <forward name="ok" path="/check/checkok.jsp" />
    </action>

validation.xml

   <form name="checkForm">
        <field property="rand" depends="required">
       <arg key="checkForm.rand"/>    
       </field>  
   </form>

spring config

<bean name="/check" class="com.yourcompany.struts.action.CheckAction"/>

checkAction.java

public class CheckAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  DynaValidatorForm checkForm = (DynaValidatorForm) form;// TODO Auto-generated method stub
        /*HttpSession session=request.getSession();
        String strrand = (String)session.getAttribute("rand");
        session.setAttribute("rand", strrand);*/
  return mapping.findForward("ok");
 }
}

check.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html>
 <head>
  <title>JSP for DynaValidatorForm form</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
  <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
  <META HTTP-EQUIV="Expires" CONTENT="0">
 </head>
 <body>
  <html:form action="/check">
  <td><img border=0 src="check/image.jsp"></td>
  <html:text property="rand"/><html:errors property="rand"/><br/>
   <html:submit/><html:cancel/>
  </html:form>
 </body>
</html>

 

checkok.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
  <body>
<%
 String rand = (String)session.getAttribute("rand");
 String input = request.getParameter("rand");
  if (rand.equals(input)) {
%>
<font color=green>OK</font>
<%
  } else {
%>
<font color=red>FAIL</font>
<%
  }
%>
  </body>
</html>

image.jsp

<%@ page language="java" import="java.util.*" %>
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,javax.imageio.*" %>
<%@ page import="java.io.OutputStream" %>
<%!
Color getRandColor(int fc,int bc){
        Random random = new Random();
        if(fc>255) fc=255;
        if(bc>255) bc=255;
        int r=fc+random.nextInt(bc-fc);
        int g=fc+random.nextInt(bc-fc);
        int b=fc+random.nextInt(bc-fc);
        return new Color(r,g,b);
        }
%>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);

int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
OutputStream os=response.getOutputStream();
Graphics g = image.getGraphics();

Random random = new Random();


g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);

g.setFont(new Font("Times New Roman",Font.PLAIN,18));

g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
 int x = random.nextInt(width);
 int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
 g.drawLine(x,y,x+xl,y+yl);
}

String sRand="";
for (int i=0;i<4;i++){
    String rand=String.valueOf(random.nextInt(10));
    sRand+=rand;

    g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
    g.drawString(rand,13*i+6,16);
}

session.setAttribute("rand",sRand);

g.dispose();

ImageIO.write(image, "JPEG",os);
os.flush();
os.close();
os=null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
%>

posted on 2008-01-18 16:09 lzj520 阅读(1026) 评论(0)  编辑  收藏 所属分类: Struts1Spring个人学习日记

只有注册用户登录后才能发表评论。


网站导航: