问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

...的格式验证。在用户登录功能中试加入图片验证码功能

发布网友 发布时间: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

你慕课网上找找吧,应该有视频和源码!
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
求抛物线y×y-6x=0的焦点坐标和准线方程 6-6x=6的解是x=0 .( ) 判断题 财富 一个方程问题:6x=0有没有解 6x=0,x=( ) 下列算式是方程的是( )A.101-1=100B.8x-2C.6x=0D.x-17 6X=0,这个方程没有解 对吗? 6x=0不是方程.××.(判断对错) 6x=0是方程. __ (判断对错) 今晚地球停电一小时,做点什么好呢 月经第八天还有褐色分泌物预防什么病 如何把html中验证码隐藏起来 做网站需要验证码的html代码 江西空中乘务专业哪个学校好? 江西哪些学校有学空乘专业的? 南昌航空大学怎么样,我要去这学校读空乘,请亲们告诉我真实情况 有哪些大学在南昌招空乘方面的学生啊? 南昌航空大学的空乘专业好吗,一学期学费要多少 江西有哪些学校有空乘专业 或者有哪些外省的学校承认空乘联考!!!急 想学空乘专业,有哪些学校可以推荐? 江西师范高等专科学校空乘专业和四川西南航空专修学院空乘那个好 “芝麻开门”如何用英语翻译? 芝麻用蒙语怎么说 帮我翻译一下吧! 初三最后4个月冲刺安排(对症下药) 闽南话黑芝麻怎么说? 黑芝麻怎么说 芝麻 日语怎么说?是胡麻吗? 芝麻福州话怎么说,我大长乐人居然不知道芝麻怎么说…… 芝麻或烧饼用英语怎么说? 芝麻的英文名怎么发音 “芝麻”的读音是什么? 下胸肌的锻炼容易让人忽略,如何能够提高下胸肌的锻炼? 为什么手机插上了SIM卡却显示没有?打不了电话。天气预报什么的短信也发不进来。 把卡放在手机里打不了电话 iphone6plys手机qq拉黑的人怎么查看 现金支付国税网上申报费用的会计分录 国税证书年费怎么做账 国税金税卡年费370元,说是可以全额抵扣,请问该怎么记账写分录呢? 缴纳国税的会计分录应该怎么做啊? 国税申报费用记入哪个科目 交国税时怎么做帐,是计入应交增值税还是已交增值税 办理地税申报和国税的网上申报的费用,是计入管理费用还是财务费用?明细科目是什么? 老路改造施工方案 国税地税申报,做账问题 道路拆除及恢复施工方案 小区路面塌陷如何修复及施工方案 *开票系统软件服务费听说可以全额抵扣,请问帐务怎么处理,在申报表中怎么填列? 在国税局缴纳的增值税应该怎么记账? 国税税友怎样网上交年费 支付国税中企业所得税的转账手续费怎么做账 混凝土路面修补的施工工艺