【Spring in action】SpringMVC的准备工作:Servlet基础
2017-09-07 19:37
603 查看
前言:
基于Spring思想的web框架SpringMVC。需要了解SpringMVC需要知道Servlet的工作方式,很简单。两篇文章,一篇各种原理组件,一篇实战搞定。高手请忽略。
正文:
1》publicclass MyServletextends HttpServlet
总结1:
在Servlet1中:新建一个servlet-mapping对象:外网访问地址http://localhost:8080/Servlet1/myServlet ,由此处约定。
关键代码新建一个servlet对象:MyServlet-->com.demo.MyServlet。外网根据MyServlet输出hello servlet!
PS:常见问题:
1、严重: Allocate exception for servlet MyServlet
java.lang.ClassNotFoundException: com.demo
原因: web.xml需要完整路径
例如错误代码:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.demo</servlet-class>
</servlet>
我的路径为com.demo.MyServlet,而我只是写了包名。
总结2:
servlet生命周期:构造方法-init初始化-doget/dopost-destory
修改web.xml,添加LoginServlet:
九月 07, 2017 2:37:08 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 4956 ms
Constructor...
init...
doFilter...
doGet...
1》在web.xml下配置:和上面的servlet平级处配置:
执行结果,由于设置/*,所有请求均会执行:
doFilter...
结果:不管调用什么,例如jdbclogin.html也会调用fliter过滤器
如果指定包下过滤:则重新配置如下:(admin下面生效)
<!-- /admin/* admin文件夹下的才需要过滤 -->
<filter-mapping>
<filter-name>MyFlter</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
例如:
http://localhost:8080/Servers1/login.html不会执行过滤器 http://localhost:8080/Servers1/admin/admin.html会执行过滤器
@WebServlet("/HeaderServlet")
执行结果:
doFilter...
name:host value:localhost:8080
name:connection value:keep-alive
name:upgrade-insecure-requests value:1
name:accept value:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
name:user-agent value:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Safari/522.0
name:accept-language value:zh-cn
name:cache-control value:no-cache
name:accept-encoding value:gzip, deflate
PS:其中如何设置返回每一秒刷新一次:例如显示不断变化的时间日期:在doGet中输出:
1》设置Cookies:
doFilter...
2》从客户端获取Cookies:
doFilter...
key:geek99value:www.geek99.com
其中:设置Cookies是通过response返回给客户端,获取Cookies是通过请求request获取。
给客户端设置Session和取出Session:
1》设置Session:
doFilter...
2》获取Session:
doFilter...
name:session_value
总结:区别于Cookies:Sesion的设置和获取都是通过request进行的。因此,Cookies是设置在浏览器本地的。不随着浏览器关闭而被清除。Session浏览器关闭则清除,同一浏览器不同页签可使用。
ServletContext整个应用程序
HttpSession当前浏览器
HttpServletRequest当前请求
1》设置共享变量和获取共享变量:
getShareVar如下:
执行/setShare打印日志如下:
ctxValue:ctx_value
sessionValue:session_value
requestValue:request_value
打开新浏览器如下:其中ctx的值携带了,而session,request_value并没有。
ctxValue:ctx_value
sessionValue:null
requestValue:null
1)整个工程状态的改变
servletContextListener ServletContextAttributeListener
2)session属性的变化
httpSessionListener httpSessionAttributeListener
3)request属性的变化
servletRequestListener servletRequestAttributeListener
1》对整个Servlet监听:前置性的工作可置于此处contextInitialized
implements ServletContextListener
写一个MyListener继承自ServletContextListener(还有其他的可以供继承),MyListener
在web.xml下配置:和上面的servlet平级处配置:
启动初始化服务的时候,会执行监听。打印:
contextInitialized...
attributeAdded...
2》对Session进行监听:
写一个类SessionListenerimplements HttpSessionListener :
当前在线人数:0
doFilter...
当前在线人数:1
name:null
doFilter...
当前在线人数:2
3》对attribute的监听:
第一次执行/SetShareVar提示
attributeAdded...
第二次执行/SetShareVar提示
attributeReplaced...
销毁时执行
attributeRemoved...
基于Spring思想的web框架SpringMVC。需要了解SpringMVC需要知道Servlet的工作方式,很简单。两篇文章,一篇各种原理组件,一篇实战搞定。高手请忽略。
正文:
一、java的Servlet的使用:
1、写一个Servlet继承HttpServlet:1》publicclass MyServletextends HttpServlet
package com.demo; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public MyServlet() { super(); // TODO Auto-generated constructor stub } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub // super.doGet(req, resp); // logic HttpServletRequest请求,HttpServletResponse 响应 PrintWriter out = resp.getWriter(); out.print("hello servlet!"); out.close(); // // String str = this.getServletContext().getInitParameter("servlet_name"); // System.out.println(str); // resp.sendRedirect("http://www.baidu.com"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub // super.doPost(req, resp); doGet(req, resp); } }2》在WEB-INF下写一个web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Servlet1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>com.demo.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/myServlet</url-pattern> </servlet-mapping> </web-app>
总结1:
在Servlet1中:新建一个servlet-mapping对象:外网访问地址http://localhost:8080/Servlet1/myServlet ,由此处约定。
关键代码新建一个servlet对象:MyServlet-->com.demo.MyServlet。外网根据MyServlet输出hello servlet!
PS:常见问题:
1、严重: Allocate exception for servlet MyServlet
java.lang.ClassNotFoundException: com.demo
原因: web.xml需要完整路径
例如错误代码:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.demo</servlet-class>
</servlet>
我的路径为com.demo.MyServlet,而我只是写了包名。
总结2:
servlet生命周期:构造方法-init初始化-doget/dopost-destory
2、写一个生命周期看效果:LoginServletextends HttpServlet
package com.demo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { // TODO Auto-generated constructor stub System.out.println("Constructor..."); } @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.println("init..."); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("doGet..."); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // System.out.println("doPost..."); PrintWriter out = response.getWriter(); out.print("<h1>hello servlet</h1>"); // request.getRequestDispatcher("hello.html").forward(request, response); request.getRequestDispatcher("hello.html").include(request, response); } @Override public void destroy() { // TODO Auto-generated method stub System.out.println("destroy..."); } }
修改web.xml,添加LoginServlet:
<servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.demo.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping>直接执行该Servlet,执行结果:
九月 07, 2017 2:37:08 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 4956 ms
Constructor...
init...
doFilter...
doGet...
3、过滤器:对特定请求进行过滤。
作用:客户端和服务端请求资源之间,起到过滤作用:session管理、权限管理、日志管理等。1》在web.xml下配置:和上面的servlet平级处配置:
<filter> <filter-name>MyFlter</filter-name> <filter-class>com.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFlter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>2》写一个MyFilter继承自Filter
package com.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class MyFilter implements Filter{ @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub System.out.println("doFilter..."); //过滤传递 arg2.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
执行结果,由于设置/*,所有请求均会执行:
doFilter...
结果:不管调用什么,例如jdbclogin.html也会调用fliter过滤器
如果指定包下过滤:则重新配置如下:(admin下面生效)
<!-- /admin/* admin文件夹下的才需要过滤 -->
<filter-mapping>
<filter-name>MyFlter</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
例如:
http://localhost:8080/Servers1/login.html不会执行过滤器 http://localhost:8080/Servers1/admin/admin.html会执行过滤器
4、获得请求头header信息:
package com.demo; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HeaderServlet */ @WebServlet("/HeaderServlet") public class HeaderServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HeaderServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Enumeration<String> enum1 = request.getHeaderNames(); while (enum1.hasMoreElements()) { String string = (String) enum1.nextElement(); String value = request.getHeader(string); System.out.println("name:"+string +" value:"+value); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }此处不需要修改web.xml配置,原因是此处通过注解进行了mapping映射:
@WebServlet("/HeaderServlet")
执行结果:
doFilter...
name:host value:localhost:8080
name:connection value:keep-alive
name:upgrade-insecure-requests value:1
name:accept value:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
name:user-agent value:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Safari/522.0
name:accept-language value:zh-cn
name:cache-control value:no-cache
name:accept-encoding value:gzip, deflate
PS:其中如何设置返回每一秒刷新一次:例如显示不断变化的时间日期:在doGet中输出:
response.setHeader("refresh", "1"); PrintWriter out = response.getWriter(); out.println(new Date().toString());
5、持久化技术:Cookies和Session:
给客户端设置Cookies和从客户端取Cookies:1》设置Cookies:
package com.demo; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class CookieServlet */ @WebServlet("/CookieServlet") public class CookieServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CookieServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Cookie c = new Cookie("geek99", "www.geek99.com"); c.setMaxAge(60*60*24*7); response.addCookie(c); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }执行结果:日志打印仅一个过滤器表示执行了。
doFilter...
2》从客户端获取Cookies:
package com.demo; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class GetCookieServlet */ @WebServlet("/GetCookieServlet") public class GetCookieServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public GetCookieServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Cookie[] cs = request.getCookies(); for (int i = 0; i < cs.length; i++) { System.out.println("key:"+cs[i].getName()+"value:"+cs[i].getValue()); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }执行结果:
doFilter...
key:geek99value:www.geek99.com
其中:设置Cookies是通过response返回给客户端,获取Cookies是通过请求request获取。
给客户端设置Session和取出Session:
1》设置Session:
package com.demo; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class SetSessionServlet */ @WebServlet("/SetSessionServlet") public class SetSessionServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SetSessionServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub HttpSession session = request.getSession(); session.setAttribute("session_name", "session_value"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }执行结果:
doFilter...
2》获取Session:
package com.demo; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class GetSessionServlet */ @WebServlet("/GetSessionServlet") public class GetSessionServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public GetSessionServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub HttpSession session = request.getSession(); String value = (String) session.getAttribute("session_name"); System.out.println("name:"+value); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }执行结果:
doFilter...
name:session_value
总结:区别于Cookies:Sesion的设置和获取都是通过request进行的。因此,Cookies是设置在浏览器本地的。不随着浏览器关闭而被清除。Session浏览器关闭则清除,同一浏览器不同页签可使用。
6、共享变量
变量的作用域ServletContext整个应用程序
HttpSession当前浏览器
HttpServletRequest当前请求
1》设置共享变量和获取共享变量:
package com.shareVar; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class SetShareVar * @WebServlet("/SetShareVar") */ public class SetShareVar extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SetShareVar() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //1.ServletContext //2.HttpSession //3.HttpServletRequest ServletContext ctx = this.getServletContext(); //1、ServletContext共享变量 ctx.setAttribute("ctx_name", "ctx_value"); HttpSession session = request.getSession(); //2、HttpSession共享变量 session.setAttribute("session_name", "session_value"); request.setAttribute("request_name", "request_value"); //3.上一行代码注释:HttpServletRequest携带信息到/getShareVar 的mapping指向的页面 request.getRequestDispatcher("getShareVar").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
getShareVar如下:
package com.shareVar; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class getShareVar * @WebServlet("/getShareVar") */ public class getShareVar extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public getShareVar() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub ServletContext ctx = this.getServletContext(); String ctxValue = (String) ctx.getAttribute("ctx_name"); HttpSession session = request.getSession(); String sessionValue = (String)session.getAttribute("session_name"); String requestValue = (String)request.getAttribute("request_name"); System.out.println("ctxValue:"+ctxValue); System.out.println("sessionValue:"+sessionValue); System.out.println("requestValue:"+requestValue); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }web.xml配置略;
执行/setShare打印日志如下:
ctxValue:ctx_value
sessionValue:session_value
requestValue:request_value
打开新浏览器如下:其中ctx的值携带了,而session,request_value并没有。
ctxValue:ctx_value
sessionValue:null
requestValue:null
7、监听:
监听web工程状态的改变1)整个工程状态的改变
servletContextListener ServletContextAttributeListener
2)session属性的变化
httpSessionListener httpSessionAttributeListener
3)request属性的变化
servletRequestListener servletRequestAttributeListener
1》对整个Servlet监听:前置性的工作可置于此处contextInitialized
implements ServletContextListener
写一个MyListener继承自ServletContextListener(还有其他的可以供继承),MyListener
package com.listener; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; /** * Application Lifecycle Listener implementation class MyListener * */ public class MyListener implements ServletContextListener { /** * Default constructor. */ public MyListener() { // TODO Auto-generated constructor stub } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } /** * @see ServletContextListener#contextInitialized(ServletContextEvent) */ public void contextInitialized(ServletContextEvent arg0) { System.out.println("contextInitialized..."); ServletContext sctx = arg0.getServletContext(); sctx.setAttribute("onLineCount", 0); } }
在web.xml下配置:和上面的servlet平级处配置:
<listener> <listener-class>com.listener.MyListener</listener-class> </listener>
启动初始化服务的时候,会执行监听。打印:
contextInitialized...
attributeAdded...
2》对Session进行监听:
写一个类SessionListenerimplements HttpSessionListener :
package com.listener; import javax.servlet.ServletContext; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class SessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent arg0) { // TODO Auto-generated method stub ServletContext ctx = arg0.getSession().getServletContext(); Integer numSessions = (Integer) ctx.getAttribute("numSessions"); if (numSessions == null) { numSessions = new Integer(1); } else { int count = numSessions.intValue(); numSessions = new Integer(count + 1); } ctx.setAttribute("numSessions", numSessions); System.out.println("当前在线人数:" + numSessions); } @Override public void sessionDestroyed(HttpSessionEvent arg0) { // TODO Auto-generated method stub ServletContext ctx = arg0.getSession().getServletContext(); Integer numSessions = (Integer) ctx.getAttribute("numSessions"); if (numSessions == null) { numSessions = new Integer(0); } else { int count = numSessions.intValue(); numSessions = new Integer(count - 1); } ctx.setAttribute("numSessions", numSessions); System.out.println("当前在线人数:" + numSessions); } }在web.xml中添加:
<listener> <listener-class>com.listener.SessionListener</listener-class> </listener>每打开一个浏览器,调用前面的/GetSessionServlet 会触发导致在线人数加1.
当前在线人数:0
doFilter...
当前在线人数:1
name:null
doFilter...
当前在线人数:2
3》对attribute的监听:
package com.listener; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; /** * Application Lifecycle Listener implementation class MyServletContextAttributeListener * */ public class MyServletContextAttributeListener implements ServletContextAttributeListener { /** * Default constructor. */ public MyServletContextAttributeListener() { // TODO Auto-generated constructor stub } /** * @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent) */ public void attributeAdded(ServletContextAttributeEvent arg0) { // TODO Auto-generated method stub System.out.println("attributeAdded..."); } /** * @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent) */ public void attributeRemoved(ServletContextAttributeEvent arg0) { // TODO Auto-generated method stub System.out.println("attributeRemoved..."); } /** * @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent) */ public void attributeReplaced(ServletContextAttributeEvent arg0) { // TODO Auto-generated method stub System.out.println("attributeReplaced..."); } }添加web.xml配置略。
第一次执行/SetShareVar提示
attributeAdded...
第二次执行/SetShareVar提示
attributeReplaced...
销毁时执行
attributeRemoved...
相关文章推荐
- 【Spring in action】SpringMVC的准备工作:Servlet实战
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-003- 上传文件multipart,配置StandardServletMultipartResolver、CommonsMultipartResolver
- maven 项目(四) spring集成springMVC开发统一接入API(准备工作:第二部分)
- (Spring In Action)bean注入基础知识
- Spring in Action(五):基于Java Config的SpringMVC
- 【Spring in action】SpringMVC基于xml及java配置的简单运用
- SpringMVC(1):基础配置文件web.xml 和 <servlet-name>-servlet.xml / springmvc.xml
- 【Spring in Action】Spring的AOP基础知识及切面运用
- maven 项目(四) spring集成springMVC开发统一接入API(准备工作:第一部分)
- 【Spring in action】基于xml配置:在SpringMVC中使用Spring容器
- Servlet [springDispatcherServlet] in web application [/SpringMVC-1] threw load() exception java.lang
- No mapping found for HTTP request with URI [/springmvc/queryitems.action] in DispatcherServlet with
- Servlet [springDispatcherServlet] in web application [/SpringMVC-1] threw load() exception java.lang
- 【spring in action 学习--springMVC搭建】在不使用web.xml的情况下,配置Srping MVC工程
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-001- DispatcherServlet的高级配置(ServletRegistration.Dynamic、WebApplicationInitializer)
- springMVC+freemarker 中 Could not resolve view with name 'XXX.ftl' in servlet with name 'SpringMVC'
- 【Spring in action】基于Java配置SpringMVC中使用Spring及SpringMVC各种请求详解
- SPRING IN ACTION 第4版笔记-第五章Building Spring web applications-001-SpringMVC介绍
- Spring in Action 读书笔记 第七章 SpringMVC
- 一、Spring In Action笔记之基础