3.9 编写监听器与过滤器

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

2004IBMSoftwareInstituteWSADWEBListener+Filter)2IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒWindows2000SP3WindowsXPƒWebSphereStudioApplicationDeveloperv5ƒDB2Version8.13IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒ786MRAM(1G)ƒ1.7G+4IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒ99999Request995IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒ9996IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒ9Web9Servlet9Servletƒ7IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒServlet2.49ServletContextAttributeListener9ServletContextListener9ServletRequestAttributeListener9ServletRequestListener9HttpSessionActivationListener9HttpSessionAttributeListener9HttpSessionBindingListener9HttpSessionListener8IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒsessionsessionƒsessionsession9IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteOnLineCounterListenerƒStp1eshopJavaSourceƃStp2com.webshop.listenerOnLineCounterListenerHTTP10IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteOnLineCounterListenerƒStp3ƒWSADOnLineCounterListener11IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteOnLineCounterListenerpackagecom.webshop.listener;importjavax.servlet.http.*;importjavax.servlet.*;publicclassOnLineCounterListenerimplementsHttpSessionListener{privateintcount;//privateServletContextcontext=null;//ServletpublicOnLineCounterListener(){count=0;}//sessionpublicvoidsessionCreated(HttpSessionEventse){count++;//1setContext(se);}//sessionpublicvoidsessionDestroyed(HttpSessionEventse){count--;//1setContext(se);}12IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteOnLineCounterListenerƒServletContextServletContextonLine//contextattributeReplacedattributeAddedpublicvoidsetContext(HttpSessionEventse){if(context==null)context=se.getSession().getServletContext();context.setAttribute(“onLine”,newInteger(count));//Servlet}13IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteJSPƒOnLineCounterListenerServletContextonLineJSPƒbottom.jspfpalign=centerahref==/images/poweredby.pngborder=0width=160height=98/abr/pcenter%=getServletContext().getAttribute(onLine)==null?0:getServletContext().getAttribute(onLine)%/center/body/html14IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstitute15IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒWebJSPServletƒ16IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒWeb17IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒStp1javax.servlet.FilterƒStp2web.xmlƒStp3web.xml18IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒStp1eshopJavaSourceÆ19IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒStp2com.webshop.filterEncodingFilter20IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteRequestƒStp3encodinggb2312URL“/*”URL21IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteEncodingFilterpackagecom.webshop.filter;importjavax.servlet.FilterChain;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjava.io.IOException;importjavax.servlet.Filter;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.ServletException;importjavax.servlet.FilterConfig;publicclassEncodingFilterimplementsFilter{protectedFilterConfigfilterConfig;privateStringtargetEncoding=gb2312;/***,Servlet*/publicvoidinit(FilterConfigconfig)throwsServletException{this.filterConfig=config;this.targetEncoding=config.getInitParameter(“encoding”);//}22IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteEncodingFilter/****/publicvoiddoFilter(ServletRequestsrequest,ServletResponsesresponse,FilterChainchain)throwsIOException,ServletException{HttpServletRequestrequest=(HttpServletRequest)srequest;request.setCharacterEncoding(targetEncoding);//chain.doFilter(srequest,sresponse);}publicvoidsetFilterConfig(finalFilterConfigfilterConfig){this.filterConfig=filterConfig;}//publicvoiddestroy(){this.filterConfig=null;}23IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒWSADƒweb.xml24IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒWebJSPStrutsActionƒ99JSP25IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteSignonFilterpublicvoiddoFilter(ServletRequestreq,ServletResponseresp,FilterChainchain)throwsServletException,IOException{HttpServletRequestrequest=(HttpServletRequest)req;HttpServletResponseresponse=(HttpServletResponse)resp;AccountFormacctForm=(AccountForm)request.getSession().getAttribute(accountForm);CartFormcartForm=(CartForm)request.getSession().getAttribute(cartForm);//acctFormacctForm.getAccount()if(acctForm==null||acctForm.getAccount()==null){request.setAttribute(targetPage,request.getRequestURI());//response.sendRedirect(LOGIN_PAGE);}else{chain.doFilter(req,resp);//}}26IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteSignonFilterƒSignonFilterƒURLURLƒweb.xmlSignonFilterURLURL27IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒƒ1SignonFilterƒ2EncodingFilterÆÆÆÆ28IBMSoftwareInstituteWSADWEB2004IBMSoftwareInstituteƒWebServletServletƒWebJSPServlet

1 / 28
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功