zoukankan      html  css  js  c++  java
  • 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,如需转载请自行联系原作者
  • 相关阅读:
    zbb20180930 Postman 使用方法详解
    Cookie、Session、jsp、EL、JSTL
    Http协议、Tomcat、servlet
    xml、网络编程、 反射
    JDBC、DBUtils
    Java IO流对象、多线程
    mySql
    Java基础
    VueJs
    GIT
  • 原文地址:https://www.cnblogs.com/twodog/p/12138756.html
Copyright © 2011-2022 走看看