import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet
 {protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException
  { resp.setContentType("text/html;charset=gbk");
  PrintWriter out=resp.getWriter();
  try{
   String myUserName=this.getInitParameter("name");
   String myPassWord=this.getInitParameter("password");
             HttpSession session=req.getSession();
   String scode=(String)session.getAttribute("code");
   String userName=req.getParameter("name");
   String passWord=req.getParameter("password");
   String code=req.getParameter("code");
   out.println("<html><body>");
   out.println("<br/>");
   if(!code.toUpperCase().equals(scode)){out.println("Wrong Validate!");}
   else if (userName.equals(myUserName)&&passWord.equals(myPassWord))
   {out.println("Login Sucessed! ");}
   else{out.println("Login Failed!");}
  }
   finally{out.close();}
  }
  protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
   doGet(req,resp);}
  
   

 }

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.*;
import javax.servlet.http.*;

public class CheckCodeServlet extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  resp.setContentType("image/jpeg");
  OutputStream out = resp.getOutputStream();
  try {
   resp.setHeader("programa", "no-cache");
   resp.setHeader("Cache-Control", "no-cache");
   resp.setDateHeader("Expires", 0);
   BufferedImage image = new BufferedImage(50, 18,
     BufferedImage.TYPE_INT_RGB);
   Graphics g = image.getGraphics();
   g.setColor(Color.LIGHT_GRAY);
   g.fillRect(0, 0, 50, 18);
   g.setColor(Color.LIGHT_GRAY);
   g.drawRect(0, 0, 50, 18);
   String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   String code = "";
   for (int i = 0; i < 4; i++) {
    int k = (int) (Math.random() * 36);
    char c = str.charAt(k);
    code += c;
   }
   HttpSession session = req.getSession();
   session.setAttribute("code", code);
   g.setColor(Color.BLACK);
   Font font = new Font("DIALOG", Font.ITALIC, 15);
   g.setFont(font);
   g.drawString(code, 3, 15);
   ImageIO.write(image, "JPEG", out);
   out.flush();
   out.close();
  } finally {
   out.close();
  }
 }

 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  doGet(req,resp);}

}

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
    <title>Login</title>
 </head>
    <BODY bgColor=#ffffff leftMargin=0 text=#000000 topMargin=30><center>
    <form action="LoginServlet" method="get">
      User  <input maxlength=10 name="name" size=8><br>
    Password <input type=password name="password" size=8><br>
    Validate <input name=code size=8><br>
    <img src="checkCodeServlet" onclick="self.location.reload();"/><br>
<input name=Submit type=submit value=Commit>
<input name=Submit2 type=reset value=Reset>
  </form>
  </body>
</html>

 

<web-app>
     <servlet>
           <servlet-name>CheckCodeServlet</servlet-name>
           <servlet-class>Com.CheckCodeServlet</servlet-class>
    </servlet>

    <servlet>
           <servlet-name>LoingServlet</servlet-name>
           <servlet-class>Com.LoingServlet</servlet-class>

           <init-param>
                    <param-name>name</param-name>
                    <param-value>abc</param-value>
            </init-param>          
           <init-param>
                    <param-name>password</param-name>
                    <param-value>123</param-value>
            </init-param>          

    </servlet>

    <servlet-mapping>
           <servlet-name>CheckCodeServlet</servlet-name>
           <url-pattern>/CheckCodeServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
           <servlet-name>LoingServlet</servlet-name>
           <url-pattern>/LoingServlet</url-pattern>
    </servlet-mapping>
</web-app>