⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 validateservlet.java

📁 提供 精通Java Web动态图表编程 一书的源代码, 大家赶快下载呀
💻 JAVA
字号:
// Fig. 5.6_01_02:  ValidateServlet.java
// 校验认证码的Servlet

package com.fatcat.webchart;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ValidateServlet extends HttpServlet
{
    // 处理客户端提交数据的 "Post" 请求
    protected void doPost(HttpServletRequest request, HttpServletResponse
        response)throws ServletException, IOException
    {
        // 发送 XHTML 格式的页面给客户端
        response.setContentType("text/html;charset=gb2312");

        // 设置页面不缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        // 获得用户输入的验证码
        String validateCode = request.getParameter("validateCode");

        // 获取保存在session中的验证码
        HttpSession session = request.getSession();
        String codeNumbers = (String)session.getAttribute("codeNumbers");

        String url = "/chap05/Fig5.6/Fig.5.6_01/validate.html";

        if (codeNumbers == null)
        {
            response.sendRedirect(url);
            return ;
        }

        PrintWriter out = response.getWriter();

        // 开始生成 XHTML 文档
        out.println("<?xml version = \"1.0\"?>");

        out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD " + 
			"XHTML 1.0 Strict//EN\" \"http://www.w3.org" + 
			"/TR/xhtml1/DTD/xhtml1-strict.dtd\">");

        out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">");

        // 生成文档的head部分
        out.println("<head>");
        out.println("<title>校验认证码的Servlet</title>");
        out.println("</head>");

        // 生成文档的body部分
        out.println("<body>");

        // 校验用户输入的验证码和session中的验证码是否相同
        if (codeNumbers.equals(validateCode))
        {
            out.println(
                "<h1><font color=\"green\">输入相同,校验成功</font></h1> ");
        }
        else
        {
            out.println("<h1><font color=\"red\">输入错误,校验失败</font> <br>");
            out.println("<p>请重新<a href=\"" + url +
				"\">输入验证码</a><h1></p>");
        }

        out.println("</body>");

        // 结束 XHTML 文档
        out.println("</html>");
        out.close(); // 关闭流
    }

    // 处理客户端提交数据的 "get" 请求, 和doPost一样
    protected void doGet(HttpServletRequest request, HttpServletResponse
        response)throws ServletException, IOException
    {
        doPost(request, response);
    }
}


/**************************************************************************
 * (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓).  *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors of this code have used their                   *
 * best efforts in preparing the code. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these codes. The authors *
 * shall not be liable in any event for incidental or consequential       *
 * damages in connection with, or arising out of, the furnishing,         *
 * performance, or use of these programs.                                 *
  **************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -