Listener执行流程
每次说起监听器,总是有些空的感觉,那么它到底是个什么东西呢?
一.概念
监听器也叫 Listener,是 Servlet的监听器,它可以监听客户端的请求,服务端的操作等。
监听器可以自动激发一些操作,比如监听在线的用户的数量. 当增加一个 HttpSession时,就激发 sessionCreated(HttpSessionEvent se) 方法,这样就可以给在线人数加 1。
二.执行原理
其实监听器的执行类似于触发器,当某些动作(接口中定义好的)执行时就会触发(至于到底是如何触发的就是底层的问题了)相应的Listener,执行相应的操作;当然如果没有执行对应的动作,则监听器就一直监听着,没有操作。
下面我来举个例子(监听在线用户的数量),来展示一下Listener的执行过程,先来看一下它的时序图:
login.jsp
[java] <%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%
String command = request.getParameter("command");
if ("login".equals(command)) {
if ("dan".equals(request.getParameter("userId"))
&& "123".equals(request.getParameter("password"))) {
//登陆成功将用户信息放到session中
session.setAttribute("user_name",
request.getParameter("userId"));
//设置超时,单位:秒
session.setMaxInactiveInterval(6000);
//重定向到主控页面
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录</title>
<SCRIPT language=JavaScript>
function init() {
loginForm.userId.focus();
}
</SCRIPT>
</head>
<body onload=init()>
<FORM name="loginForm">
<input type="hidden" name="command" value="login">
用户名:
<INPUT name="userId" value="dan" type="text" size="20" maxlength="20">
<p>密 码:
<INPUT name="password" value="123" type="password" size="21" maxlength="20">
<input type="submit" onclick="submitForm()" value="提交" name="login" id="login">
</FORM>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%
String command = request.getParameter("command");
if ("login".equals(command)) {
if ("dan".equals(request.getParameter("userId"))
&& "123".equals(request.getParameter("password"))) {
//登陆成功将用户信息放到session中
session.setAttribute("user_name",
request.getParameter("userId"));
//设置超时,单位:秒
session.setMaxInactiveInterval(6000);
//重定向到主控页面
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录</title>
<SCRIPT language=JavaScript>
function init() {
loginForm.userId.focus();
}
</SCRIPT>
</head>
<body onload=init()>
<FORM name="loginForm">
<input type="hidden" name="command" value="login">
用户名:
<INPUT name="userId" value="dan" type="text" size="20" maxlength="20">
<p>密 码:
<INPUT name="password" value="123" type="password" size="21" maxlength="20">
<input type="submit" onclick="submitForm()" value="提交" name="login" id="login">
</FORM>
</body>
</html>
TestHttpSessionListener.java
[java] package listener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class TestHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("-------------TestHttpSessionListener.sessionCreated---------------------");
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
}
}
package listener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class TestHttpSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("-------------TestHttpSessionListener.sessionCreated---------------------");
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
}
}
TestHttpSes
补充:软件开发 , Java ,