表单

<form action="loginServlet" method="post">
请输入验证码:<input type="text" name="code" />
<img src="getCodeServlet" /><br />
<button type="submit">提交</button>
</form>

载入页面时,会自动请求getCodeServlet,获取图片(验证码)。

getCodeServlet,产生验证码

 @WebServlet("/getCodeServlet")
public class GetCodeServlet extends HttpServlet {
//验证码的宽、高
private static int WIDTH=80;
private static int HEIGHT=25; //绘制背景
private void drawBg(Graphics g){
//rgb
g.setColor(new Color(128, 128, 128));
//绘制矩形。x,y,wigth,height
g.fillRect(0,0,WIDTH,HEIGHT);
//随机绘制100个干扰点
Random random=new Random();
for (int i=0;i<100;i++){
//产生(0,1)上的小数,*WIDTH|HEIGHT,再取整也行
int x=random.nextInt(WIDTH);
int y=random.nextInt(HEIGHT);
g.drawOval(x,y,1,1); //干扰点的颜色也可以随机,随机产生red,green,blue即可
//g.setColor(new Color(red,green,blue));
}
} //绘制验证码
private void drawCode(Graphics g,char[] code){
g.setColor(Color.BLACK);
//字体、样式(多个时竖线分隔)、字号
g.setFont(new Font("serif",Font.ITALIC|Font.BOLD,18));
//在不同位置绘制验证码字符,参数:要绘制的String、横、纵坐标。+""是为了char转String。
g.drawString(code[0]+"",1,17);
g.drawString(code[1]+"",16,15);
g.drawString(code[2]+"",31,18);
g.drawString(code[3]+"",46,16);
} //随机产生4位验证码
private char[] getCode(){
String chars="0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
char[] code=new char[4];
Random random=new Random();
for (int i=0;i<4;i++){
//[0,62)
int index= random.nextInt(62);
code[i]=chars.charAt(index);
}
return code;
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
ServletOutputStream sos = response.getOutputStream();
response.setContentType("image/jpeg"); //设置浏览器不缓存此图片
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0); //创建内存图片
BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, TYPE_INT_RGB);
Graphics g= bufferedImage.getGraphics();
char[] code=getCode();
//将验证码放到session域中。session对象要在提交响应之前获得
session.setAttribute("code",new String(code));
drawBg(g);
drawCode(g,code);
g.dispose(); //将图片输出到浏览器
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"JPEG",baos);
baos.writeTo(sos);
baos.close();
sos.close();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
doPost(request,response);
}
}

loginServlet,处理表单

 @WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
HttpSession session = request.getSession();
String trueCode= (String) session.getAttribute("code");
String code=request.getParameter("code"); if (code.equals(trueCode)){
response.getWriter().write("验证码正确");
}
else {
response.getWriter().write("验证码错误");
}
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}

上面的处理方式要区分验证码的大小写。

不区分大小写:

//先转换为全大写|全小写,再判断
trueCode=trueCode.toLowerCase();
code=code.toLowerCase(); //trueCode=trueCode.toUpperCase();
//code=trueCode.toUpperCase();

最新文章

  1. 无限级ddsmoothmenu菜单实例
  2. C:Wordpress自定义文章类型(图视频)
  3. Overload和Override的区别
  4. android 中listview之BaseAdapter的使用
  5. ASP.NET静态页生成方法(模板替换)
  6. Android SDK 国内源-好用。
  7. extjs 6.2 helloworld
  8. SpringMvc笔记-对RESTFUL风格的配置
  9. java语言环境jdk的安装和环境变量的配置
  10. Ubuntu 16.04.1 LTS配置LNMP使用wordpress搭建博客
  11. window环境下使用filezilla server搭建ftp服务器
  12. 微信小程序弹出操作菜单
  13. 用Python3实现的Mycin专家系统简单实例
  14. linux的基本操作(一)
  15. 用 python 生成一个简单的词云图
  16. Scrapy对接Splash基础知识学习
  17. NDK samples以及部分博客
  18. python输出显示颜色
  19. springmvc/springboot处理前台字符串日期自动转换成后台date类型的三种办法
  20. 利用cross-entropy cost代替quadratic cost来获得更好的收敛

热门文章

  1. SpringBoot设置支持跨域请求
  2. Tomcat的下载和安装
  3. 代码规范 &amp; 数学之美读后感
  4. Codechef August Challenge 2019 Division 2
  5. Salesforce 开发整理(七)配置审批流
  6. oracle--10GRAC集群(NFS共享存储)
  7. Ubuntu18.04开机挂载硬盘
  8. jenkins 更新插件使用代理
  9. SQL Server 2014:为什么会提示“用户登录失败”?
  10. HandlerInterceptorAdapter