Struts2验证码的生成

时间:2021-12-06 10:48:05



1.1 创建Web Project工程

Struts2验证码的生成

 

1.1.1  引入jar文件

Struts2验证码的生成

 

1.1.2  配置核心控制器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">
<filter>
<filter-name>strutsDemo</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>strutsDemo</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

2  编写CheckCodeAction组件

package web;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Random;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class CheckCodeAction extends ActionSupport implements SessionAware {
private InputStream input;
private Map<String, Object> session;
private String number;

public InputStream getInput() {
return input;
}

public void setInput(InputStream input) {
this.input = input;
}

public String execute() {
byte[] buf = this.randomImage();
this.input = new ByteArrayInputStream(buf);
session.put("number", number);
String num = (String) session.get("number");
System.out.println("系统产生的验证码:" + num);
return SUCCESS;
}

private byte[] randomImage() {
BufferedImage image =

new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB);
// 获得画笔
Graphics g = image.getGraphics();

Random r = new Random();
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.fillRect(0, 0, 60, 20);
g.setColor(new Color(0, 0, 0));

number = String.valueOf(r.nextInt(99999));

g.drawString(number, 5, 15);
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.drawLine(r.nextInt(60), r.nextInt(20), r.nextInt(60), r.nextInt(20));
ByteArrayOutputStream os = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
try {
encoder.encode(image);
} catch (ImageFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return os.toByteArray();
}

public void setSession(Map<String, Object> session) {
this.session = session;
}

}

3 配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="image" class="web.CheckCodeAction">
<result type="stream">
<param name="inputName">input</param>
<param name="contentType">image/jpeg</param>
</result>
</action>



</package>

</struts>

4 编写index.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>验证码的生成</title>

</head>

<body>
<img alt="验证码" src="image.action" id="code" onclick="document.getElementById('code').src='image.action?id='+Math.random()">
</body>
</html>


 


5 部署服务器,在浏览器地址进行访问

效果图如下:

Struts2验证码的生成