[Html]
<% @ 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>
</Head>
<Form action = "./LoginServlet" method = "post">
<Body>
Employee information entry <br/>
Username: <input type = "text" value = "" name = "username"/> <br/>
Password: <input type = "password" name = "psw"/> <br/>
Verification Code: <input name = "checkcode" type = "text" classs = "big-input" maxlength = "20" value = ""/> <br/>
<br/>
Save username and password <input type = "checkbox" name = "save" value = "yes"/> <br/>
<Input type = "submit" value = "save"/>
</Body>
<Script type = "text/javascript">
Function nextPic (){
Var picStr = document. getElementById ("pic ");
// Use a random number
// PicStr. src = "$ {pageContext. request. contextPath}/ImageServlet? "+ Math. random ();
// Use the timestamp
PicStr. src = "$ {pageContext. request. contextPath}/ImageServlet? "+ Date. parse (new Date ());
}
</Script>
</Form>
</Html>
ImageServlet:
[Java]
Package aaa;
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;
Import javax. servlet. http. HttpSession;
Public class ImageServlet extends HttpServlet {
Private int width = 85;
Private int height = 30;
// Define the letters and numbers used for the verification code
Private char [] charSeq = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789". toCharArray ();
Private int codeCount = 4;
// Define the baseline of the Verification Code
Private int codeX;
Private int codeY;
Public ImageServlet (){
Super ();
}
Public void destroy (){
Super. destroy (); // Just puts "destroy" string in log
// Put your code here
}
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoPost (request, response );
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Request. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html; charset = UTF-8 ");
// PrintWriter out = response. getWriter ();
HttpSession session = request. getSession ();
/*
* BufferedImage describes the image with access to the image buffer.
* The Graphics class is applicable to all graphic contexts.
*/
BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB );
Graphics g = image. getGraphics ();
// Set the current color of the image context to the specified color
G. setColor (Color. white );
G. fillRect (0, 0, width, height );
// Set the yans of the border
G. setColor (Color. black );
G. drawRect (0, 0, width-1, height-1 );
// Generate the verification code
Random random = new Random ();
String randstr = "";
For (int I = 1; I <= codeCount; I ++ ){
// Obtain the verification code immediately.
String rand = String. valueOf (charSeq [random. nextInt (charSeq. length)]);
System. out. println ("rand =" + rand );
Randstr = randstr + rand;
G. setColor (Color. blue );
G. setFont (new Font ("", Font. BOLD, 30 ));
G. drawString (rand, codeX * I, codeY );
}
System. out. println ("randstr =" + randstr );
// Place randstr in the session
Session. setAttribute ("checkKey", randstr );
// Set interference points
For (int I = 0; I <155; I ++ ){
Int x = random. nextInt (width );
Int y = random. nextInt (height );
G. setColor (Color. red );
G. drawOval (x, y, 0, 0 );
}
// Release resources
G. dispose ();
HttpServletResponse res = (HttpServletResponse) response;
// Set the expiration time of the webpage. Once it expires, it must be re-called on the server.
Res. setDateHeader ("Expires",-1 );
// Cache-Control specifies the cache mechanism that the request and response should follow. no-Cache indicates that the request or response message cannot be cached.
Res. setHeader ("Cache-Control", "no-cache ");
// Used to disable the browser from calling the page content from the local Cache. Once the page is set, it cannot be called out from the Cache once it leaves the page.
Res. setHeader ("Pragma", "no-cache ");
ImageIO. write (image, "jpeg", response. getOutputStream ());
}
Public void init () throws ServletException {
This. codeX = width/(codeCount + 2 );
This. codeY = height;
}
}
LoginServlet:
[Java]
Package aaa;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletContext;
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 LoginServlet extends HttpServlet {
Public LoginServlet (){
Super ();
}
Public void destroy (){
Super. destroy (); // Just puts "destroy" string in log
}
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DoPost (request, response );
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
// Request. setCharacterEncoding ("UTF-8 ");
Response. setContentType ("text/html; charset = UTF-8 ");
PrintWriter out = response. getWriter ();
HttpSession session = request. getSession ();
String username = request. getParameter ("username ");
String checkcode = request. getParameter ("checkcode ");
String checkKey = (String) session. getAttribute ("checkKey ");
If (checkcode. equals (checkKey )){
System. out. println ("Allow Logon ");
} Else {
System. out. println ("Log on again ");
}
System. out. println ("username =" + username );
Request. getRequestDispatcher ("/purview. jsp"). forward (request, response );
}
Public void init () throws ServletException {
}