HttpSessionListener不起作用帮忙看下
listener:
public class UserListListener implements HttpSessionAttributeListener {
private List<String> userList;
public void attributeAdded(HttpSessionBindingEvent arg0) {
System.out.println("dadsad");
HttpSession session = arg0.getSession();
String username = (String)session.getAttribute("username");
System.out.println(username);
ServletContext application = arg0.getSession().getServletContext();
synchronized(application) {
userList = (List<String>) application.getAttribute("userList");
if(userList == null) {
userList = new ArrayList<String>();
}
userList.add(username);
}
}
public void attributeRemoved(HttpSessionBindingEvent arg0) {
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
}
这个是servlet:
public class UserLogin extends HttpServlet {
public UserLogin() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String username=request.getParameter("username");
HttpSession session = request.getSession();
session.setAttribute("username", username);
out.write("登陆成功");
}
public void init() throws ServletException {
}
}
在登陆后把用户名存入session中,然后把坚听器会坚听到的啊?可是System.out.println("dadsad");
根本没被打印..
web.xml:
<listener>
<listener-class>UserListListener</listener-class>
</listener>