javaWeb验证码
2015-09-21 20:48
519 查看
在web开发中,我们经常会用到,验证码的用法。现在就给大家写一个例子。
注:利用到的技术:主要是HttpServlet
jsp主页面:
servlet页面生成验证码的图片代码:
最后是xml映射文件:
结果:
注:利用到的技术:主要是HttpServlet
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 'index.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"> --> </head><!-- 该函数无法执行的话,可能是没有通知浏览器不要缓存的原因 --> <script type="text/javascript"> function toreal(){ document.location.href="index.jsp"; } </script> <body> <form action="" method="post"> <input type="text" name="username"><br/> <input type="password" name="password"><br/> <input type="text" name="code"> <img alt="验证码" src="/java_Code/servlet/code"> <a href="javascript:toreal()">看不清</a> <br/> <input type="submit" value="登录"><br/><!-- 内部自动发送请求,加载验证码 --> </form> </body> </html>
servlet页面生成验证码的图片代码:
package com.dp.java.code; 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.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 输出验证码图片 * */ public class code extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //通知浏览器不要缓存 response.setHeader("Expires", "-1"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "-1"); int height=25; int width=120; //得到一个内存图像BufferedImage BufferedImage img=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //得到一个画笔 Graphics g=img.getGraphics(); //画边框drawRect绘制指定矩形的边框。 g.drawRect(0, 0, width, height); //填充颜色 g.setColor(Color.RED); g.fillRect(1, 1, width-2, height-2); //画干扰线 g.setColor(Color.BLACK); Random r=new Random(); for(int i=0;i<20;i++) g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height)); //生成随机数字 g.setColor(Color.BLUE); g.setFont(new Font("微软雅黑", Font.BOLD|Font.ITALIC, 20));//BOLD加粗,ITALIC斜体 int d=15; for(int j=0;j<4;j++){ g.drawString(r.nextInt(10)+"", d, 20); d+=20; } //输出打web页面 ImageIO.write(img, "jpg", response.getOutputStream()); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
最后是xml映射文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>java_Code</display-name> <servlet> <servlet-name>code</servlet-name> <servlet-class>com.dp.java.code.code</servlet-class> </servlet> <servlet-mapping> <servlet-name>code</servlet-name> <url-pattern>/servlet/code</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
结果:
相关文章推荐
- Java基础知识强化50:运行javac 报告javac不是内部或外部命令(已解决)
- Windows平台搭建Kafka源代码开发环境(Eclipse版本)
- myeclipse的优化
- Java中删除文件、删除目录及目录下所有文件
- -Java语法基础(概述)
- java File类
- java中创建对象的方法
- Java 多线程常用概念
- java中的堆、栈和常量池
- Java 字符串
- java学习笔记多线程学习总结(下)
- Java线程进入
- ubuntu java jdk安装及环境变量设置
- Java8揭秘(四)Java集合类库的批量数据操作
- 我们神码小组第一次合作完成的java项目——类的多态继承与接口的使用练习。
- Java 垃圾回收机制 GC的算法和操作
- Java8揭秘(三)Default 方法
- Struts2环境的搭建
- 创建并已入javaweb项目,运行之后提示如下错误
- Java8揭秘(二)Java 8中的 Lambda表达式