Servlet生成验证码并输出:

@WebServlet(name = "yzmServlet")
public class yzmServlet extends HttpServlet {
    private static final int WIDTH = 100;
    private static final int HEIGHT = 40;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        BufferedImage image=new BufferedImage(WIDTH,HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics g=image.getGraphics();
        //设置背景色
        g.setColor(Color.CYAN);
        //填充背景色
        g.fillRect(0,0,WIDTH,HEIGHT);
        //设置前景色
        g.setColor(Color.BLACK);
        //设置字体
        Font font=new Font("仿宋",Font.BOLD,20);
        g.setFont(font);
        //获取验证码
        int len=4;
        String result="";
        for(int x=0;x

Servlet对登录界面输入的昵称/密码/验证码进行验证:

@WebServlet(name = "identifyYzmServlet")
public class identifyYzmServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         response.setCharacterEncoding("utf-8");
         response.setContentType("text/html;charset=utf-8");
         request.setCharacterEncoding("utf-8");
         //获取输入的用户名
         String username=request.getParameter("username");
        //获取输入的密码
         String password=request.getParameter("password");
         //获取输入的验证码
         String yzm2=request.getParameter("yzm01");
         //获取Session对象中存取的数据
         HttpSession session1=request.getSession();
         String yzm1=(String)session1.getAttribute("identifyCode");
         if(!username.equals("")&&!password.equals("")&&!yzm2.equals("")){
             if(username.equals("Bighead")) {
                 if (password.equals("4214963")) {
                     if (yzm1.equals(yzm2)) {
                         response.getWriter().println("登录成功");
                         //登录成功后跳转到的页面
                         //request.getRequestDispatcher("form01.html").forward(request,response);
                     } else {
                         response.getWriter().println("验证码有误,请重新输入");
                     }
                 } else {
                     response.getWriter().println("密码有误,请重新输入");
                 }
             }else{
                 response.getWriter().println("昵称有误,请重新输入");
             }
        }else{
            response.getWriter().println("昵称或密码或验证码不能为空,请输入");
       }
    }
}

web-xml进行配置:


-

identifyYzmServlet

com.westos.identifyYzmServlet



-

identifyYzmServlet

/ident



-

yzmServlet

com.westos.untitle2.yzmServlet



-

yzmServlet

/yzm01

html实现登录界面:




    
    login
    
    


登录

昵称:

密码:

请输入验证码: