Servlet和Listener集成一个程序为什么有问题
在上一篇总结中我写到我看到这个题目的第一个想法是写出Servlet和Listener集成的一个程序。第一是实现过程中出现了很多始料未及的异常情况。后来我想了一下,之所以SUN公司的费劲千辛万苦将不同功能的程序分离若干个小程序,实现框架化编程模式的目的是提升编程效率,简化流程和逻辑,但是我想把他们集成起来,这不是使事情变的更复杂了吗?无论是从逻辑上还是从效率上都产生了不良的影响。于是我放弃了这个方案,转而使用较为稳妥的Servlet和Listener分离的方案。中间的数据使用ServletContext传输。因为ServletContext的作用范围是整个Web应用程序,理论上可以实现Listener与Servlet之间的数据共享。
前一篇总结最大的问题
在昨天的总结中最大的问题是反应速度太慢了,我认为主要是从txt文本读取信息到JS中需要反应时间过长。但是今天的方案明速度正常了很多,可以说是解决了问题。
最终方案
1.监听器
主要使注意实现一个监听Context创建的方法,在这个方法中实现获取Context实例,然后将属性添加上去。
package com.filter;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Application Lifecycle Listener implementation class OnLineuser
*
*/
@WebListener
public class OnLineuser implements ServletContextListener, HttpSessionListener {
private ServletContext context;
private int count=0;
/**
* Default constructor.
*/
public OnLineuser() {
// TODO Auto-generated constructor stub
}
private void logout(int message)
{
PrintWriter out=null;
try {
out=new PrintWriter(new FileOutputStream("D://ServletDemo/src/main/webapp/log.txt"));
out.println(message);
out.close();
}catch(Exception e) {
out.close();
e.printStackTrace();//控制台输出;
}
}
/**
* @see HttpSessionListener#sessionCreated(HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent se) {
// TODO Auto-generated method stub
count++;
System.out.println("创建session方法启动,当前Session数:"+count);
context.setAttribute("count", count);
logout(count);
}
/**
* @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent se) {
// TODO Auto-generated method stub
count--;
System.out.println("删除session方法启动,当前Session数:"+count);
context.setAttribute("count", count);
logout(count);
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
context=sce.getServletContext();//获得这个实例
}
}
2.Servlet实现
主要注意实现对于Listener监听到的数据的接受,并将这个数据传递到JS中
package com.servelt;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
//import javax.servlet.ServletContext;
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 OnLineServlet
*/
@WebServlet("/OnLineServlet")
public class OnLineServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
//private ServletContext context=this.getServletContext();
/**
* @see HttpServlet#HttpServlet()
*/
public OnLineServlet() {
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
PrintWriter out=response.getWriter();
ServletContext context=this.getServletContext();//获得Context
Integer temp=(Integer)context.getAttribute("count");
out.println(temp);
out.flush();
out.close();
}
/**
* @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);
}
/**
* @see HttpSessionListener#sessionCreated(HttpSessionEvent)
*/
}
3.实现前端JSP文件(集成JS)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
var xmlHttp;
var i=0;
function createXMLHttpRequest()
{
xmlHttp=new XMLHttpRequest();
}
function updateUser()
{
createXMLHttpRequest();
xmlHttp.onreadystatechange=processor;
i=i+1;
xmlHttp.open("GET", "OnLineServlet?count="+i);
xmlHttp.send(null);
}
function processor()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("count").innerHTML=xmlHttp.responseText;
}
}
}
setInterval("updateUser();",1);
</script>
<title>动态显示在线人数</title>
</head>
<body>
<div id="count"></div>
<!-- 直接连接log.txt -->
</body>
</html>