Accessing ServletContext outside of a servlet

前端 未结 4 1215
长发绾君心
长发绾君心 2021-01-03 05:26

I wonder if anyone can advise on a Java webapp question?

I have a set standard Java class which has methods to set and get values of a property file. These methods a

4条回答
  •  Happy的楠姐
    2021-01-03 05:34

    The ServletContext via a ServletContextListener, as someone stated above, can be used to load static variables in a configuration class. It can also be triggered to reload those variables if needed. Also, you simply need access to the HttpServletRequest object to get to the context. If you pass the request to an intermediary object you can get to the context easy enough.

    ServletContext application = req.getSession().getServletContext();
    

    Then you can access the data by putting it into a temp variable if needed which can then be passed to other functions and eventually release it's resources when it goes out of scope.

提交回复
热议问题