struts2使用kaptcha生成验证图片

1、到http://code.google.com/p/kaptcha/ 下载jar文件到 WEB-INF/lib目录下

2、配置web.xml文件如下:

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

<web-app id="starter" version="2.4" 
         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-app_2_4.xsd">
	
	<display-name>Struts 2 - Maven Archetype - Starter</display-name>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext.xml</param-value>
	</context-param>
<!-- 关键是这部分servlet配置要放在下面的filter配置之前,否则就会找默认的action去处理了-->	
	<servlet>
		<servlet-name>Kaptcha</servlet-name>
		<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
		
		<init-param>
			<param-name>kaptcha.image.width</param-name>
			<param-value>100</param-value>
		</init-param>
		<init-param>
			<param-name>kaptcha.image.height</param-name>
			<param-value>30</param-value>
		</init-param>
		<init-param>
			<param-name>kaptcha.textproducer.font.size</param-name>
			<param-value>30</param-value>
		</init-param>
		<init-param>
			<param-name>kaptcha.textproducer.char.length</param-name>
			<param-value>4</param-value>
		</init-param>
		<init-param>
			<param-name>kaptcha.border</param-name>
			<param-value>no</param-value>
		</init-param>

		<init-param>
			<param-name>kaptcha.textproducer.font.color</param-name>
			<param-value>black</param-value>
		</init-param>

		<init-param>
			<param-name>kaptcha.textproducer.char.space</param-name>
			<param-value>2</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>Kaptcha</servlet-name>
		<url-pattern>/Kaptcha.jpg</url-pattern>
	</servlet-mapping>

	<!-- Filters -->
    <filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>action2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
		
    
	<!-- Listeners -->
	<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
      
    
    <!-- Welcome file lists -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.jsp</welcome-file>
        <welcome-file>login.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>
关键是这部分servlet配置要放在下面的filter配置之前,否则就会找默认的action去处理了,而且struts2是支持servlet配置的
 

 3、jsp文件

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<%@ page language="java" contentType="text/html; charset=UTF-8" %>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>登陆</title>
	</head>
	<body>							
		<form method="POST">
			<table>
				<tr>
					<td>用户名称:</td>
					<td><input type="text" name="userId"></td>
				</tr>
				<tr>
					<td>用户密码:</td>
					<td><input type="password" name="passwd"></td>
				</tr>
				<tr>
					<td>验证码:</td>
					<td><input type="text" name="userId"></td>
					<td><img src="../Kaptcha.jpg"></td>
				</tr>
				<tr>
					<td><input type="submit" name="submit"></td>
				</tr>
			</table>
		</form>
	</body>
</html>

 关键是<td><img src="../Kaptcha.jpg"></td>这个文件名是在配置文件里边指定的,而且要注意相对路径
4、action中可以利用下列代码获得验证码数值

Map<String,Object> session = ActionContext.getContext().getSession();		
String kaptchaExpected = (String)session.get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
session.remove(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
 

你可能感兴趣的:(jsp,Web,xml,servlet,Google)