实验五会话管理(实验报告)班级:网络111姓名:李坤学号:201106090117一、实验目的1.了解Web服务器对客户会话跟踪的各种方法;2.重点掌握使用HttpSession对象跟踪会话的方法;3.掌握使用Cookie技术跟踪会话的方法;4.了解URL重写和隐藏表单域的方法。二、实验原理HTTP协议是无状态的协议。在很多情况下,Web服务器必须能够跟踪客户的状态。比如,对于一个购物网站,在一个时刻可能有多个客户购物,Web服务器必须能够区分不同的客户。一般情况下,Web服务器为每个客户配置了虚拟的购物车(ShoppingCart)。当某个客户请求将一个商品放入购物车时,Web服务器必须根据发出请求的客户的身份,找到该客户的购物车,然后把商品放入其中。Web服务器跟踪客户的状态通常有4种方法:(1)使用HttpSession对象管理会话;(2)使用持久的Cookie对象;(3)使用URL重写机制;(4)使用隐藏的表单域。三、实验内容与步骤(一)使用HttpSession对象管理会话。在名为ch05的Web项目下,创建一个名为ShowSessionInfo的Servlet。代码importjava.io.IOException;importjava.io.PrintWriter;importjava.sql.Date;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;publicclassShowSessionServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType(text/html;charset=gb2312);HttpSessionsession=request.getSession(true);Stringheading=null;Stringinfo=Informationaboutyoursession;IntegeraccessCount=(Integer)session.getAttribute(accessCount);if(accessCount==null){accessCount=newInteger(1);heading=Welcome,enterthispagefirsttime!;}else{heading=WelcomeBack!;accessCount=accessCount+1;}session.setAttribute(accessCount,accessCount);PrintWriterout=response.getWriter();out.println(HTML);out.println(BODYcenter);out.println(h2+heading+ahref='showsession.do'enteragain/a+/h2+h3+info+/h3);out.println(tableborder='0');out.println(trbgcolor=\ffad0\tdinformatontdvalue\n);Stringstate=session.isNew()?newsession:oldsession;out.println(trtdsessionstate:td+state+\n);out.println(trtdsessionID:td+session.getId()+\n);out.println(trtdcreatetime:td);out.println(+newDate(session.getCreationTime())+\n);out.println(trtdlastaccesstime:td);out.println(+newDate(session.getLastAccessedTime())+\n);out.println(trtdmaxinactiveinterval:td+session.getMaxInactiveInterval()+\n);out.println(trtdCookie:td+request.getHeader(Cookie)+\n);out.println(trtdaccesscount:td+accessCount+\n);out.println(/table);out.println(/center/BODY);out.println(/HTML);}}结果(二)使用HttpSession会话对象设计一个GuessNumberServlet.java,实现简单的猜数游戏:代码importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Random;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;publicclassGuessNumberServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{intmagic=newRandom().nextInt(100)+1;HttpSessionsession=request.getSession();session.setAttribute(num,newInteger(magic));response.setContentType(text/html;charset=gb2312);PrintWriterout=response.getWriter();out.println(HTMLBODY);out.println(Pleaseguessanumber,thenumberisbetween0and100!);out.println(formaction='/ch05/guessnumber.do'method='post');out.println(inputtype='text'name='guess');out.println(inputtype='submit'value='submit');out.println(/form);out.println(/BODY/HTML);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{intguess=Integer.parseInt(request.getParameter(guess));HttpSessionsession=request.getSession();intmagic=(Integer)session.getAttribute(num);response.setContentType(text/html;charset=gb2312);PrintWriterout=response.getWriter();out.println(HTMLBODY);if(guess==magic){session.invalidate();out.println(Congratulation,you'reright!);out.println(ahref='/ch05/guessnumber.do'Guessagain/a);}elseif(guessmagic){out.println(It'stoolittle,pleasetryagain!);}else{out.println(It'stoolarge,pleasetryagain!);}out.println(formaction='/ch05/guessnumber.do'method='post');out.println(inputtype='text'name='guess');out.println(inputtype='submit'value='submit');out.println(/form);out.println(/BODY/HTML);}}结果(三)编写一个CheckUserServlet,通过Cookie实现自动登录的功能。(参考程序4.5)(四)使用Cookie跟踪会话,与实验3要求类似,尝试用JSP实现。1)编写cookieTest.jsp页面:首先从客户端读取Cookies,判断是否其中包含了名为“mrCookie”的cookie,如果存在,从该cookie值中取出用户名和用户的登录时间,显示欢迎信息;如果该cookie不存在,显示用户登录的表单,要求用户输入用户名,表单处理程序为deal.jsp。2)编写deal.jsp:获取用户输入的登录信息,创建cookie,其名称为“mrCookie”,其值为“用户名#用户登录时间”,保存该cookie并设置其有效生存期为3天,处理完后将页面转发到cookieTest.jsp,测试cookieTest.jsp中cookie保存用户信息实现用户自动登录的效果。(五)URL重写编写HomeServlet.java,对通过超链接请求的两个URL进行重写,在浏览器中禁用Cookie后,servlet运行效果如下图所示。代码importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;publicclassHomeServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{response.setContentType(text/html;charset=gb2312);HttpSessionsession=request.getSession();PrintWriterout=response.getWriter();out.println(H