文件一LoginCheckServlet.java
package Random;
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;
public class LoginCheckServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginCheckServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
String randomCode=(String)session.getAttribute("randomCode");
if(randomCode==null){
response.sendRedirect("http://localhost:8080/code/MyHtml.html");
return;
}
String reqRandom=request.getParameter("random").trim();
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
PrintWriter out=response.getWriter();
if(randomCode.equals(reqRandom)){
out.print("验证匹配!");
}
else{
out.print(randomCode);
out.print("验证码效验错误!");
}
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
文件二RandomCodeServlet .java
package Random;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class RandomCodeServlet extends HttpServlet {
private int width=60;
private int height=20;
protected void service(HttpServletRequest rsq,HttpServletResponse resp) throws ServletException ,java.io.IOException{
BufferedImage buffimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g=buffimg.createGraphics();
Random random=new Random();
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
Font font=new Font("Times New Roman",Font.PLAIN,18);
g.setFont(font);
g.setColor(Color.BLACK);
g.drawRect(0, 0, width-1, height-1);
g.setColor(Color.GRAY);
for(int i=0;i<160;i++){
int x=random.nextInt(width);
int y=random.nextInt(height);
int xl=random.nextInt(12);
int yl=random.nextInt(12);
g.drawLine(x, y, xl, yl);
}
StringBuffer randomCode=new StringBuffer();
int red=0,green=0,blue=0;
for (int i=0;i<4;i++){
String strRand=String.valueOf(random.nextInt(10));
red=random.nextInt(110);
green=random.nextInt(50);
blue=random.nextInt(50);
g.setColor(new Color(red,green,blue));
g.drawString(strRand,13*i,16);
randomCode.append(strRand);
}
HttpSession session=rsq.getSession();
session.setAttribute("randomCode",randomCode.toString());
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
ServletOutputStream sos =resp.getOutputStream();
ImageIO.write(buffimg, "jpeg", sos);
sos.println("a"+buffimg.toString());
sos.close();
}
}
文件三MyHtml.html
<html>
<head>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script langeage="javascript">
<!--
function show(o){
//重载验证码
var timenow = new Date().getTime();
o.src="http://localhost:8080/code/servlet/RandomCodeServlet?d="+timenow
}
-->
</script>
</head>
<body >
<form method="post" action="servlet/LoginCheckServlet" >
<p> 用户名:
<input name="username" type="text" size="20">
</p>
<p> 密 码:
<input name="password" type="password" size="20">
<p> 验证码: <input name="random" type="text" size="20" maxlength="4">
<img id="image" src="http://localhost:8080/code/servlet/RandomCodeServlet" onClick="show(document.getElementById('image'))"> </p>
<p>
<div id=txtHint>
<a onClick="show(document.getElementById('image'))">看不清</a>
</div>
<input type="submit" name="submit" value="submit"> <input type="reset" value="reset" name="Reset" onClick="change()">
<br>
<p> </p><p> </p></form>
</body>
</html>
效果如下
用户名:
package Random;
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;
public class LoginCheckServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginCheckServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession();
String randomCode=(String)session.getAttribute("randomCode");
if(randomCode==null){
response.sendRedirect("http://localhost:8080/code/MyHtml.html");
return;
}
String reqRandom=request.getParameter("random").trim();
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
PrintWriter out=response.getWriter();
if(randomCode.equals(reqRandom)){
out.print("验证匹配!");
}
else{
out.print(randomCode);
out.print("验证码效验错误!");
}
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
文件二RandomCodeServlet .java
package Random;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class RandomCodeServlet extends HttpServlet {
private int width=60;
private int height=20;
protected void service(HttpServletRequest rsq,HttpServletResponse resp) throws ServletException ,java.io.IOException{
BufferedImage buffimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g=buffimg.createGraphics();
Random random=new Random();
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
Font font=new Font("Times New Roman",Font.PLAIN,18);
g.setFont(font);
g.setColor(Color.BLACK);
g.drawRect(0, 0, width-1, height-1);
g.setColor(Color.GRAY);
for(int i=0;i<160;i++){
int x=random.nextInt(width);
int y=random.nextInt(height);
int xl=random.nextInt(12);
int yl=random.nextInt(12);
g.drawLine(x, y, xl, yl);
}
StringBuffer randomCode=new StringBuffer();
int red=0,green=0,blue=0;
for (int i=0;i<4;i++){
String strRand=String.valueOf(random.nextInt(10));
red=random.nextInt(110);
green=random.nextInt(50);
blue=random.nextInt(50);
g.setColor(new Color(red,green,blue));
g.drawString(strRand,13*i,16);
randomCode.append(strRand);
}
HttpSession session=rsq.getSession();
session.setAttribute("randomCode",randomCode.toString());
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
ServletOutputStream sos =resp.getOutputStream();
ImageIO.write(buffimg, "jpeg", sos);
sos.println("a"+buffimg.toString());
sos.close();
}
}
文件三MyHtml.html
<html>
<head>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script langeage="javascript">
<!--
function show(o){
//重载验证码
var timenow = new Date().getTime();
o.src="http://localhost:8080/code/servlet/RandomCodeServlet?d="+timenow
}
-->
</script>
</head>
<body >
<form method="post" action="servlet/LoginCheckServlet" >
<p> 用户名:
<input name="username" type="text" size="20">
</p>
<p> 密 码:
<input name="password" type="password" size="20">
<p> 验证码: <input name="random" type="text" size="20" maxlength="4">
<img id="image" src="http://localhost:8080/code/servlet/RandomCodeServlet" onClick="show(document.getElementById('image'))"> </p>
<p>
<div id=txtHint>
<a onClick="show(document.getElementById('image'))">看不清</a>
</div>
<input type="submit" name="submit" value="submit"> <input type="reset" value="reset" name="Reset" onClick="change()">
<br>
<p> </p><p> </p></form>
</body>
</html>
效果如下
用户名:
密 码:
验证码:
看不清