java验证码及其刷新
1.servlet产生验证码:
package com.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Img extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
OutputStream out=response.getOutputStream();
int w=80,h=20;
BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics g=img.getGraphics();
Random ran=new Random();
g.fillRect(0, 0, w, h);
g.setFont(new Font("Times New Roman",Font.ITALIC,18));
String sRan="";
for(int i=0;i<4;i++){
String rand=String.valueOf(ran.nextInt(10));
sRan+=rand;
g.setColor(new Color(20+ran.nextInt(110),20+ran.nextInt(110),20+ran.nextInt(110)));
g.drawString(rand,20*i+6,16);
}
request.getSession().setAttribute("ch", sRan);
g.dispose();
ImageIO.write(img, "jpeg", out);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
2.嵌入html中
<img src="Img" >
3.验证码的刷新
1)点击验证码刷新
<img
相关文档:
import
java.util.Random;
/**
*
排序测试类
*
*
排序算法的分类如下:
*
1.
插入排序(直接插入排序、折半插入排序、希尔排序);
*
2.
交换排序(冒泡泡排序、快速排序);
*
3.
选择排序(直接选择排序、堆排序);
  ......
/**
* 处理时间异常 Date to String
* 如果转换出现异常会给默认值00:00
* @param time
* @param sdf
* @return
*/
public static String handleDateParseException(Object time){
String tempTime = "00:00";
try{
......
1. 首先String不属于8种基本数据类型,String是一个对象。
因为对象的默认值是null,所以String的默认值也是null;但它又是一种特殊的对象,有其它对象没有的一些特性。
2. new String()和new String(“”)都是申明一个新的空字符串,是空串不是null;
3. String str=”kvill”;
String str=n ......
package org.mingyuan.fetcher;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
i ......
文中引用了孙老师的代码,并注明。
import java.io.*;
import java.net.*;
public class EchoServer {
private int port=8888;
private ServerSocket serverSocket;
public EchoServer() throws IOException {
serverSocket = new ServerSocket(port);
System.out.println("服务器启动");
......