用Servlet生成图片在表单中生成验证码
2016-11-15 14:28
573 查看
1:html(登录页面)
<!DOCTYPE html>
<html>
<head>
<title>login.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="../LoginImageServlet" method="post">
<table cellpadding="0" cellspacing="0" align="center">
<tr>
<td>用户名</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td>密 码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" size="10" name="valiCode"/><img src="../AuthTmageServlet"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/></td>
</tr>
</form>
</body>
</html>
2:servlet(用于生成图片)
package com.study;
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.text.SimpleDateFormat;
import java.util.Date;
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;
import javax.servlet.http.HttpSession;
public class AuthTmageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Font mFont=new Font("Arial Black",Font.PLAIN,16);
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//PrintWriter out = response.getWriter();
response.setHeader("Param", "No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
int width=80;
int height=20;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
Random random=new Random();
g.setColor(getRandomColor(200,250));
g.fillRect(1, 1, width-1, height-1);
g.setColor(new Color(102,102,102));
g.fillRect(0,0, width-1, height-1);
g.setFont(mFont);
g.setColor(getRandomColor(160,200));
for(int i=0;i<155;i++)
{
int x=random.nextInt(width-1);
int y=random.nextInt(height-1);
int x1=random.nextInt(6)+1;
int y1=random.nextInt(12)+1;
g.drawLine(x, y, x+x1, y+y1);
}
for(int i=0;i<70;i++)
{
int x=random.nextInt(width-1);
int y=random.nextInt(height-1);
int x1=random.nextInt(12)+1;
int y1=random.nextInt(6)+1;
g.drawLine(x, y, x-x1, y-y1);
}
String sRand="";
int length=4;
for(int i=0;i<length;i++)
{
String temp=getRandomChar();
sRand+=temp;
g.setColor(new Color(20+random.nextInt(110)));
g.drawString(temp, 15*i+10, 15);
}
HttpSession session=request.getSession(true);
session.setAttribute("randomImageStr", sRand.toLowerCase());
g.dispose();
ImageIO.write(image, "JPEG",response.getOutputStream());
/*out.flush();
out.close();*/
}
private String getRandomChar() {
int rand=(int)Math.round(Math.random()*2);
long itmp=0;
char ctmp='\u0000';
switch(rand)
{
case 1:itmp=Math.round(Math.random()*25+65);
ctmp=(char)itmp;
return String.valueOf(ctmp);
case 2:itmp=Math.round(Math.random()*25+97);
ctmp=(char)itmp;
return String.valueOf(ctmp);
default:itmp=Math.round(Math.random()*9);
return String.valueOf(itmp);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public Color getRandomColor(int fc,int bc)
{
Random random=new Random();
if(fc>255)
fc=255;
if(bc>255)
bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
}
3:servlet:(进行表单验证)
package com.study;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.text.SimpleDateFormat;
public class LoginImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String valiCode=request.getParameter("valiCode");
HttpSession session=request.getSession();
String randomImageStr=(String)session.getAttribute("randomImageStr");
if(valiCode!=null)
{
if(valiCode.toLowerCase().equals(randomImageStr))
out.print("验证码匹配!");
else
{
out.print("验证码不匹配!");
out.print("<a href='day2/login.html'>重新登录</a>");
}
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>login.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="../LoginImageServlet" method="post">
<table cellpadding="0" cellspacing="0" align="center">
<tr>
<td>用户名</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td>密 码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" size="10" name="valiCode"/><img src="../AuthTmageServlet"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/></td>
</tr>
</form>
</body>
</html>
2:servlet(用于生成图片)
package com.study;
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.text.SimpleDateFormat;
import java.util.Date;
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;
import javax.servlet.http.HttpSession;
public class AuthTmageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Font mFont=new Font("Arial Black",Font.PLAIN,16);
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//PrintWriter out = response.getWriter();
response.setHeader("Param", "No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
int width=80;
int height=20;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
Random random=new Random();
g.setColor(getRandomColor(200,250));
g.fillRect(1, 1, width-1, height-1);
g.setColor(new Color(102,102,102));
g.fillRect(0,0, width-1, height-1);
g.setFont(mFont);
g.setColor(getRandomColor(160,200));
for(int i=0;i<155;i++)
{
int x=random.nextInt(width-1);
int y=random.nextInt(height-1);
int x1=random.nextInt(6)+1;
int y1=random.nextInt(12)+1;
g.drawLine(x, y, x+x1, y+y1);
}
for(int i=0;i<70;i++)
{
int x=random.nextInt(width-1);
int y=random.nextInt(height-1);
int x1=random.nextInt(12)+1;
int y1=random.nextInt(6)+1;
g.drawLine(x, y, x-x1, y-y1);
}
String sRand="";
int length=4;
for(int i=0;i<length;i++)
{
String temp=getRandomChar();
sRand+=temp;
g.setColor(new Color(20+random.nextInt(110)));
g.drawString(temp, 15*i+10, 15);
}
HttpSession session=request.getSession(true);
session.setAttribute("randomImageStr", sRand.toLowerCase());
g.dispose();
ImageIO.write(image, "JPEG",response.getOutputStream());
/*out.flush();
out.close();*/
}
private String getRandomChar() {
int rand=(int)Math.round(Math.random()*2);
long itmp=0;
char ctmp='\u0000';
switch(rand)
{
case 1:itmp=Math.round(Math.random()*25+65);
ctmp=(char)itmp;
return String.valueOf(ctmp);
case 2:itmp=Math.round(Math.random()*25+97);
ctmp=(char)itmp;
return String.valueOf(ctmp);
default:itmp=Math.round(Math.random()*9);
return String.valueOf(itmp);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public Color getRandomColor(int fc,int bc)
{
Random random=new Random();
if(fc>255)
fc=255;
if(bc>255)
bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
}
3:servlet:(进行表单验证)
package com.study;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.text.SimpleDateFormat;
public class LoginImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String valiCode=request.getParameter("valiCode");
HttpSession session=request.getSession();
String randomImageStr=(String)session.getAttribute("randomImageStr");
if(valiCode!=null)
{
if(valiCode.toLowerCase().equals(randomImageStr))
out.print("验证码匹配!");
else
{
out.print("验证码不匹配!");
out.print("<a href='day2/login.html'>重新登录</a>");
}
}
}
}
相关文章推荐
- servlet实现随即生成验证码图片
- 在servlet中生成验证码图片(二)
- 通过Servlet生成验证码图片
- javaweb学习总结(九)—— 通过Servlet生成验证码图片
- jsp/servlet生成验证码图片
- 【Java】使用Servlet生成随机验证码图片
- javaweb学习总结(九)—— 通过Servlet生成验证码图片
- Java Web学习总结(6)——通过Servlet生成验证码图片
- servlet 生成动态图片验证码
- javaweb学习总结(九)—— 通过Servlet生成验证码图片
- Servlet生成验证码图片
- 通过Servlet生成验证码图片
- Servlet 3 HttpServletRequest HttpServletResponse 验证码图片 form表单
- Java Web学习总结(6)——通过Servlet生成验证码图片
- 生成用户登录页面中验证码图片的Servlet
- Servlet生成验证码图片
- 【验证码】servlet 生成 验证码图片
- 通过Servlet生成验证码图片
- Java Web学习总结(6)——通过Servlet生成验证码图片
- javaweb学习总结(九)—— 通过Servlet生成验证码图片