当前位置: 代码迷 >> Java Web开发 >> 登陆的时候怎么增加一个链接 点击一上刷新一上验证码 大神们进来帮个忙
  详细解决方案

登陆的时候怎么增加一个链接 点击一上刷新一上验证码 大神们进来帮个忙

热度:8070   发布时间:2013-02-25 21:10:13.0
登陆的时候如何增加一个链接 点击一下刷新一下验证码 大神们进来帮个忙
登陆页面如何增加一个链接 比如我们常用的功能 验证码看不清楚 点击一下旁边的链接 刷新一下验证码 求大神指导 小弟不胜感激

------解决方案--------------------------------------------------------
登录jsp中:
<img src="/identityServlet.do" id="identity" />
<input type="button" value="图片看不清,换个图片" onclick="reloadImage()" >
js中:
<script>
function reloadImage(){
document.getElementById('identity').src='/identityServlet.do?time='+new Date().getTime();
}
</script>
------解决方案--------------------------------------------------------
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class IdentityServlet extends Action {

/**

*/
private static final long serialVersionUID = -479885884254942306L;

public static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

public static Random random = new Random();

public static String getRandomString() {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 6; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}

public static Color getRandomColor() {
return new Color(random.nextInt(255), random.nextInt(255), random
.nextInt(255));
}

public static Color getReverseColor(Color c) {
return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c
.getBlue());
}



@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setContentType("image/jpeg");

String randomString = getRandomString();
request.getSession(true).setAttribute("randomString", randomString);

int width = 100;
int height = 30;

Color color = getRandomColor();
Color reverse = getReverseColor(color);

BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
//g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
g.setColor(color);
g.fillRect(0, 0, width, height);
g.setColor(reverse);
g.drawString(randomString, 18, 20);
for (int i = 0, n = random.nextInt(100); i < n; i++) {
g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
}

// 转成JPEG格式
ServletOutputStream out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.flush();
return super.execute(mapping, form, request, response);
}


// public void doGet(HttpServletRequest request, HttpServletResponse response)
// throws ServletException, IOException {
//
//
//
// }
}
  相关解决方案