JSP / Servlet Creation Question - Make request / response globally accessible via JNDI

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?

+3
source share
2 answers

I think that some web frameworks (GWT, Axis) already do this, but much easier: using ThreadLocal a static variable (or accessible from a singleton). Spring also has this feature.

I am not sure if it works with all containers. If a container uses a non-blocking IO and reuses the same stream to process multiple requests in parallel, it will no longer work.

See Get an HttpServletRequest object (request) from Java code for a similar question (and its answers).

+3
source

If you are worried that the different queries are confused (and then think about “sub-queries,” for example, the model window), would you rather use Seam ? They use an abstraction called "Session" to handle a lot of the things that we developers are trying to hack using other traditional web technology stacks. A seam built on JSF is exactly like fyi. You do not need to use EJB 3 or Hibernate, but it goes well with both of them. Something to think about.

0
source

All Articles