SoFunction
Updated on 2025-05-20

The reasons and solutions for the failure of registration HttpSessionListener

Problem description

Listener

@WebListener
public class MyHttpSessionListener implements HttpSessionListener {
	/**
	  * session creation
	  */
	@Override
	public void sessionCreated(HttpSessionEvent e) {
		HttpSession session=();
		("sessioncreate===ID===="+());
	}
	/**
	  * session destruction
	  */
	@Override
	public void sessionDestroyed(HttpSessionEvent e) {
		HttpSession session=();
		("DestroyedsessionID===="+());
	}

}

Annotations have been added to the startup class@ServletComponentScan

Access interface

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String handle01(){
        return "Hello nihao";
    }
}

After writing this way, I found that the console will not print on the first visit:

("sessioncreate===ID===="+());

reason

When accessing the interface, the formal parameters must be brought with youHttpSession session.

as follows:

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String handle01(HttpSession session){
        return "Hello nihao";
    }
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.