Servlet的会话管理技术主要有4种:URL重写,Cookie,隐藏表单域以及HttpSession。下面的例子通过一个动态验证码的实例详细说明了这4种技术的具体应用方式。为了简单起见,动态验证码没有采用图片生成技术,其原理和效果与本例中相同。另外,为了代码结构的清晰性,在登录表单生成和登录验证部分都没有采用统一的代码。对这些代码稍作修改,就可以实现单一的表单生成Servlet和验证Servlet,感兴趣的朋友可以与我联系(qingbt@126.com)。
//************************************** index.jsp代码:
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>Servlet会话管理实例</title>
</head>
<body>
<h2>Servlet会话管理实例</h2><hr>
本实例通过实现用户登录过程中的动态验证码校验功能,说明4种Session管理方法的特点。<br>
<ul>
<li><a href="UrlRedirect">URL重写方法</a></li>
<li><a href="HiddenField">隐藏表单域方法</a></li>
<li><a href="Cookie">Cookie方法</a></li>
<li><a href="HttpSessionManagement">HttpSession方法</a></li>
</ul>
</body>
</html>
//************************************** web.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app >
<servlet>
<servlet-name>UrlRedirect</servlet-name>
<servlet-class>UrlRedirect</servlet-class>
</servlet>
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>LoginWithUrlRedirect</servlet-class>
</servlet>
<servlet>
<servlet-name>HiddenField</servlet-name>
<servlet-class>HiddenField</servlet-class>
</servlet>
<servlet>
<servlet-name>Cookie</servlet-name>
<servlet-class>CookieManagement</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginWithCookie</servlet-name>
<servlet-class>LoginWithCookie</servlet-class>
</servlet>
<servlet>
<servlet-name>HttpSessionManagement</servlet-name>
<servlet-class>HttpSessionManagement</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginWithSession</servlet-name>
<servlet-class>LoginWithSession</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UrlRedirect</servlet-name>
<url-pattern>/UrlRedirect</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HiddenField</servlet-name>
<url-pattern>/HiddenField</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Cookie</servlet-name>
<url-pattern>/Cookie</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginWithCookie</servlet-name>
<url-pattern>/LoginWithCookie</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HttpSessionManagement</servlet-name>
<url-pattern>/HttpSessionManagement</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginWithSession</servlet-name>
<url-pattern>/LoginWithSession</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
//************************************** 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("<HTML>");
out.println("<HEAD><TITLE>URL重写</TITLE></HEAD>");
out.println("<BODY><h2>通过URL重写实现会话管理</h2><hr>");
out.println("<form action='Login?code="+validationCode+"' method=post>");
out.println("用户名:<input type=text width=10 name='username' value='abc'><br>");
out.println("密码: <input type=password width=10 name='password' value='123'><br>");
out.println("验证码:<input type=text width=10 name='inputedcode'>"+validationCode);
out.println("<input type=submit value='登录'><br>");
out.println("</form>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
[1] [2] [3] 下一页