jsp页面该如何刷新验证码
我的验证码是用servlet写的,代码如下点击图片后,提示说yzmServlet没有定义,求正确写法...
我的验证码是用servlet写的,代码如下
点击图片后,提示说yzmServlet没有定义,求正确写法 展开
点击图片后,提示说yzmServlet没有定义,求正确写法 展开
6个回答
展开全部
(1)jsp代码:
<img id = "img_authcode" src="${ctx}/account/authcode" /><a href="javascript:;" onclick="javascript:document.getElementById('img_authcode').setAttribute('src', '${ctx}/account/authcode?' + Math.random())">换一换</a>
(2)java代码(该代码为我自己框架代码,跟servlet写法不一样的我都给你注释了):
public View authcode() throws IOException {
HttpServletResponse response = PuffContext.getResponse();//获取response
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String authCode = AuthCodeUtil.getRandom(4); //获取验证码,代码在下面(3)
System.out.println("生成随机码:" + authCode);
PuffContext.getSession().setAttribute("session_authcode", authCode);//把该验证码存储在session
ServletOutputStream output = response.getOutputStream();
AuthCodeUtil.draw(output, authCode);
output.flush();
output.close();
return ViewFactory.nullView();//返回null
}
(3)///////////////////////////下面为生成验证码类////////////////////////////////////
public class AuthCodeUtil {
private final static Random random = new Random();
// 随机字体样式
private final static int[] fontStyle = { Font.HANGING_BASELINE, Font.ITALIC, Font.LAYOUT_LEFT_TO_RIGHT, Font.LAYOUT_NO_LIMIT_CONTEXT,
Font.LAYOUT_NO_START_CONTEXT, Font.LAYOUT_RIGHT_TO_LEFT };
/**
* 画随机码图
*
* @param out
* @param width
* @param height
* @throws IOException
*/
public static void draw(OutputStream out, String value) throws IOException {
int width = 80, height = 30;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.drawRect(1, 1, width - 2, height - 2);
for (int i = 0; i < 10; i++) {
g.setColor(randColor(150, 250));
g.drawOval(random.nextInt(110), random.nextInt(24), 5 + random.nextInt(10), 5 + random.nextInt(10));
}
int n = (int) (Math.random() * 6);
Font mFont = new Font("Arial", fontStyle[n], 23);
g.setFont(mFont);
g.setColor(randColor(10, 240));
g.drawString(value, 10, 21);// 随机数,水平距离,垂直距离
ImageIO.write(bi, "png", out);
}
private static Color randColor(int fc, int bc) {// 给定范围获得随机颜色
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("d:\\aa.png");
draw(out, getRandom(4));
}
public static String getRandom(int size) {// 随机字符串
char[] c = { '1', '3', '5', '6', '7', '8', '9' };
StringBuffer sb = new StringBuffer();
for (int i = 0; i < size; i++)
sb.append(c[Math.abs(random.nextInt()) % c.length]);
return sb.toString();
}
}
<img id = "img_authcode" src="${ctx}/account/authcode" /><a href="javascript:;" onclick="javascript:document.getElementById('img_authcode').setAttribute('src', '${ctx}/account/authcode?' + Math.random())">换一换</a>
(2)java代码(该代码为我自己框架代码,跟servlet写法不一样的我都给你注释了):
public View authcode() throws IOException {
HttpServletResponse response = PuffContext.getResponse();//获取response
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String authCode = AuthCodeUtil.getRandom(4); //获取验证码,代码在下面(3)
System.out.println("生成随机码:" + authCode);
PuffContext.getSession().setAttribute("session_authcode", authCode);//把该验证码存储在session
ServletOutputStream output = response.getOutputStream();
AuthCodeUtil.draw(output, authCode);
output.flush();
output.close();
return ViewFactory.nullView();//返回null
}
(3)///////////////////////////下面为生成验证码类////////////////////////////////////
public class AuthCodeUtil {
private final static Random random = new Random();
// 随机字体样式
private final static int[] fontStyle = { Font.HANGING_BASELINE, Font.ITALIC, Font.LAYOUT_LEFT_TO_RIGHT, Font.LAYOUT_NO_LIMIT_CONTEXT,
Font.LAYOUT_NO_START_CONTEXT, Font.LAYOUT_RIGHT_TO_LEFT };
/**
* 画随机码图
*
* @param out
* @param width
* @param height
* @throws IOException
*/
public static void draw(OutputStream out, String value) throws IOException {
int width = 80, height = 30;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.drawRect(1, 1, width - 2, height - 2);
for (int i = 0; i < 10; i++) {
g.setColor(randColor(150, 250));
g.drawOval(random.nextInt(110), random.nextInt(24), 5 + random.nextInt(10), 5 + random.nextInt(10));
}
int n = (int) (Math.random() * 6);
Font mFont = new Font("Arial", fontStyle[n], 23);
g.setFont(mFont);
g.setColor(randColor(10, 240));
g.drawString(value, 10, 21);// 随机数,水平距离,垂直距离
ImageIO.write(bi, "png", out);
}
private static Color randColor(int fc, int bc) {// 给定范围获得随机颜色
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("d:\\aa.png");
draw(out, getRandom(4));
}
public static String getRandom(int size) {// 随机字符串
char[] c = { '1', '3', '5', '6', '7', '8', '9' };
StringBuffer sb = new StringBuffer();
for (int i = 0; i < size; i++)
sb.append(c[Math.abs(random.nextInt()) % c.length]);
return sb.toString();
}
}
展开全部
我从头到尾说一遍吧
通过一个serlet绘制验证码,如DispCodeServlet
public class DispCodeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
Random random=new Random();
int length =5;
String checkcode="";
char code;
int number;
for(int i=0; i<length;i++){
number=random.nextInt(26);
if(number%2==0)
code=(char)('0'+(char)(number%10));
else
code=(char)('A'+(char)(number%26));
checkcode+=code+"";
}
int width=(int)Math.ceil(length*12.5),height=22;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.setColor(Color.GRAY);
g.drawRect(0,0,width-1,height-1);
for(int i=0;i<25;i++){
int x1=random.nextInt(width);
int y1=random.nextInt(height);
int x2=random.nextInt(width);
int y2=random.nextInt(height);
g.setColor(Color.GRAY);
g.drawLine(x1,y1,x2,y2);
}
g.setColor(Color.BLUE);
g.setFont(new Font("Arial",Font.BOLD|Font.ITALIC,16));
g.drawString(checkcode,5,18);
HttpSession session=request.getSession();
session.setAttribute("rand", checkcode);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
在jsp页面中使用画验证码,如下
<input type=text name=checkcode><img id="cc"
src="DispCodeServlet"><input type=button name=btn1 value="刷新"
onclick="refresh()">
然后在javascript中写入refresh()方法
function refresh() {
document.getElementById("cc").src = "DispCodeServlet?a="
+ Math.random();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<label>验证码</label><input id="code" type="text"/ maxlength="4" tabindex="3"><img id="checkCode" src="imgcode.jsp?random_mark=1111" width="83" height="24" onclick="javascript:changeCode();"/>
imgcode.jsp就是输出的一个验证码, 然后changeCode是个js函数, 点一下就更换random_mark=1111 这个的值, 从而达到刷新的效果。希望采纳
imgcode.jsp就是输出的一个验证码, 然后changeCode是个js函数, 点一下就更换random_mark=1111 这个的值, 从而达到刷新的效果。希望采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
this.src=你能访问到serlvet的路径;最好在这个路径的后面放一个随机数Math.random();不需要接收这个参数,这只是告诉后台,这个路径跟你原来的是不一样的。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
连接后面加编号,每次刷新编号+1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询