1. 程式人生 > >登陸頁面的驗證碼生成與驗證

登陸頁面的驗證碼生成與驗證

數字 name ron als ntc per null 註冊 errorlog

登陸頁面的驗證碼生成與驗證

特點:基於strurs2框架 減少對數據庫的訪問次數 避免無效的訪問 提高服務器的性能

1 登陸界面:基於struts2框架 <s:src action>動態獲取隨機驗證碼圖片

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

<%@ taglib prefix="s" uri="/struts-tags" %>

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

<html>

<head>

</head>

<body>

<form action="${pageContext.request.contextPath}/user/loginUser" method="post">

帳號:<input type="text"user.username"/><br/>

密碼:<input type="password" name="user.password"/>

<input name="clientCode" type="text" ><img src="

<s:url action="imageAction" namespace="/user" />"/>

<br/>

<input type="submit" value="登錄"/><input type="reset" value="取消"/>

</form>

</body>

</html>

2 userAction:

a:getImage()方法中 產生隨機4位String驗證碼 存入值棧session會話中

b:把4為數字轉為jpg格式的圖片 並把圖片傳給login頁面 並顯示出來

c: loginUser()方法中 通過成員變量收參 獲取頁面輸入框中的client輸入的驗證碼 與值棧session中存進的隨機產生驗證碼serviceCode進行比較 , 相同則驗證通過 允許訪問數據庫 不相同則返回登陸頁面 並提示“驗證碼錯誤”

package com.baizhi.nick.action;

import java.awt.image.BufferedImage;

import java.io.IOException;

import javax.imageio.ImageIO;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.baizhi.nick.entity.Users;

import com.baizhi.nick.service.UserServiceImpl;

import com.baizhi.nick.validimage.SecurityCode;

import com.baizhi.nick.validimage.SecurityImage;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.util.ValueStack;

public class UserAction extends ActionSupport {

private Users user;

private String clientCode;

//登陸

public String loginUser(){

System.out.println("---- im loginUser---- "+user);

//值棧

ValueStack vs=ActionContext.getContext().getValueStack();

//get serviceCode

String serviceCode = (String) vs.findValue("#session.code");

if(!serviceCode.equals(clientCode)){

vs.setValue("#request.errMsg", "驗證碼不正確");

return "errorlogin";

}

//invoking service

UserServiceImpl service=new UserServiceImpl();

user=service.loginUser(user);

if(user==null){

vs.setValue("#request.errMsg", "改用戶未註冊,請註冊!");

return "errorlogin";

}

//set user

vs.setValue("#session.user", user);

System.out.println("查詢結果:"+user);

return "queryAll";

}

//註冊

public String registUser(){

UserServiceImpl service=new UserServiceImpl();

if(user.getUsername()==null){

return "errorregist";

}

service.registUser(user);

return "login";

}

//驗證碼

public String getImage() throws IOException{

//1 獲取4為驗證碼

String serviceCode = SecurityCode.getSecurityCode();

//2 把驗證碼--》session

ActionContext.getContext().getValueStack().setValue("#session.code", serviceCode);

//3 根據驗證碼 生成圖片

BufferedImage bufferedImage = SecurityImage.createImage(serviceCode);

//4 把圖片寫到client

HttpServletResponse response = ServletActionContext.getResponse();

response.setContentType("image/jpeg");

ServletOutputStream out = response.getOutputStream();

ImageIO.write(bufferedImage, "jpg", out);

return null;

}

public Users getUser() {

return user;

}

public void setUser(Users user) {

this.user = user;

}

public String getClientCode() {

return clientCode;

}

public void setClientCode(String clientCode) {

this.clientCode = clientCode;

}

}

3:配置文件xml

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

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd" >

<struts>

<package name="users" extends="basepage" namespace="/user">

<action name="loginUser" class="com.baizhi.nick.action.UserAction" method="loginUser">

<result name="errorlogin">/jsp/login.jsp</result>

<result name="queryAll" type="redirectAction">

<param name="namespace">/person</param>

<param name="actionName">queryAll</param>

</result>

</action>

<!-- 驗證碼 -->

<action name="imageAction" class="com.baizhi.nick.action.UserAction" method="getImage">

</action>

</package>

<include file="com/baizhi/nick/struts/struts-person.xml"></include>

</struts>

登陸頁面的驗證碼生成與驗證