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