How to build a raw non-blocking servlet - servlets

Is there over there a lot of examples about how to create "reactive" services using spring, or whichever microprofile implementation (quarkus, kuuluzee).
According to Servlet 3.1, whichever servlet can be handled as "non-blocking".
I figure out that is mandatory to perform this servlet into a non-blocking servlet container like netty...
First question:
Is it really as I writing? Do I need a non-blocking servlet container implementation in order to make really "reactive" services?
Second question:
It's not clear to me about how to create a basic raw "non-blocking" servlet. I mean, which would be the difference between a "normal" extending servlet from HttpServlet? How would this implementation look like?
Any references?

Related

Asynchronous Servlet processing in Spring Web Flow

Is there any way to make use of Java EE Asynchronous Web Servlet processing in Spring Web Flow? I see a fair amount about Spring MVC support, but nothing about Web Flow. Where ideally I'd want only certain transitions or actions to use the approach.
Would you have to use the DeferredResult approach? Although that seems to be intended for doing the work somewhere unaware of the web container context, which Web Flow isn't.
I can get the native HttpServletRequest, of course, and from there call:
AsyncContext acontext = startAsync();
But since Web Flow isn't dealing with low level HTTP request/response objects, I don't know how I'd tie the two together.
I'd love to see a working example. I'm hoping I don't have to exit Web Flow in order to do the Asynchronous request processing.

How to run two servlets parallely

I have two servlets (e.g. servlet1.java, servlet2.java). I want to run two servlets in parallel with one request. How can I achieve this?
I have some idea about multithreading concepts but I have no idea how to implement this.
Actually there is no way to do that. For a single HTTP request you cannot pass it to two servlets.
The request belongs to only one servlet.
In a Java EE application every servlet acts as a thread.
One HTTP request belongs to one servlet only. Maybe an HTTP response can be passed to some other servlet (servlet chaining).
Really not sure why you wold want to do this? What's your use case?
How would the response form each servlet be handled and returned back to the client?
If you need to spawn a thread during the servlet's doGet or doPost method, then I suggest taking a look at this answer
Depending on your usecase though it may be better to implement your thread using a servlet filter.
If you want a single thread to pass through two classes on server side, think of using Servlet filters

JSF context from Servlet

I have a JSF2 controller/bean/view that is invoked the standard JSF way. Now I have a need to access this logic from a legacy part of the app that knows servlets and URL params only.
I thought of creating a "loader.xhtm" view that bridges the gap between non-jsf requester and the jsf part of the app. Loader will take URL params and make the necessary JSF post to backing bean. etc. One downside of this approach is - it's one extra client/server hop, a programmatic redirect. However it is simple to implement.
But is there a more clever way? I found someone who created servlet filter and explicitly created a FacesContext, started the lifecycle and the ViewRoot. Conceptually I see what is being done, but I don't know how to put it into practice. Has anyone interfaced with JSF lifecycle directly from a servlet? Do you have a sample?
Another mention of this concept.

OSGI HttpService : securing all the servlets

I'm developing servlets and register them into my OSGI container thanks to HttpService.
My goal is to secure all the servlets registered in my OSGI container.
I saw that I can register my Servlet with an HttpContext with my own handleSecurity method implementation to process my security.
But I'm thinking to the case where a bundle registers a servlet with the default HttpContext (with implies no security).
So my question is, is there a way to force the security of all the servlets deployed in my OSGI container once for all?
I'm going to use the Service hook feature (OSGI 4.3) in order to override the behaviour of HttpService.registerServlet. In my hook I'll force the usage of my HttContext implementation.
With this solution, any bundle that register a servlet with the HttpService will be secured by my HttpContext implementation.
The short answer is No for using the HttpService.
The longer answer, you might achieve something like this if you use the whiteboard-extender which isn't available per OSGi spec yet, but felix and pax-web do provide it.
When using the whiteboard-extender you're able to register your servlet in combination with a reference to a HttpContext (as property). Of course this HttpContext would also need to be a "customized" one but you only need to register it once and are able to reference it from your Servlets.
This is probably the closest you get to your question.
If you use the Apache Felix whiteboard extender you can register a Servlet Filter, this is a much better way to handle security since it is easy to support different strategies. The intention is that Filters and whiteboard will be supported in the next update of the Http Service: https://github.com/osgi/design/tree/master/rfcs/rfc0189
You could use hooks as suggested, but please don't. Hooks were intended for deep middleware, not for application oriented aspects. They create start/stop ordering issues, they make the system more opaque for debugging tools, in short they make your system much more complex. If you start using hooks for these purposes you will find many more use cases and they will start to interact. Stay away from them except for very core system middleware.

How does a Struts2 action compare to a Servlet?

How do Struts2 actions compare to Servlets? Can an action act as a servlet?
A Struts (Struts1/Struts classic) action was more tied to a servlet. In Struts2, things are quite different. A Struts2 action is just a POJO (plain Java class), totally decoupled from the Servlet API. This decoupling eases testing.
In the typical workflow of a Struts2 webapp, an action will be instantiated for each request and will be associated with a Servlet (it can implement the ServletAware interface if it needs to be aware of this association; normally this is not necessary nor advisable).
An important conceptual difference with Servlets (and with Struts actions) is that Struts2 actions are not reused for different requests, and hence are thread safe: say, it can happen that three http requests (simultaneous or not) are served by one servlet instance; but inthat case we will still have three different Struts2 action instances, one for each request.
Struts is an abstraction layer on top of the vanilla java servlet stuff. Actions themselves are defined by the programmer and are invoked by struts frameworks when a URL is hit (you configure what url maps to which action). So they don't really "compare" to a servlet, they are an abstraction around the functionality the servlet provides. One typical thing you do with an action is output a jsp, which is equivalent to a servlet. so what happens is
a) request comes in, gets mapped to action
b) action loads some data
c) action renders a jsp, passing loaded data to the jsp.
An action can output directly to the request/response, if that is what you want, but in most cases is probably not good practice.
Struts2 is a MVC framework implementation based on Java EE technology.

Resources