开发者

Single Global Variables for multiple client requests using JSP

开发者 https://www.devze.com 2023-03-28 07:19 出处:网络
I am developing a custom search application. The query input by the user is to be pre-processed to remove certain query terms. The criteria for removal is based on a score that has been pre-computed f

I am developing a custom search application. The query input by the user is to be pre-processed to remove certain query terms. The criteria for removal is based on a score that has been pre-computed for all the terms in the corpus. I am thinking of storing the terms and their scores in a hash and when the user inputs a query, I can get the scores of the terms from the hash and remove the terms depending upon the criteria.

However, since the hash is very big, is it possible to have it as a global variable such that for multiple client requests only a single copy of the variable is in the memory? I guess that declaring it as a global variable in index.jsp will not work because for every client request a separate copy w开发者_开发百科ill be created in the memory.


You could store it in the application scope.

application.setAttribute("someName", someObject);

You can get it again by getAttribute().


Note that this job is normally to be done by a ServletContextListener and/or a HttpServlet, not inside a JSP. Writing Java code in JSPs is considered poor practice.


You can also use Singleton pattern. It will initiate only once.

0

精彩评论

暂无评论...
验证码 换一张
取 消