Servlet的4种会话管理技术

Servlet的会话管理技术主要有4种:URL重写,Cookie,隐藏表单域以及HttpSession。下面的例子通过一个动态验证码的实例详细说明了这4种技术的具体应用方式。为了简单起见,动态验证码没有采用图片生成技术,其原理和效果与本例中相同。另外,为了代码结构的清晰性,在登录表单生成和登录验证部分都没有采用统一的代码。对这些代码稍作修改,就可以实现单一的表单生成Servlet和验证Servlet,感兴趣的朋友可以与我联系([email protected])。
//**************************************   index.jsp代码:
<%@ page contentType="text/html;charset=gb2312" %>

 
    Servlet会话管理实例
 
 
 
   

Servlet会话管理实例



    本实例通过实现用户登录过程中的动态验证码校验功能,说明4种Session管理方法的特点。

   
   
 

//**************************************   web.xml内容:


 
    UrlRedirect
    UrlRedirect
 

 
    Login
    LoginWithUrlRedirect
 

 
    HiddenField
    HiddenField
 

 
    Cookie
    CookieManagement
 

 
    LoginWithCookie
    LoginWithCookie
 

 
    HttpSessionManagement
    HttpSessionManagement
 

 
    LoginWithSession
    LoginWithSession
 

 
    UrlRedirect
    /UrlRedirect
 

 
    Login
    /Login
 

 
    HiddenField
    /HiddenField
 

 
    Cookie
    /Cookie
 

 
    LoginWithCookie
    /LoginWithCookie
 

 
    HttpSessionManagement
    /HttpSessionManagement
 

 
    LoginWithSession
    /LoginWithSession
 

 
    index.jsp
 

//**************************************   URL重写技术的登录表单生成Servlet
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;

public class UrlRedirect extends HttpServlet {
 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  int validationCode=(int)(Math.random()*1000);
  out.println("");
  out.println("URL重写");
  out.println("

通过URL重写实现会话管理


");
  out.println("
");
  out.println("用户名:
");
  out.println("密码: 
");
  out.println("验证码:"+validationCode);
  out.println("
");
  out.println("
");
  out.println("");
  out.println("");
  out.flush();
  out.close();
 }
}
//**************************************   隐藏域技术的登录表单生成Servlet
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;

public class HiddenField extends HttpServlet {
 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  int validationCode=(int)(Math.random()*1000);
  out.println("");
  out.println("隐藏域");
  out.println("

通过隐藏域实现会话管理


");
  out.println("
");
  out.println("用户名:
");
  out.println("密码: 
");
  out.println("验证码:"+validationCode);
  out.println("");
  out.println("
");
  out.println("
");
  out.println("");
  out.println("");
  out.flush();
  out.close();
 }
}

//**************************************   URL重写和隐藏域技术的登录验证Servlet
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;

public class LoginWithUrlRedirect extends HttpServlet {
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  out.println("

通过URL重写实现会话管理


");
  out.println("用户名:"+request.getParameter("username")+"
");
  out.println("密码: "+request.getParameter("password")+"
");
  out.println("系统验证码:"+request.getParameter("code")+"
");
  out.println("输入验证码:"+request.getParameter("inputedcode")+"
");
  out.println("");
  out.flush();
  out.close();
 }

}
//**************************************   Cookie技术的登录表单生成Servlet
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.Cookie;

public class CookieManagement extends HttpServlet {
 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  int validationCode=(int)(Math.random()*1000);
  Cookie myCookie=new Cookie("code",""+validationCode);
  myCookie.setMaxAge(60*60);   //一小时
  response.addCookie(myCookie);
  out.println("");
  out.println("Cookie");
  out.println("

通过Cookie实现会话管理


");
  out.println("
");
  out.println("用户名:
");
  out.println("密码: 
");
  out.println("验证码:"+validationCode);
  out.println("
");
  out.println("
");
  out.println("");
  out.println("");
  out.flush();
  out.close();
 }
}
//**************************************   Cookie技术的登录验证Servlet
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.Cookie;

public class LoginWithCookie extends HttpServlet {
 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  Cookie cookies[] = request.getCookies(); // 获取所有Cookie
  String code="";
  if (cookies != null){     
   for(int i = 0; i < cookies.length; ++i)   // 遍历所有Cookie
    if (cookies[i].getName().equals("code")){ //找到
     code=cookies[i].getValue();
     break;
    }
  }
  out.println("

通过Cookie实现会话管理


");
  out.println("用户名:"+request.getParameter("username")+"
");
  out.println("密码: "+request.getParameter("password")+"
");
  out.println("系统验证码:"+code+"
");
  out.println("输入验证码:"+request.getParameter("inputedcode")+"
");
  out.println("");
  out.flush();
  out.close();
 }

}

//**************************************   HttpSession技术的登录表单生成Servlet
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 HttpSessionManagement extends HttpServlet {
 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  int validationCode=(int)(Math.random()*1000);
  HttpSession session=request.getSession();
  session.setAttribute("code",validationCode);
  out.println("");
  out.println("HttpSession");
  out.println("

通过HttpSession实现会话管理


");
  out.println("
");
  out.println("用户名:
");
  out.println("密码: 
");
  out.println("验证码:"+validationCode);
  out.println("
");
  out.println("
");
  out.println("");
  out.println("");
  out.flush();
  out.close();
 }
}

//**************************************   HttpSession技术的登录验证Servlet
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 LoginWithSession extends HttpServlet {

 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

  response.setContentType("text/html;charset=gb2312");
  PrintWriter out = response.getWriter();
  HttpSession session=request.getSession();
  out.println("

通过HttpSession实现会话管理


");
  out.println("用户名:"+request.getParameter("username")+"
");
  out.println("密码: "+request.getParameter("password")+"
");
  out.println("系统验证码:"+session.getAttribute("code")+"
");
  out.println("输入验证码:"+request.getParameter("inputedcode")+"
");
  out.println("");
  out.flush();
  out.close();
 }
}


你可能感兴趣的:(Servlet的4种会话管理技术)