...的格式验证。在用户登录功能中试加入图片验证码功能
发布网友
发布时间:2022-05-04 20:14
我来回答
共3个回答
热心网友
时间:2022-04-21 13:59
下面是关键代码,如果剩下的你都搞不懂,我就无语了
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.hui.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.hui.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 = 0; i < 15; i++) {
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 = 0; i < codeCount; i++) {
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;
}
}追问然而我并不会 我需要一个可以直接运行的代码 包括注册和登录和验证码部分 谢谢了 我的邮箱hell 0 4 3 6@ foxmail . com
热心网友
时间:2022-04-21 15:17
你就直接说用html5写个图片验证不就得了 邮箱留下
热心网友
时间:2022-04-21 16:52
你慕课网上找找吧,应该有视频和源码!