您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > java_web课件3
JavaWeb开发第三课Servlet(下)讲师:李玉明目标使用ServletContext掌握转发和包含掌握重定向掌握会话跟踪技术ServletContext和Web应用关系当Servlet容器启动Web应用时,并为每个Web应用创建惟一的ServletContext对象。当Servlet终止一个Web应用时,就会销毁它的ServletContext对象。你可以把ServletContext看成是一个Web应用的服务器端组件的共享内存。在ServletContext中可以存放共享数据,它提供了读取或设置共享数据的方法:•setAttribute(Stringname,Objectobject)把一个对象和一个属性名绑定,将这个对象存储在ServletContext中。•getAttribute(Stringname)根据给定的属性名返回所绑定的对象统计客户端访问次数packagelesson3;publicclassCounter{privateintcount;//计数值publicCounter(){this(0);}publicCounter(intcount){this.count=count;}publicvoidsetCount(intcount){this.count=count;}publicintgetCount(){returncount;}publicvoidadd(intstep){count+=step;}}packagelesson3;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassCounterServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//获得ServletContext的引用ServletContextcontext=getServletContext();//从ServletContext中读取counter属性Countercounter=(Counter)context.getAttribute(counter);//如果ServletContext中没有counter属性,就创建counter属性if(counter==null){counter=newCounter(1);context.setAttribute(counter,counter);}response.setContentType(text/html;charset=GB2312);PrintWriterout=response.getWriter();out.println(htmlheadtitleCounterServlet/TITLE/head);out.println(body);//输出当前的counter属性out.println(h1欢迎光临本站。您是第+counter.getCount()+位访问者。/h1);out.println(/body/html);counter.add(1);//将计数器递增1out.close();}}Web.xml增加内容:servletservlet-namecounter/servlet-nameservlet-classlesson3.CounterServlet/servlet-class/servletservlet-mappingservlet-namecounter/servlet-nameurl-pattern/counter/url-pattern/servlet-mapping测试结果Reload后:从1开始;ServletContextListener接口ServletContextListener接口能够监听ServletContext对象的生命周期。当Servlet容器启动或者终止Web应用时,会触发ServletContextEvent事件,该事件由ServletContextListener来处理:•contextInitialized(ServletContextsc):Servlet容器启动Web应用时调用该方法。•contextDestroyed(ServletContextsc)):Servlet容器终止Web应用时调用该方法。重新实现统计客户端访问次数在web应用启动时,从文件读取计数器的数值,并把表示计数器的Counter对象存放在Web应用范围内;在Web应用终止时把Web应用范围内的计数器的数值保存到count.txt文件中。packagelesson3;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjavax.servlet.ServletContext;importjavax.servlet.ServletContextEvent;importjavax.servlet.ServletContextListener;publicclassMyContextListenerimplementsServletContextListener{publicvoidcontextInitialized(ServletContextEventsce){System.out.println(myapplicationisInitialized.);//获取ServletContext对象ServletContextcontext=sce.getServletContext();try{//从文件中读取计数器的数值BufferedReaderreader=newBufferedReader(newInputStreamReader(context.getResourceAsStream(/count.txt)));intcount=Integer.parseInt(reader.readLine());reader.close();System.out.println(count=+count);//创建计数器对象Countercounter=newCounter(count);//把计数器对象保存到Web应用范围context.setAttribute(counter,counter);}catch(Exceptione){e.printStackTrace();}}publicvoidcontextDestroyed(ServletContextEventsce){System.out.println(myapplicationisDestroyed.);//获取ServletContext对象ServletContextcontext=sce.getServletContext();//从Web应用范围获得计数器对象Countercounter=(Counter)context.getAttribute(counter);if(counter!=null){try{//把计数器的数值写到count.txt文件中Stringfilepath=context.getRealPath(/);filepath=filepath+/count.txt;PrintWriterpw=newPrintWriter(filepath);System.out.println(count=+counter.getCount());pw.println(counter.getCount());pw.close();}catch(IOExceptione){e.printStackTrace();}}}}Web.xml增加内容listenerlistener-classlesson3.MyContextListener/listener-class/listenerListenerVSservlet生命周期Web应用启动时,servlet容器先调用listener的contextInitialzed()方法,再调用servlet的init()方法;Web应用终止时,servlet容器先调用servlet的destroy()方法,再调用listener的contextDestroyed()方法;在Web应用的生命周期中,ServletContext对象最早被创建,最晚被销毁。转发和包含请求转发:Servlet(源组件)先对客户请求做一些预处理操作,然后把请求转发给其他Web组件(目标组件)来完成包括生成响应结果在内的后续操作。包含:Servlet(源组件)把其他Web组件(目标组件)生成的响应结果包含到自身的响应结果中。转发和包含转发和包含共同点•源组件和目标组件处理的都是同一客户请求,源组件和目标组件共享同一个ServletRequest和ServletResponse对象;•目标组件可以为Servlet、JSP或者html文档;•都依赖于javax.servlet.RequestDispatcher接口。RequestDispatcher接口Javax.servlet.RequestDispatcher接口表示请求分发器,它有两个方法:•forword():把请求转发给目标组件:•Publicvoidforward(ServletRequestreq,ServletResponseres)throwsServletException,java.io.IOException•include():包含目标组件的响应结果:•Publicvoidforward(ServletRequestreq,ServletResponseres)throwsServletException,java.io.IOExceptionpackagelesson3;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.GenericServlet;importjavax.servlet.RequestDispatcher;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;publicclassCheckServletextendsGenericServlet{@Overridepublicvoidservice(ServletRequestrequest,ServletResponseresponse)throwsServletException,IOException{//TODOAuto-generatedmethodstub//读取用户名Stringusername=request.getParameter(username);Str
本文标题:java_web课件3
链接地址:https://www.777doc.com/doc-4876831 .html