资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

struts---JSP界面验证码生成与验证-创新互联

之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来!

为隆林等地区用户提供了全套网页设计制作服务,及隆林网站建设行业解决方案。主营业务为做网站、成都网站设计、隆林网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

首先是生成随机验证码图片的action:

CreateImageAction:

package com.xiaoluo.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext; 

import com.opensymphony.xwork2.ActionSupport;

public class CreateImageAction extends ActionSupport
{
    private ByteArrayInputStream inputStream;

    private static int WIDTH = 60;

    private static int HEIGHT = 20;

    public ByteArrayInputStream getInputStream()
    {
        return inputStream;
    }

    public void setInputStream(ByteArrayInputStream inputStream)
    {
        this.inputStream = inputStream;
    }
private static String createRandom() { String str = "0123456789qwertyuiopasdfghjklzxcvbnm"; char[] rands = new char[4]; Random random = new Random(); for (int i = 0; i < 4; i++) { rands[i] = str.charAt(random.nextInt(36)); } return new String(rands); } private void drawBackground(Graphics g) { // 画背景 g.setColor(new Color(0xDCDCDC)); g.fillRect(0, 0, WIDTH, HEIGHT); // 随机产生 120 个干扰点 for (int i = 0; i < 120; i++) { int x = (int) (Math.random() * WIDTH); int y = (int) (Math.random() * HEIGHT); int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); g.setColor(new Color(red, green, blue)); g.drawOval(x, y, 1, 0); } } private void drawRands(Graphics g, String rands) { g.setColor(Color.BLACK); g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18)); // 在不同的高度上输出验证码的每个字符 g.drawString("" + rands.charAt(0), 1, 17); g.drawString("" + rands.charAt(1), 16, 15); g.drawString("" + rands.charAt(2), 31, 18); g.drawString("" + rands.charAt(3), 46, 16); System.out.println(rands); } @Override public String execute() throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); // 设置浏览器不要缓存此图片 response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); String rands = createRandom(); BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); // 产生图像 drawBackground(g); drawRands(g, rands); // 结束图像 的绘制 过程, 完成图像 g.dispose(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(image, "jpeg", outputStream); ByteArrayInputStream input = new ByteArrayInputStream(outputStream .toByteArray()); this.setInputStream(input); HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("checkCode", rands); input.close(); outputStream.close(); return SUCCESS; } }

以上是生成随机验证码图片的action,将生成的随机数放到session里,然后页面提交到验证随机数的action:

LoginValidateAction:

package com.xiaoluo.action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoginValidateAction extends ActionSupport
{
    private String checkCode;
    
    public String getCheckCode()
    {
        return checkCode;
    }

    public void setCheckCode(String checkCode)
    {
        this.checkCode = checkCode;
    }

    @Override
    public String execute() throws Exception
    {
        return SUCCESS;
    }
    
    @Override
    public void validate()
    {
        HttpSession session = ServletActionContext.getRequest().getSession();
        
        String checkCode2 = (String)session.getAttribute("checkCode");
        
        if(!checkCode.equals(checkCode2))
        {
            this.addActionError("输入的验证码不正确,请重新输入!");
        }
    }
}

下面是struts.xml配置部分代码:

      
                
                    image/jpeg
                    inputStream
                
        
            
            
                /success.jsp
                /login.jsp
            

最后就是jsp部分的代码:

login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
    
    
    My JSP 'login.jsp' starting page
    
    
    
        
    
    
    
    
  
  
  

    

带有验证码的登陆界面

用户名:
密码 :
验证码:
     
     
做做笔记,传播知识,我是个勤劳的搬运工 1224144402@qq.com
分享题目:struts---JSP界面验证码生成与验证-创新互联
文章来源:http://cdkjz.cn/article/cogcdi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220