In PHP, you can always access the current request or response from any part of your code. This concept is fundamental to PHP programming. There is always a request for data, response data, session data (etc.) !
This does not happen in Java Servlets! To access HttpServletRequest, HttpServletResponse, HttpSession (etc.) In your code, you need to pass them as function variables. This means that you cannot encode a web framework that essentially “knows” about all these problems and eliminates the complexity of communicating them.
So, I developed this solution:
- Create anf by registering a ServletRequestListener.
- Upon request, an initialized event associates the current HttpServletRequest with a JNI context indicating the name of the current thread (Thread.currentThread (). GetName ());
- At the requestDestroyed event, untie the above JNI resource for cleanup.
This way you have access to the current request / response from anywhere in your code , as they are always present in the JNI context and can be obtained by providing the current stream name.
All known servlet containers implement a single-threaded model for each request , so there is no need to confuse the requests (of course, do not forget to clear them).
Also, the JNI resources of each web application are separated by default, so there are no problems mixing them or security problems that may arise from one web application that has access to the requests of others.
The pig is twisted, but pleasant and simple ...
What do you think?
Takis bouyouris
source share