Servlet实现数字字母验证码图片(一):

生成验证码图片主要用到了一个BufferedImage类,如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcwAAAEiCAIAAAD793UjAAAT/ElEQVR4nO3dzXHjPLOGYcblHJSGo/HC61l9MbwLRzEZOImTAc9CNocCuhsNEE2C5H2Va0qCQPxQ5GOIkjzTDAAIMx09AAC4sp+QnQAA/Qghe0DCA8AVEbIAEIiQBYBAhCwABCJkASAQIQsAgQhZAAhEyAJAIEIWAAK1hGzyHYZruOSkABzumitZEhPAIAhZAAhUF7LinzwQy5e7Hx8fxqPOKNRaeDaedKH9aYaqSdnteMq11jzzBXAZ0/Zrsnn2td32dJfcTkJWG6GHmJhi+8Xbzv0D4A46hOy60B+mtYs7sf1DQrY4X3EwtfMFcA19QtaTL576xR4HCdnifO2WAdxHh5AVV3Pru8762kLPbv/Pnz/OXNPa1zbRQtZz2zl+AJfXIWTn1zA1ItWub4Sg1r4WsmJrWvvGYPI31jzjz3vRygFc3tQlZDGzYgUgqQhZlmNF7CIAiZaVLADAiZAFgECELAAEImQBIBAhCwCBuoXs9+fb9P7VZ1AAcBVtIfv9+TZNr5n6LJpWxc+St8/vnuMFgFNpXsl+vb9m6k/I/svUpcLEChfAbW25XPCSs0nI/jzGOnYf359v/C4DhrTpmuzvJYIsZJ8Ru+2k/3f9QdAhT1YrbatJZ7WjrPfSemj63htsAsDVdX3j6ydkv95fz+UNq9p1jL80ti0tXkf020k2QGe1w/2MU9wh+R4EsKeakDXXli7V+SRFxDKM1txILx8r6emsNgBCFhjX9pXsz4cI3t//JXC/GBIjYmvaZe/Rbao2AEIWGNfWkF1eUX89T+b3r5f17taEcofs9+db3lt63eJ1e0IWQLwtIfty/n7/hmz66Kagsi4XJJdKp9Lnx6yrHav2ndUW5jtjrw++fX4n7WuXgc0eszY+G0I27ejZ0te6YWMm5f6WOsuGyVwHf0cR6GVqDNlsNZmF7DzP6zOpLWeziJAvFUhvrelLuH4r2bRf9S2+5wO/11TePr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ab8uh9a1Vnzs7SWWFULtb0L82cXfsXLG2LLHsA/o6Mu/l+NiK12HutiCfOeLRhsdn2bOaVCdljDRSybZcL7Kacj6aVe4ds7QnmWRDtVhg3o2Q/++NDrul7paINqVgy8k7273nsryJk8xfs6xvJ3eR1vbh5Ur6+m7ep1cw3NO46z0NxhSW+vhPLl4fyknXlqjan7DVjfre5plFfG6dYszjNdcl6PxtD8vdeFbIN0zR2l7a5s03/NP3P+7oF5z5BkOqVbG2jwBZJZhUqD3bgHR5whw8Ac0TI+heMgIe2RnupM94h5xn2VXvHWshK9ry014b8DP7DU9x3L6EjQhYAAhGyABCIkAWAQIQsAAQiZAEgECELAIEIWQAIRMgCQCBCFgACySELAOglDVkAQARCFgAC/T/3B6pmzPIFFAAAAABJRU5ErkJggg==" alt="" />

1、引入jquery-1.10.2.js文件,创建verifyCode.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>Java验证码</title>
</head> <body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100"></td>
<td width="100">验证码:</td>
<td width="150"><input id="veryCode" name="veryCode" type="text"
class="yzm" value="" onmouseout="isRightCode()" /></td>
<td width="135"><img id="imgObj" alt="" src="${pageContext.request.contextPath}/DrawImage"
onclick="changeImg()" /></td>
<td><a href="javascript:void(0);" onclick="changeImg()">看不清,换一张</a></td>
<td></td>
</tr>
<tr>
<td style="line-height: 38px; color: #FF0000; text-align: center;"
colspan="4" id="info"></td>
<td></td>
</tr>
</table>
</body>
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<script type="text/javascript">
/**
* 验证code-js
*/ // 点击换一张验证码
function changeImg() {
var imgSrc = $("#imgObj");
var src = imgSrc.attr("src");
//时间戳 ,为了使每次生成图片不一致,即不让浏览器读缓存,所以需要加上时间戳
var timestamp = (new Date()).valueOf();
src =src +"?timestamp=" + timestamp;;
imgSrc.attr("src", src);
$("#info").html("");
}
// 验证码验证
function isRightCode() {
var code = $("#veryCode").val();
alert("code="+code);
code = "c=" + code;
$.ajax({
type : "POST",
url : "resultServlet",
data : code,
success :function callback(data) {// 验证以后处理提交信息或错误信息
if (data.toString() == 1) {
$("#info").html("提示您:验证成功了!");
return;
} else {
$("#info").html(data);
return;
}
}
});
}
</script>
</html>

2.创建画图servlet,DrawImage.java

 package com.utils;

 import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random; import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* 生成随机验证码图片
*
* @author huang
*
*/
@WebServlet("/DrawImage")
public class DrawImage extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 120;
public static final int HEIGHT = 30; // 在报java.lang.NoClassDefFoundError: Could not initialize class
// javax.imageio.ImageIO错的时候加上这个很管用。
static {
ImageIO.scanForPlugins();
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// 创建缓存
BufferedImage bi = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
// 获得画布
Graphics g = bi.getGraphics();
// 设置背影色
setBackGround(g);
// 设置边框
setBorder(g);
// 画干扰线
drawRandomLine(g);
// 写随机数
String random = drawRandomNum((Graphics2D) g);
// 将随机汉字存在session中
request.getSession().setAttribute("checkcode", random);
// 将图形写给浏览器
response.setContentType("image/jpeg");
// 发头控制浏览器不要缓存
response.setDateHeader("expries", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
// 将图片写给浏览器
ImageIO.write(bi, "jpg", response.getOutputStream());
} /**
* 设置背景色
*
* @param g
*/
private void setBackGround(Graphics g) {
// 设置颜色
g.setColor(new Color(249, 249, 249));
// 填充区域
g.fillRect(0, 0, WIDTH, HEIGHT); } /**
* 设置边框
*
* @param g
*/
private void setBorder(Graphics g) {
// 设置边框颜色
// RGB
g.setColor(new Color(210, 209, 205));
// 边框区域
g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
} /**
* 画随机线条
*
* @param g
*/
private void drawRandomLine(Graphics g) {
// 设置颜色
g.setColor(Color.GRAY);
// 设置线条个数并画线
for (int i = 0; i <; i++) {
int x1 = new Random().nextInt(WIDTH);
int y1 = new Random().nextInt(HEIGHT);
int x2 = new Random().nextInt(WIDTH);
int y2 = new Random().nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
} /**
* 画随机汉字
*
* @param g
* @return
*/
private String drawRandomNum(Graphics2D g) {
StringBuffer sb = new StringBuffer();
// 设置字体
g.setFont(new Font("宋体", Font.BOLD, 20));
// 准备常用汉字集
// String base =
// "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97\u4e5f\u548c\u90a3\u8981\u4e0b\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u597d\u8fd8\u591a\u6ca1\u4e3a\u53c8\u53ef\u5bb6\u5b66\u53ea\u4ee5\u4e3b\u4f1a\u6837\u5e74\u60f3\u751f\u540c\u8001\u4e2d\u5341\u4ece\u81ea\u9762\u524d\u5934\u9053\u5b83\u540e\u7136\u8d70\u5f88\u50cf\u89c1\u4e24\u7528\u5979\u56fd\u52a8\u8fdb\u6210\u56de\u4ec0\u8fb9\u4f5c\u5bf9\u5f00\u800c\u5df1\u4e9b\u73b0\u5c71\u6c11\u5019\u7ecf\u53d1\u5de5\u5411\u4e8b\u547d\u7ed9\u957f\u6c34\u51e0\u4e49\u4e09\u58f0\u4e8e\u9ad8\u624b\u77e5\u7406\u773c\u5fd7\u70b9\u5fc3\u6218\u4e8c\u95ee\u4f46\u8eab\u65b9\u5b9e\u5403\u505a\u53eb\u5f53\u4f4f\u542c\u9769\u6253\u5462\u771f\u5168\u624d\u56db\u5df2\u6240\u654c\u4e4b\u6700\u5149\u4ea7\u60c5\u8def\u5206\u603b\u6761\u767d\u8bdd\u4e1c\u5e2d\u6b21\u4eb2\u5982\u88ab\u82b1\u53e3\u653e\u513f\u5e38\u6c14\u4e94\u7b2c\u4f7f\u5199\u519b\u5427\u6587\u8fd0\u518d\u679c\u600e\u5b9a\u8bb8\u5feb\u660e\u884c\u56e0\u522b\u98de\u5916\u6811\u7269\u6d3b\u90e8\u95e8\u65e0\u5f80\u8239\u671b\u65b0\u5e26\u961f\u5148\u529b\u5b8c\u5374\u7ad9\u4ee3\u5458\u673a\u66f4\u4e5d\u60a8\u6bcf\u98ce\u7ea7\u8ddf\u7b11\u554a\u5b69\u4e07\u5c11\u76f4\u610f\u591c\u6bd4\u9636\u8fde\u8f66\u91cd\u4fbf\u6597\u9a6c\u54ea\u5316\u592a\u6307\u53d8\u793e\u4f3c\u58eb\u8005\u5e72\u77f3\u6ee1\u65e5\u51b3\u767e\u539f\u62ff\u7fa4\u7a76\u5404\u516d\u672c\u601d\u89e3\u7acb\u6cb3\u6751\u516b\u96be\u65e9\u8bba\u5417\u6839\u5171\u8ba9\u76f8\u7814\u4eca\u5176\u4e66\u5750\u63a5\u5e94\u5173\u4fe1\u89c9\u6b65\u53cd\u5904\u8bb0\u5c06\u5343\u627e\u4e89\u9886\u6216\u5e08\u7ed3\u5757\u8dd1\u8c01\u8349\u8d8a\u5b57\u52a0\u811a\u7d27\u7231\u7b49\u4e60\u9635\u6015\u6708\u9752\u534a\u706b\u6cd5\u9898\u5efa\u8d76\u4f4d\u5531\u6d77\u4e03\u5973\u4efb\u4ef6\u611f\u51c6\u5f20\u56e2\u5c4b\u79bb\u8272\u8138\u7247\u79d1\u5012\u775b\u5229\u4e16\u521a\u4e14\u7531\u9001\u5207\u661f\u5bfc\u665a\u8868\u591f\u6574\u8ba4\u54cd\u96ea\u6d41\u672a\u573a\u8be5\u5e76\u5e95\u6df1\u523b\u5e73\u4f1f\u5fd9\u63d0\u786e\u8fd1\u4eae\u8f7b\u8bb2\u519c\u53e4\u9ed1\u544a\u754c\u62c9\u540d\u5440\u571f\u6e05\u9633\u7167\u529e\u53f2\u6539\u5386\u8f6c\u753b\u9020\u5634\u6b64\u6cbb\u5317\u5fc5\u670d\u96e8\u7a7f\u5185\u8bc6\u9a8c\u4f20\u4e1a\u83dc\u722c\u7761\u5174\u5f62\u91cf\u54b1\u89c2\u82e6\u4f53\u4f17\u901a\u51b2\u5408\u7834\u53cb\u5ea6\u672f\u996d\u516c\u65c1\u623f\u6781\u5357\u67aa\u8bfb\u6c99\u5c81\u7ebf\u91ce\u575a\u7a7a\u6536\u7b97\u81f3\u653f\u57ce\u52b3\u843d\u94b1\u7279\u56f4\u5f1f\u80dc\u6559\u70ed\u5c55\u5305\u6b4c\u7c7b\u6e10\u5f3a\u6570\u4e61\u547c\u6027\u97f3\u7b54\u54e5\u9645\u65e7\u795e\u5ea7\u7ae0\u5e2e\u5566\u53d7\u7cfb\u4ee4\u8df3\u975e\u4f55\u725b\u53d6\u5165\u5cb8\u6562\u6389\u5ffd\u79cd\u88c5\u9876\u6025\u6797\u505c\u606f\u53e5\u533a\u8863\u822c\u62a5\u53f6\u538b\u6162\u53d4\u80cc\u7ec6";
// String base =
// "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97\u4e5f\u548c\u90a3\u8981\u4e0b\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u597d\u8fd8\u591a\u6ca1\u4e3a\u53c8\u53ef\u5bb6\u5b66\u53ea\u4ee5\u4e3b\u4f1a\u6837\u5e74\u60f3\u751f\u540c\u8001\u4e2d\u5341\u4ece\u81ea\u9762\u524d\u5934\u9053\u5b83\u540e\u7136\u8d70\u5f88\u50cf\u89c1\u4e24\u7528\u5979\u56fd\u52a8\u8fdb\u6210\u56de\u4ec0\u8fb9\u4f5c\u5bf9\u5f00\u800c\u5df1\u4e9b\u73b0\u5c71\u6c11\u5019\u7ecf\u53d1\u5de5\u5411\u4e8b\u547d\u7ed9\u957f\u6c34\u51e0\u4e49\u4e09\u58f0\u4e8e\u9ad8\u624b\u77e5\u7406\u773c\u5fd7\u70b9\u5fc3\u6218\u4e8c\u95ee\u4f46\u8eab\u65b9\u5b9e\u5403\u505a\u53eb\u5f53\u4f4f\u542c\u9769\u6253\u5462\u771f\u5168\u624d\u56db\u5df2\u6240\u654c\u4e4b\u6700\u5149\u4ea7\u60c5\u8def\u5206\u603b\u6761\u767d\u8bdd\u4e1c\u5e2d\u6b21\u4eb2\u5982\u88ab\u82b1\u53e3\u653e\u513f\u5e38\u6c14\u4e94\u7b2c\u4f7f\u5199\u519b\u5427\u6587\u8fd0\u518d\u679c\u600e\u5b9a\u8bb8\u5feb\u660e\u884c\u56e0\u522b\u98de\u5916\u6811\u7269\u6d3b\u90e8\u95e8\u65e0\u5f80\u8239\u671b\u65b0\u5e26\u961f\u5148\u529b\u5b8c\u5374\u7ad9\u4ee3\u5458\u673a\u66f4\u4e5d\u60a8\u6bcf\u98ce\u7ea7\u8ddf\u7b11\u554a\u5b69\u4e07\u5c11\u76f4\u610f\u591c\u6bd4\u9636\u8fde\u8f66\u91cd\u4fbf\u6597\u9a6c\u54ea\u5316\u592a\u6307\u53d8\u793e\u4f3c\u58eb\u8005\u5e72\u77f3\u6ee1\u65e5\u51b3\u767e\u539f\u62ff\u7fa4\u7a76\u5404\u516d\u672c\u601d\u89e3\u7acb\u6cb3\u6751\u516b\u96be\u65e9\u8bba\u5417\u6839\u5171\u8ba9\u76f8\u7814\u4eca\u5176\u4e66\u5750\u63a5\u5e94\u5173\u4fe1\u89c9\u6b65\u53cd\u5904\u8bb0\u5c06\u5343\u627e\u4e89\u9886\u6216\u5e08\u7ed3\u5757\u8dd1\u8c01\u8349\u8d8a\u5b57\u52a0\u811a\u7d27\u7231\u7b49\u4e60\u9635\u6015\u6708\u9752\u534a\u706b\u6cd5\u9898\u5efa\u8d76\u4f4d\u5531\u6d77\u4e03\u5973\u4efb\u4ef6\u611f\u51c6\u5f20\u56e2\u5c4b\u79bb\u8272\u8138\u7247\u79d1\u5012\u775b\u5229\u4e16\u521a\u4e14\u7531\u9001\u5207\u661f\u5bfc\u665a\u8868\u591f\u6574\u8ba4\u54cd\u96ea\u6d41\u672a\u573a\u8be5\u5e76\u5e95\u6df1\u523b\u5e73\u4f1f\u5fd9\u63d0\u786e\u8fd1\u4eae\u8f7b\u8bb2\u519c\u53e4\u9ed1\u544a\u754c\u62c9\u540d\u5440\u571f\u6e05\u9633\u7167\u529e\u53f2\u6539\u5386\u8f6c\u753b\u9020\u5634\u6b64\u6cbb\u5317\u5fc5\u670d\u96e8\u7a7f\u5185\u8bc6\u9a8c\u4f20\u4e1a\u83dc\u722c\u7761\u5174\u5f62\u91cf\u54b1\u89c2\u82e6\u4f53\u4f17\u901a\u51b2\u5408\u7834\u53cb\u5ea6\u672f\u996d\u516c\u65c1\u623f\u6781\u5357\u67aa\u8bfb\u6c99\u5c81\u7ebf\u91ce\u575a\u7a7a\u6536\u7b97\u81f3\u653f\u57ce\u52b3\u843d\u94b1\u7279\u56f4\u5f1f\u80dc\u6559\u70ed\u5c55\u5305\u6b4c\u7c7b\u6e10\u5f3a\u6570\u4e61\u547c\u6027\u97f3\u7b54\u54e5\u9645\u65e7\u795e\u5ea7\u7ae0\u5e2e\u5566\u53d7\u7cfb\u4ee4\u8df3\u975e\u4f55\u725b\u53d6\u5165\u5cb8\u6562\u6389\u5ffd\u79cd\u88c5\u9876\u6025\u6797\u505c\u606f\u53e5\u533a\u8863\u822c\u62a5\u53f6\u538b\u6162\u53d4\u80cc\u7ec6";
// 准备常用字符集
String base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int x = 5;
// 控制字数
for (int i = 0; i < 4; i++) {
// 生成0-255的随机数
int R = new Random().nextInt(256);
int G = new Random().nextInt(256);
int B = new Random().nextInt(256);
// 设置每个随机数的颜色
g.setColor(new Color(R, G, B));
// 设置字体旋转角度
int degree = new Random().nextInt() % 30;
// 截取汉字
String ch = base.charAt(new Random().nextInt(base.length())) + "";
sb.append(ch);
// 正向角度
g.rotate(degree * Math.PI / 180, x, 20);
g.drawString(ch, x, 20);
// 反向角度
g.rotate(-degree * Math.PI / 180, x, 20);
x += 30;
}
return sb.toString();
}
}

3.创建验证结果servlet,ResultServlet.java

 package com.utils;

 import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* 结果验证Servlet
* @author huang
*
*/
@WebServlet("/resultServlet")
public class ResultServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response);
} /**
* 后台验证输入
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String validateC = (String) request.getSession().getAttribute("checkcode");
//从前台传来的用户输入的验证码
String veryCode = request.getParameter("c");
System.out.println("veryCode:"+veryCode);
PrintWriter out = response.getWriter();
if (veryCode == null || "".equals(veryCode)) {
out.println("请输入验证码!");
} else {
// if (validateC.equals(veryCode)) {
// 忽略大小写处理:
if (validateC.equalsIgnoreCase(veryCode)) {
out.println(1);
} else {
out.println("验证码输入错误!");
}
}
out.flush();
out.close();
} }

4、结果

项目下载:下载工程

Servlet实现验证码图片(一)的更多相关文章

  1. 转: 通过Servlet生成验证码图片

    孤傲苍狼 只为成功找方法,不为失败找借口! javaweb学习总结(九)—— 通过Servlet生成验证码图片 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedIma ...

  2. java web学习总结(九) -------------------通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  3. JavaWeb---总结(九)通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片  1 package gacl. ...

  4. javaweb学习总结(九)—— 通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  5. java web 学习九(通过servlet生成验证码图片)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  6. 通过Servlet生成验证码图片

    原文出自:http://www.cnblogs.com/xdp-gacl/p/3798190.html 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类, ...

  7. javaWeb学习总结(3)- 通过Servlet生成验证码图片(2)

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  8. 通过Servlet生成验证码图片(转)

    原文地址:http://www.cnblogs.com/xdp-gacl/p/3798190.html 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类, ...

  9. JavaWeb-Servlet-通过servlet生成验证码图片

    BufferedImage类 创建一个BufferImage servlet,用来生成验证码图片: package com.fpc; import java.awt.Color; import jav ...

随机推荐

  1. The SO_REUSEPORT socket option

    One of the features merged in the 3.9 development cycle was TCP and UDP support for the SO_REUSEPORT ...

  2. 批量操作QT UI中的控件

    背景:在一个项目中,可能一个UI中存在大量相同的tablewidget,combobox,label等控件,每种可能有100个,此时想对它们进行同样的操作 方案:(以tablewidget为例,UI中 ...

  3. codeforces103E Buying Sets

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. Class 的继承

    简介 Class 可以通过extends关键字实现继承,这比 ES5 的通过修改原型链实现继承,要清晰和方便很多. class Point { } class ColorPoint extends P ...

  5. 《深入理解mybatis原理3》 Mybatis数据源与连接池

    <深入理解mybatis原理> Mybatis数据源与连接池 对于ORM框架而言,数据源的组织是一个非常重要的一部分,这直接影响到框架的性能问题.本文将通过对MyBatis框架的数据源结构 ...

  6. Post with HttpClient4

    转载:http://www.cnblogs.com/luxiaoxun/p/6165237.html 作者:阿凡卢 出处:http://www.cnblogs.com/luxiaoxun/ HttpC ...

  7. 在js中做数字字符串补0

    转自(http://blog.csdn.net/aimingoo/article/details/4492592) 通常遇到的一个问题是日期的“1976-02-03 HH:mm:ss”这种格式 ,我的 ...

  8. 转载-lvs官方文档-Linux服务器集群系统(二)

    Linux服务器集群系统(二) LVS集群的体系结构 章文嵩 (wensong@linux-vs.org) 2002 年 4 月 本文主要介绍了LVS集群的体系结构.先给出LVS集群的通用体系结构,并 ...

  9. APUE学习笔记——10.9 信号发送函数kill、 raise、alarm、pause

    转载注明出处:Windeal学习笔记 kil和raise kill()用来向进程或进程组发送信号 raise()用来向自身进程发送信号. #include <signal.h> int k ...

  10. L157

    UK Watchdog: Smugglers to Exploit Border if no Brexit DealSmugglers and other organized criminals ar ...