implementing HttpSessionListener

老子叫甜甜 提交于 2019-12-24 03:27:56

问题


I am facing a similar problem and have learnt that my listener class will be instantiated when the web.xml is read. I have few functions in the listener class that are used by the main servlet's doPost(which interacts with the database(mysql) to read/write data regarding the users.)along with the methods of HttpSessionListener. So what prevents the container from loading the listener class. I am getting the 404 error.

snippet:

public class EntryServlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {


    PlayersList playa = new PlayersList(); //listener class

    if(!playa.isExisting(params[3])){
        if(params[1].equals("Register")){           
            playa.addPlayer(params);
        }
    }       
    else
        out.println("Username already exists");


    if(playa.isExisting(params[argUName],params[argUPwd])){
        HttpSession session = request.getSession(true);     
        session.setMaxInactiveInterval(600);            
        HashMap playersMap = playa.getActivePlayers();
        //code . .
    }
    else
        out.println("Couldn't locate player Name!");    
        out.println(playa.getIn());         
}

Listener class:

public class PlayersList implements HttpSessionListener{
private Connection cn;
private Statement st;

PlayersList(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        cn =  DriverManager.getConnection("jdbc:mysql://localhost/homeDbse\'","root","");
        st = cn.createStatement();
    }
    catch(ClassNotFoundException ce){}
    catch(SQLException se){}
    catch(Exception e){}
}   

public boolean isExisting(String player){
    //interaction with dbse
}   

public void addPlayer(String[] args){
    //interaction with dbse

}   
public void sessionCreated(HttpSessionEvent se){
    //session managing
}   
public void sessionDestroyed(HttpSessionEvent se){
    //session managing
}

回答1:


This approach is completely wrong. You should be doing it from the other side on. Create a Player object which implements HttpSessionBindingListener and use it instead.

public class Player implements HttpSessionBindingListener {

    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        // Add player to list.
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        // Remove player from list.
    }

    // ...

}

The valueBound() will be triggered when you do

Player player = new Player(name);
request.getSession().setAttribute("player", player);

The valueUnbound() will be triggered when the session has expired or when you remove the attribute.


Unrelated to the concrete problem, you have serious JDBC problems in your code. This application will crash whenever the DB times out the connection because it's been open for too long. You should open and close the connection, statement and resultset in the shortest possible scope. I.e., close them in the finally block of the very same try block where the SQL query is been executed.



来源:https://stackoverflow.com/questions/5259764/implementing-httpsessionlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!