• struts2验证码


    验证码大多是jsp,servlet写的. 我拿来主义了, 再自己完善了一下(我一直努力想要站在巨人的肩膀)
     
    首先是页面 test.jsp
    <%@ page contentType="text/html; charset=GBK"%> 
    <html> 
      <head> 
        <title>test image code</title> 
    <script type="text/javascript"> 
    function reloadImg(){ 
    var i = Math.random(); 
    document.getElementById("imgCode").src="imgcode?"+i; 

    </script> 
      </head> 
      <body> 
        <form action="imageCodeCheck.action" method="POST" name="form1"> 
          <img id="imgCode" src="imgcode" border="0"> 
          <input type="text" name="thecode" /><a href="javascript:reloadImg();">看不清?</a> 
          <font color="red">${requestScope.imgCodeError }</font> 
          <br> 
          <input type="submit" name="submit" value=" 提 交 " /> 
        </form> 
      </body> 
    </html> 
     
     
    接下来是Action了 ImageCodeCheck.java
    package com.lichen.action; 

    import java.util.Map; 

    import javax.servlet.http.HttpServletRequest; 

    import org.apache.struts2.interceptor.ServletRequestAware; 
    import org.apache.struts2.interceptor.SessionAware; 

    import cn.com.enorth.util.imgcode.ImageCodeUtils; 

    import com.opensymphony.xwork2.ActionSupport; 

    public class ImageCodeCheck extends ActionSupport implements 
        ServletRequestAware, SessionAware { 

      private HttpServletRequest request; 
      private Map session; 

      @Override 
      public String execute() throws Exception { 
        if (ImageCodeUtils.isGoodCode(request, "thecode")) { 
          return SUCCESS; 
        } else { 
          request.setAttribute("imgCodeError""验证码输入不正确"); 
        } 
        return INPUT; 
      } 

      @Override 
      public void setServletRequest(HttpServletRequest request) { 
        this.request = request; 
      } 

      @Override 
      public void setSession(Map session) { 
        this.session = session; 
      } 

     
     
    struts的配置很普通 struts.xml
    <?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE struts PUBLIC 
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
            "http://struts.apache.org/dtds/struts-2.0.dtd"> 
    <struts> 
      <constant name="struts.i18n.encoding" value="GBK"></constant> 
      <constant name="struts.locale" value="zh_CN"></constant> 
      <package name="struts2" extends="struts-default"> 
        <action name="imageCodeCheck" class="com.lichen.action.ImageCodeCheck" > 
          <result name="success" >index.jsp</result> 
          <result name="input" >test.jsp</result> 
        </action> 
      </package> 
    </struts>
     
     
    然后就是web的配置了 web.xml
    <?xml version="1.0" encoding="UTF-8"?> 
    <web-app version="2.5"    
      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_2_5.xsd"> 

        <!-- struts2 --> 
      <filter> 
        <filter-name>struts2</filter-name> 
        <filter-class> 
          org.apache.struts2.dispatcher.FilterDispatcher 
        </filter-class> 
      </filter> 
      <filter-mapping> 
        <filter-name>struts2</filter-name> 
        <url-pattern>/*</url-pattern> 
      </filter-mapping> 
         
        <!-- spring --> 
      <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath*:applicationContext-*.xml</param-value> 
      </context-param> 

      <listener> 
        <listener-class> 
          org.springframework.web.context.ContextLoaderListener 
        </listener-class> 
      </listener> 

      <!-- hibernate 的session 创建关闭 --> 
      <filter> 
        <filter-name>hibernateFilter</filter-name> 
        <filter-class> 
          org.springframework.orm.hibernate3.support.OpenSessionInViewFilter 
        </filter-class> 
      </filter> 
      <filter-mapping> 
        <filter-name>hibernateFilter</filter-name> 
        <url-pattern>/*</url-pattern> 
      </filter-mapping> 
         
        <!-- 验证码 --> 
        <servlet> 
            <servlet-name>imagecodeservlet</servlet-name> 
            <servlet-class>cn.com.enorth.util.imgcode.ImageCodeServlet</servlet-class> 
            <init-param> 
                <param-name>type</param-name> 
                <param-value>1</param-value> 
            </init-param> 
            <init-param> 
                <param-name>length</param-name> 
                <param-value>4</param-value> 
            </init-param> 
        </servlet> 
        <servlet-mapping> 
            <servlet-name>imagecodeservlet</servlet-name> 
            <url-pattern>/imgcode</url-pattern> 
        </servlet-mapping> 
         
        <welcome-file-list> 
            <welcome-file>index.jsp</welcome-file> 
        </welcome-file-list> 
    </web-app> 
    重点在验证码的配置
     
     

    本文转自chainli 51CTO博客,原文链接:http://blog.51cto.com/lichen/157515,如需转载请自行联系原作者
  • 相关阅读:
    UtraEdit 不启用语法彩色显示
    XXX 不是当前用户的有效责任,请联系您的系统管理员
    子帐 SLA(SubLedger Accounting)
    518 vue组件的data是函数,不是对象
    517 vue注册组件语法糖,模板的分离写法
    516 vue父组件和子组件
    515 vue全局组件和局部组件
    514 vue组件化开发概述,注册组件的基本步骤
    513 v-model:表单绑定,原理,radio,checkbox,select,修饰符,值绑定
    512 高阶函数 filter、map、reduce
  • 原文地址:https://www.cnblogs.com/twodog/p/12138756.html
Copyright © 2020-2023  润新知