Servlet&JSP的那些事儿(七)中我们介绍了会话管理,但是没有给出一个具体的实例。俗话说,耳听为虚眼见为实。所以,本篇讲述一个会话管理的实例。首先,建立一个web工程,LoginTest。建立的过程和之前MyDemo相同。不再赘述~LoginTest完成的主要功能为:开始时,这个程序采用基于cookie的会话更总,当客户端禁用cookie之后,采用URL重写的机制来进行会话跟踪。
首先编写一个工具类OutputSessionInfo。它有一个静态方法,该方法以表格形式输出session相关信息,方便我们随时查看。具体代码如下:
package com.shan.util;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class OutputSessionInfo{
public static void printSessionInfo (PrintWriter out,
HttpSession session) {
out.println("<table>");
out.println("<tr>");
out.println("<td>会话状态:</td>");
if(session.isNew()) {
out.println("<td>新的会话</td>");
} else {
out.println("<td>旧的会话</td>");
}
out.println("</tr>");
out.println("<tr>");
out.println("<td>会话ID:</td>");
out.println("<td>"+session.getId()+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>会话ID:</td>");
out.println("<td>"+session.getId()+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>创建时间:</td>");
out.println("<td>"+new Date(session.getCreationTime())+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>上次访问时间:</td>");
out.println("<td>"+new Date(session.getLastAccessedTime())+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>最大不活动时间间隔(s):</td>");
out.println("<td>"+session.getMaxInactiveInterval()+"</td>");
out.println("</tr>");
out.println("</table>");
}
}
然后,编写LoginServlet类。代码如下:
package com.shan.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.shan.util.OutputSessionInfo;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
HttpSession session = request.getSession();
String user = (String)session.getAttribute("user");
out.println("<html>");
//告诉浏览器不要缓存这个页面
out.println("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
out.println("<head><title>登录页面</title></head>");
out.println("<body>");
OutputSessionInfo.printSessionInfo(out,session);
out.println("<form method=post action=loginchk.do>");
out.println("<table>");
out.println("<tr>");
out.println("<td>请输入用户名:</td>");
if(user == null) {
out.println("<td><input type=text name=user></td>");
} else {
out.println("<td><input type=text name=user value="+user+"></td>");
}
out.println("</tr>");
out.println("<tr>");
out.println("<td>请输入密码:</td>");
out.println("<td><input type=password name=passwd></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td><input type=reset value=重填></td>");
out.println("<td><input type=submit value=登录></td>");
out.println("</tr>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
doGet(request,response);
}
}
编写LoginchkServlet类,代码如下:
package com.shan.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginchkServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String username = request.getParameter("user");
String passwd = request.getParameter("passwd");
if(username == null || passwd == null || username.equals("") || passwd.equals("")) {
response.sendRedirect("login.do");
} else {
HttpSession session = request.getSession();
session.setAttribute("user",username);
response.sendRedirect("welcome.do");
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
doGet(request,response);
}
}
编写WeclomeServlet类,代码如下:
package com.shan.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.shan.util.OutputSessionInfo;
public class WelcomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
HttpSession session = request.getSession();
String user = (String)session.getAttribute("user");
if(user == null) {
response.sendRedirect("/login.do");
} else {
String username=request.getParameter("username");
String welcomeInfo="Hello, "+ username;
out.println("<html><head><title>");
out.println("欢迎页面");
out.println("</title></head>");
out.println("<body>");
OutputSessionInfo.printSessionInfo(out,session);
out.println("欢迎你,"+user);
out.println("<a href=login.do>重新登录</a>");
out.println("<a href=logout.do>注销登录</a>");
out.println("</body></html>");
out.close();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
doGet(request,response);
}
}
编写LogoutServlet类,代码如下:
package com.shan.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
HttpSession session = request.getSession();
session.invalidate();
out.println("<html><head><title>");
out.println("退出登录");
out.println("</title></head>");
out.println("<body>");
out.println("已退出登录");
out.println("<a href=login.do>重新登录</a>");
out.println("</body></html>");
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
doGet(request,response);
}
}
最后,配置web.xml文件。如下:
<?xml version='1.0' encoding='utf-8'?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.shan.web.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginchkServlet</servlet-name>
<servlet-class>com.shan.web.LoginchkServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>com.shan.web.WelcomeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.shan.web.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginchkServlet</servlet-name>
<url-pattern>/loginchk.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/welcome.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>5</session-timeout>
</session-config>
</web-app>
完成后,还需要编译java文件。Dos环境下切换到工程所在目录,执行如下语句:
javac -classpath D:\apache-tomcat-7.0.33\lib\servlet-api.jar;classes -d classes src\com\shan\util\OutputSessionInfo.java
javac -classpath D:\apache-tomcat-7.0.33\lib\servlet-api.jar;classes -d classes src\\com\shan\web\LoginServlet.java
javac -classpath D:\apache-tomcat-7.0.33\lib\servlet-api.jar;classes -d classes src\\com\shan\web\LoginchkServlet.java
javac -classpath D:\apache-tomcat-7.0.33\lib\servlet-api.jar;classes -d classes src\\com\shan\web\WelcomeServlet.java
javac -classpath D:\apache-tomcat-7.0.33\lib\servlet-api.jar;classes -d classes src\\com\shan\web\LogoutServlet.java
最后,将编译好的文件拷贝到D:\apache-tomcat-7.0.33\webapps\LoginTest\WEB-INF\classes文件夹下,并把web.xml拷贝到D:\apache-tomcat-7.0.33\webapps\LoginTest\WEB-INF文件夹下。在Dos环境中执行startup,启动tomcat。运行结果如图1所示。
![](https://img-my.csdn.net/uploads/201212/05/1354686536_8310.png)
![](https://img-my.csdn.net/uploads/201212/05/1354686550_8836.png)
![](https://img-my.csdn.net/uploads/201212/05/1354686557_3421.png)
![](https://img-my.csdn.net/uploads/201212/05/1354686565_9669.png)
图1 实例的运行结果
可以禁用cookie,在IE或者chrome的设置中选择禁用cookie。然后就可以看到基于cookie的会话跟踪机制就失效了。这时,可以采用Servlet&JSP的那些事儿(七)所述的办法来解决上述问题。在此就不再细述,有兴趣可以自己添加,看看效果。
转载请注明出处:http://blog.csdn.net/iAm333