Servlet监听器在开发中的应用案例

案例:显示登陆用户列表,并实现踢人功能。
 
登陆的jsp页面------------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


	
		My JSP 'login.jsp' starting page
	
	
		
用户名:
密    码:

 
处理登陆请求的servlet---------------------------
package com.hbsi.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hbsi.domain.User;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		User user=new User();
		user.setUsername(username);
		user.setPassword(password);
		request.getSession().setAttribute("user",user);
		response.sendRedirect("/ListenerKicking/index.jsp");
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 
显示登陆成功与否的jsp页面------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


  
    My JSP 'index.jsp' starting page
  
  
  
    欢迎您:${user.username} 

 
显示用户列表的jsp页面-----------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>


	
		My JSP 'userJsp.jsp' starting page
	
	
		当前在线用户为:
		
${me.key}剔出去

 
处理踢人请求的servlet-----------------------------
package com.hbsi.servlet;

import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class KickUserServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username=request.getParameter("username");
		username=new String(username.getBytes("iso8859-1"),"Utf-8");
		Map map=(Map) this.getServletContext().getAttribute("map");
		HttpSession session=(HttpSession) map.get(username);
		if(session!=null){
			session.invalidate();
			map.remove(username);
		}
		request.getRequestDispatcher("/userJsp.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

 

servlet监听器-------------

package com.hbsi.listener;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import com.hbsi.domain.User;

public class SessionListener implements HttpSessionAttributeListener {

	@Override
	public void attributeAdded(HttpSessionBindingEvent se) {
		Object obj=se.getValue();
		if(obj instanceof User){
			User user=(User) obj;
		    HttpSession session=se.getSession();
		    Map map=(Map) session.getServletContext().getAttribute("map");
		    if(map==null){
		    	map=new HashMap();
		    	session.getServletContext().setAttribute("map",map);
		    }
		    map.put(user.getUsername(),session);
		}
	}

	@Override
	public void attributeRemoved(HttpSessionBindingEvent se) {
		
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent se) {
		// TODO Auto-generated method stub
		
	}

}

实体类------------------------
package com.hbsi.domain;

public class User {
  private String username;
  private String password;
public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}
  
}


处理乱码的过滤器----------------
package com.hbsi.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 CharseterEncodingFilter implements Filter {
	
	private FilterConfig config = null;
	private String defaultCharset = "UTF-8";

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		
		String charset = config.getInitParameter("charset");
		if(charset==null){
			charset = defaultCharset;
		}
		request.setCharacterEncoding(charset);
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset="+charset);
		
		chain.doFilter(request, response);
		

	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.config = filterConfig;

	}

}

配置文件-----------


  
  
    CharseterEncodingFilter
    com.hbsi.filter.CharseterEncodingFilter
    
       charset
       UTF-8
    
  
  
     CharseterEncodingFilter
     /*
  
  
     com.hbsi.listener.SessionListener
  
  
  
    LoginServlet
    com.hbsi.servlet.LoginServlet
  
  
    KickUserServlet
    com.hbsi.servlet.KickUserServlet
  


  
    LoginServlet
    /LoginServlet
  
  
    KickUserServlet
    /KickUserServlet
  
  
    index.jsp
  


项目所导的包------------

你可能感兴趣的:(例题)