关于生成验证码!! 在jsp中怎么生成验证码啊??需要添加额外的包吗????看了个方式!!是添加个jGraphic 那怎么做成验证码是”花“的??就是那种防止用读取像素的方法电脑自动获得验证码!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不要,给你一段代码吧,也不知道你要的是不是这种效果servlet:package com.jhc;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.PrintWriter;import java.util.Random;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class YzmServlet extends HttpServlet { /** * Constructor of the object. */ public YzmServlet() { super(); } public char[] getRand() { char ch[] = new char[4]; String s = "ABCDEFGHJKLMNPQRSTUVWXY3456789"; for (int i = 0; i < 4; i++) { Random r = new Random(); ch[i] = s.charAt(r.nextInt(s.length())); } return ch; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedImage image = new BufferedImage(80, 30, 4); Graphics g = image.getGraphics(); g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, 80, 30); g.setColor(Color.blue); char ch[] = this.getRand(); g.setFont(new Font("宋体", Font.BOLD, 20)); g.drawString("" + ch[0], 8, 15); g.setColor(Color.magenta); g.drawString("" + ch[1], 24, 22); g.setColor(Color.magenta); g.drawString("" + ch[2], 42, 18); g.setColor(Color.black); g.drawString("" + ch[3], 56, 25); g.setColor(Color.black); Random r = new Random(); g.drawLine(r.nextInt(60), r.nextInt(20), r.nextInt(60), r.nextInt(20)); g.setColor(Color.green); g.drawLine(r.nextInt(60), r.nextInt(20), r.nextInt(60), r.nextInt(20)); g.dispose(); response.setContentType("image/jpeg"); ServletOutputStream sos = response.getOutputStream(); JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(sos); jpeg.encode(image); //request.getSession().setAttribute("yzm", new String(ch)); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}jsp:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'yzm.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --><script></script> </head> <body> <form name="form1" method="post" action="" > <img width="56" height="29" src="YzmServlet" id="img"></td> </form> </body></html> 多人同时操作一条数据如何保证数据准确性 在ssh框架的项目中,实现像淘宝一样的在结果中再次查询 jdom有没有中文文档? 从作用域中取值的一个问题 j2ee错误 大家进来看一下? hidden是如何使用的 java程序不能运行 http、session相关问题 如何使jdom生成的xml文件不是一行? 极菜问:如何锁住文件不让别的程序或人修改删除,高分求教 JDK与SDK的区别,如何从官网下载到JavaEE的src 想做一个简单的查询系统
servlet:package com.jhc;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;public class YzmServlet extends HttpServlet { /**
* Constructor of the object.
*/
public YzmServlet() {
super();
} public char[] getRand() {
char ch[] = new char[4];
String s = "ABCDEFGHJKLMNPQRSTUVWXY3456789";
for (int i = 0; i < 4; i++) {
Random r = new Random();
ch[i] = s.charAt(r.nextInt(s.length()));
}
return ch;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { BufferedImage image = new BufferedImage(80, 30, 4);
Graphics g = image.getGraphics();
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, 80, 30);
g.setColor(Color.blue); char ch[] = this.getRand();
g.setFont(new Font("宋体", Font.BOLD, 20));
g.drawString("" + ch[0], 8, 15);
g.setColor(Color.magenta);
g.drawString("" + ch[1], 24, 22);
g.setColor(Color.magenta);
g.drawString("" + ch[2], 42, 18);
g.setColor(Color.black);
g.drawString("" + ch[3], 56, 25);
g.setColor(Color.black);
Random r = new Random();
g.drawLine(r.nextInt(60), r.nextInt(20), r.nextInt(60), r.nextInt(20));
g.setColor(Color.green);
g.drawLine(r.nextInt(60), r.nextInt(20), r.nextInt(60), r.nextInt(20));
g.dispose();
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(sos);
jpeg.encode(image);
//request.getSession().setAttribute("yzm", new String(ch));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doGet(request, response);
}}jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'yzm.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script></script>
</head>
<body>
<form name="form1" method="post" action="" >
<img width="56" height="29" src="YzmServlet" id="img"></td>
</form>
</body>
</html>