62,614
社区成员
发帖
与我相关
我的任务
分享
public class DynamicPasswordServlet extends HttpServlet {
private static final long serialVersionUID = 2766016519012092763L;
private String keyFontName = "Rage Italic";
private int keyWidth = 25;
private int keyHeight = 25;
private int keyFontSize = 48;
/**
*
*/
public DynamicPasswordServlet() {
super();
}
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
int[] imageOrder = null;
try {
String orderString = (String)session.getAttribute( "ctpPassword" );
String timestamps = (String)session.getAttribute("time");
String timestamp = (String)request.getParameter("time");
if (orderString == null || orderString.length() == 0 ||(!timestamp.equals(timestamps))) // 没有生成,重新生成
{
DynamicPasswordBean bean = new DynamicPasswordBean();
imageOrder = bean.generateRadomSerial(10);
// imageOrder = new int[]{ 4,7,2,3,6,5,1,9,0,8};
orderString = packOrder(imageOrder);
session.setAttribute( "ctpPassword", orderString );
session.setAttribute( "time", timestamp );
//EMPLog.log( EMPConstance.ATTR_SESSION_CONTEXT, EMPLog.INFO, 0, "解包前1:"+orderString);
}else{
//EMPLog.log( EMPConstance.ATTR_SESSION_CONTEXT, EMPLog.INFO, 0, "解包前2:"+orderString);
imageOrder = parserOrder(orderString, 10);
}
} catch (Exception e) {
System.out.println(e.toString());
}
int idx = Integer.parseInt(request.getParameter("img"));
//EMPLog.log( EMPConstance.ATTR_SESSION_CONTEXT, EMPLog.INFO, 0, "前台键盘Id:"+idx);
// 在内存中创建图象
int width = keyWidth, height = keyHeight;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
// Color color = new Color(0x1A4876);
//g.setColor(new Color(224,224,224));
g.setColor(new Color(255,255,255));
// g.setColor(Color.GREEN);
g.fillRect(0, 0, width, height);
/*g.setColor(new Color(179,179,179));
g.fillRoundRect(0, 0, width, height,width/2,height/2);*/
//画阴影
/*g.setColor(Color.black);
g.drawLine(0, 0, 0, height);
g.drawLine(1, 0, 1, height);
g.drawLine(0, height, width, height);
g.drawLine(0, height-1, width, height-1);*/
// 设定字体
g.setFont(new Font(keyFontName, Font.PLAIN, keyFontSize)); // Arial
//g.setColor(Color.black);
FontMetrics metrics = g.getFontMetrics();
// System.out.println("FontHeight: " + metrics.getHeight());
// System.out.println("FontLeading: " + metrics.getLeading());
// System.out.println("FontAscent: " + metrics.getAscent());
// System.out.println("FontDescent: " + metrics.getDescent());
// try to draw some ramdon line's
/*
Random random = new Random(System.currentTimeMillis() + seed++);
for (int i = 0; i < width / 3; i++) {
Color aColor = new Color(random.nextInt(0xffffff));
g.setColor(aColor);
int x0 = random.nextInt(width);
int y0 = random.nextInt(height);
int xe = random.nextInt(width);
int ye = random.nextInt(height);
//g.drawLine(x0, y0, xe, ye);
}*/
int x0 = (width - metrics.getWidths()['9']) / 2;
int y0 = metrics.getAscent()+ metrics.getLeading() / 2+2; // /2 +
// metrics.getAscent();
/*int colorIdx = random.nextInt(4);
if (colorIdx == 0)
g.setColor(new Color(24, 73, 181)); // Color.RED );
if (colorIdx == 1)
g.setColor(new Color(214, 36, 8)); // Color.YELLOW );
if (colorIdx == 2)
g.setColor(new Color(16, 150, 24)); // Color.GREEN );
if (colorIdx == 3)
g.setColor(new Color(239, 186, 0)); // Color.BLUE );
*/
// g.setXORMode(Color.GREEN);
//g.setColor(Color.black);
g.setColor(new Color(70,70,70));
g.drawString(String.valueOf(imageOrder[idx]), x0, y0);
//EMPLog.log( EMPConstance.ATTR_SESSION_CONTEXT, EMPLog.INFO, 0, "键盘"+idx+"加密生成:"+imageOrder[idx]);
// 输出密码序号图片
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
JPEGImageEncoder encoder = JPEGCodec
.createJPEGEncoder(outputStream);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
param.setQuality(1.0F,true);
encoder.setJPEGEncodeParam(param);
encoder.encode(image);
outputStream.flush();
} catch (IOException ex) {
throw ex;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex) {
}
}
}
}
private String packOrder(int order[]){
String ordS = "";
for(int i=0; i<order.length; i++){
ordS += String.valueOf(order[i]) + "|";
}
return ordS;
}
private int[] parserOrder(String orderS , int maxValue){
int serialValue[] = new int[maxValue];
String tmp = orderS;
int i = 0;
int indx = -1;
while((indx = tmp.indexOf('|')) > 0){
serialValue[i] = Integer.parseInt(tmp.substring(0,indx));
tmp = tmp.substring(indx+1);
i++;
}
return serialValue;
}
public void init(ServletConfig sc) {
try {
super.init(sc);
// private String certifyFontName = "Rage Italic";
// private String keyFontName = "Rage Italic";
//
// private int keyWidth = 48;
// private int keyHeight = 48;
String tmpValue;
tmpValue = getInitParameter("keyFontName");
if (tmpValue != null)
keyFontName = tmpValue;
tmpValue = getInitParameter("keyFontSize");
if (tmpValue != null)
keyFontSize = Integer.parseInt(tmpValue);
tmpValue = getInitParameter("keyWidth");
if (tmpValue != null)
keyWidth = Integer.parseInt(tmpValue);
tmpValue = getInitParameter("keyHeight");
if (tmpValue != null)
keyHeight = Integer.parseInt(tmpValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
<input type="text" name="tempCheckCode" id="tempCheckCode" size="12" maxlength="4" style="height:16px;" /> <img id="verifyImg" src="VerifyImage" align="absmiddle" onclick="this.src='VerifyImage?update=' + Math.random()" style="cursor:pointer;" title="点击图片可刷新验证码"/>
看不清?<a href="#1" onclick="javascript:document.getElementById('verifyImg').src='VerifyImage?update=' + Math.random()" class="blue12">换一张</a>
/*
放进1个,苹果ID是0苹果筐共有1个苹果
取出1个,苹果ID是0苹果筐共有0个苹果
放进1个,苹果ID是1苹果筐共有1个苹果
放进1个,苹果ID是2苹果筐共有2个苹果
取出1个,苹果ID是1苹果筐共有1个苹果
放进1个,苹果ID是3苹果筐共有2个苹果
取出1个,苹果ID是2苹果筐共有1个苹果
放进1个,苹果ID是4苹果筐共有2个苹果
取出1个,苹果ID是3苹果筐共有1个苹果
取出1个,苹果ID是4苹果筐共有0个苹果
放进1个,苹果ID是5苹果筐共有1个苹果
取出1个,苹果ID是5苹果筐共有0个苹果
放进1个,苹果ID是6苹果筐共有1个苹果
放进1个,苹果ID是7苹果筐共有2个苹果
放进1个,苹果ID是8苹果筐共有3个苹果
取出1个,苹果ID是6苹果筐共有2个苹果
取出1个,苹果ID是7苹果筐共有1个苹果
放进1个,苹果ID是9苹果筐共有2个苹果
取出1个,苹果ID是8苹果筐共有1个苹果
取出1个,苹果ID是9苹果筐共有0个苹果
*/
import java.util.*;
public class TestPutAndGetApple {
List<Apple> apples = null;
public int appleSize = 0;//苹果总数 苹果数有限制
public TestPutAndGetApple(int appleSize) {
this.appleSize = appleSize;
apples = new ArrayList<Apple>(appleSize);;
}
public static void main(String[] args) {
TestPutAndGetApple tpaga = new TestPutAndGetApple(100);
new Thread(new TestPutAppleRunnable(tpaga)).start();
new Thread(new TestGetAppleRunnable(tpaga)).start();
}
}
class Apple {
static int count = 0;
final int id = count++;
public int getId() {
return id;
}
}
class TestPutAppleRunnable implements Runnable {
int putAppleCount = 0;
TestPutAndGetApple tpaga;
public TestPutAppleRunnable(TestPutAndGetApple tpaga) {
this.tpaga = tpaga;
}
public void run() {
while (putAppleCount<tpaga.appleSize) {
Apple apple = new Apple();
putAppleCount++;
tpaga.apples.add(apple);
System.out.println("放进1个,苹果ID是"+ apple.getId()+"苹果筐共有" + tpaga.apples.size()
+ "个苹果");
try {
Thread.sleep((int) (Math.random() * 2000));//速度太快了,延迟一下
} catch (InterruptedException e) {
}
}
}
}
class TestGetAppleRunnable implements Runnable {
TestPutAndGetApple tpaga;
public TestGetAppleRunnable(TestPutAndGetApple tpaga) {
this.tpaga = tpaga;
}
public void run() {
while (true) {
if (!tpaga.apples.isEmpty()) {
Apple apple = tpaga.apples.remove(0);
System.out.println("取出1个,苹果ID是"+ apple.getId()+"苹果筐共有" + tpaga.apples.size()
+ "个苹果");
}
try {
Thread.sleep((int) (Math.random() * 2000));//速度太快了,延迟一下
} catch (InterruptedException e) {
}
}
}
}
select a.name
from class a,
(select b.classid as classid,
(select count(1)
from (select classid, count(1) as cnt
from student
group by classid) c
where c.cnt > (select count(1)
from student
where classid = b.classid)
) as rank
from stuent b) d
where a.id = d.classid
and d.rank < 5
order by d.rank
List<Integer> list = new LinkedList<Integer>();
for (int i=0; i<30; i++) { //准备30个数
list.add(i+1);
}
List<Integer> result = new ArrayList<Integer>();
for (int i=0; i<7; i++) { //随机取7个放到结果集
result.add(list.remove((int)(Math.random()*list.size())));
}
Collections.sort(result); //排序
for (Integer i : result) { //打印
System.out.println(i);
}