代码我就不写了,估计你也会的。
就给几个意见:
验证码输入框最好与验证码图片在一行。(输入框适当短一点毕竟只需要4个字母)
验证码图片的边框可以根据页面背景的颜色色系接近(可以是蓝色)
整个登入界面的几个输入,可以紧凑一些么不需要为了撑开页面而故意分那么大,不好看(可以在表单位置再加一个单独背景图片使其更加突出)
希望对你有帮助。
下面是关键代码,如果剩下的你都搞不懂,我就无语了JS
<script type="text/javascript" language="javascript">
function reloadcodeOne(){//刷新验证码函数
var verify = document.getElementById('checkCodeImg')
verify.setAttribute('src', 'validateCode?dt=' + Math.random())
}
<script type="text/javascript" >
html
<p>
<label>验证码:</label>
<input class="code" value="请输入验证码" title="请输入验证码" name="rendCode" id="rendCode" onfocus="if (value =='请输入验证码'){value =''}" onblur="if (value ==''){value='请输入验证码'}" type="text" size="6" />
<span><img id="checkCodeImg" src="validateCodeServlet" onclick="javascript:reloadcodeOne()" alt="" width="75" height="24" /></span>
</p>
java代码
package com.zhihui.action.common
import java.awt.Color
import java.awt.Font
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.util.Random
import javax.imageio.ImageIO
import javax.servlet.ServletException
import javax.servlet.ServletOutputStream
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpSession
import org.apache.struts2.ServletActionContext
import com.zhihui.action.base.BaseAction
/**
* <p>
* 校验码控制器
* </p>
*
* @author liurong
* @version ValidateCodeServlet.java,v 0.1 2008-11-20 上午09:22:31 Administrator
* Exp
*/
public class ValidateCodeAction extends BaseAction {
private static final long serialVersionUID = 1L
// 验证码图片的宽度。
private int width = 10
// 验证码图片的高度。
private int height = 5
// 验证码字符个数
private int codeCount = 5
private int x = 0
// 字体高度
private int fontHeight
private int codeY
/*char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' }*/
char[] codeSequence = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }
HttpServletRequest req=ServletActionContext.getRequest()
HttpServletResponse resp=ServletActionContext.getResponse()
public String execute()
throws ServletException, java.io.IOException {
// 宽度
String strWidth = "70"
// 高度
String strHeight = "22"
// 字符个数
String strCodeCount = "5"
width = Integer.parseInt(strWidth)
height = Integer.parseInt(strHeight)
codeCount = Integer.parseInt(strCodeCount)
x = width / (codeCount)
fontHeight = height - 4
codeY = height - 4
// 定义图像buffer
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB)
Graphics2D g = buffImg.createGraphics()
Random random = new Random()
// 将图像填充为白色
g.setColor(Color.WHITE)
g.fillRect(0, 0, width, height)
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight)
g.setFont(font)
g.setColor(Color.BLACK)
g.drawRect(0, 0, width - 1, height - 1)
g.setColor(Color.BLACK)
for (int i = 0i <15i++) {
int x = random.nextInt(width)
int y = random.nextInt(height)
int xl = random.nextInt(8)
int yl = random.nextInt(8)
g.drawLine(x, y, x + xl, y + yl)
}
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer()
int red = 0, green = 0, blue = 0
for (int i = 0i <codeCounti++) {
String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)])
red = 0//random.nextInt(255)
green = 0//random.nextInt(255)
blue = 0//random.nextInt(255)
g.setColor(new Color(red, green, blue))
g.drawString(strRand, (i ) * x, codeY)
randomCode.append(strRand)
}
HttpSession session = req.getSession()
session.setAttribute("validateCode", randomCode.toString())
resp.setHeader("Pragma", "no-cache")
resp.setHeader("Cache-Control", "no-cache")
resp.setDateHeader("Expires", 0)
resp.setContentType("image/jpeg")
ServletOutputStream sos = resp.getOutputStream()
ImageIO.write(buffImg, "jpeg", sos)
sos.close()
return null
}
public int getWidth() {
return width
}
public void setWidth(int width) {
this.width = width
}
public int getHeight() {
return height
}
public void setHeight(int height) {
this.height = height
}
public int getCodeCount() {
return codeCount
}
public void setCodeCount(int codeCount) {
this.codeCount = codeCount
}
public int getX() {
return x
}
public void setX(int x) {
this.x = x
}
public int getFontHeight() {
return fontHeight
}
public void setFontHeight(int fontHeight) {
this.fontHeight = fontHeight
}
public int getCodeY() {
return codeY
}
public void setCodeY(int codeY) {
this.codeY = codeY
}
public char[] getCodeSequence() {
return codeSequence
}
public void setCodeSequence(char[] codeSequence) {
this.codeSequence = codeSequence
}
public HttpServletRequest getReq() {
return req
}
public void setReq(HttpServletRequest req) {
this.req = req
}
public HttpServletResponse getResp() {
return resp
}
public void setResp(HttpServletResponse resp) {
this.resp = resp
}
}