Bypassing Servlets - What are the implications involved?

20 views
Skip to first unread message

Gerrit

unread,
Aug 20, 2013, 2:41:09 AM8/20/13
to ctjug...@googlegroups.com
Hi

I recently wrote a couple of annotations which bypasses servlets. I try and stay away from frameworks like spring and faces etc due to  their limitations and overheads.

The annotations which I have implemented basically bypasses servlets and just uses plain java objects which are delegated to by a filter to process the requests and responses and returns either some plain text, json or the request is forwarded by a request dispatcher.
To achieve this I had to skip chaining in my filter.

Does anyone know what/if there are any risks involved in doing this? I know that web and app servers currently pool their servlets, but is  is there anything else which could pose a problem?

Dion Dodgen

unread,
Aug 20, 2013, 3:04:42 AM8/20/13
to ctjug...@googlegroups.com
Hi Gerrit,

Just curious:
What kind of overheads hand limitations are you concerned about ?
Why use an app server if you want to bypass its functionality ?


--
--
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To post to this group, send email to CTJUG...@googlegroups.com
To unsubscribe from this group, send email to CTJUG-Forum...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
For jobs see http://jobs.gamatam.com/
---
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ctjug-forum...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Kind Regards,
Dion

Andrew van der Westhuizen

unread,
Aug 20, 2013, 3:05:40 AM8/20/13
to ctjug...@googlegroups.com
Off the top of my head, if you end up using any library, or tech (e.g. CAS), that makes use of filters- Then unless your filter is the last in the chain, I would foresee problems there. 

Warm regards,

Andrew van der Westhuizen (B.Sc, MBA)
Senior Analyst Programmer, Stellenbosch University


--

Gerrit Brink

unread,
Aug 20, 2013, 3:33:18 AM8/20/13
to ctjug...@googlegroups.com

I have used Struts and Faces in the past and had some terrible experiences when I had to achieve something outside the scope it was intended for. This lead to some hackish code and I ended up dropping the frameworks. Also I do not want to include Spring so I can use one of its features.

 

App servers have other functionality I would like to use if necessary. I do a lot of small prototyping projects and I would like to keep my code as portable as possible, web and app server compatible.

Yusuf Jakoet

unread,
Aug 20, 2013, 4:10:58 AM8/20/13
to ctjug...@googlegroups.com
>> I try and stay away from frameworks like spring and faces etc due to  their limitations and overheads.
I feel that by doing what you're intending, you'll be creating a limitation that will be realized at the worst possible time.

Also, about the "hackish code" bit... All systems have them, it's a reality.  There is no super framework that solves all problems.
However if a framework does handle 90% of the cases well, why not use it and accept the workarounds?  Or are you saying that your apps are written such that they fall in the 10% of cases not handled by the frameworks?  If so, maybe Java isn't the best language to be using... Have you considered something else (or is it a must)?

As to your original question the only risk I see is that you have to re-implement functionality that you'd get for free (well not exactly,
but you get where I'm going) with a web server.

Gerrit Brink

unread,
Aug 20, 2013, 4:32:17 AM8/20/13
to ctjug...@googlegroups.com

Can we please get back to the original question? I am currently not looking for a framework, because my original question is a single part of a much larger project I am dabbling around with. The end result would be my own framework.

 

 

From: ctjug...@googlegroups.com [mailto:ctjug...@googlegroups.com] On Behalf Of Yusuf Jakoet
Sent: 20 August 2013 10:11 AM
To: ctjug...@googlegroups.com
Subject: Re: [CTJUG Forum] Bypassing Servlets - What are the implications involved?

 

>> I try and stay away from frameworks like spring and faces etc due to  their limitations and overheads.

--

--
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To post to this group, send email to

Yusuf Jakoet

unread,
Aug 20, 2013, 4:59:00 AM8/20/13
to ctjug...@googlegroups.com
No worries dude...  Perhaps make mention though of the fact that you'll be creating your own framework though!

The only way you going to find out what the implications are, is by browsing the source of the some of the open source servers.  Specifically, the part that bootstraps the functionality that it offers.  You'll find that many, if not all of those servers have very lightweight bootstrappers.  You may have to debug into the source of the server to understand exactly what you'll be missing as server implementations vary and you've stated that you want to be compatible.

>> App servers have other functionality I would like to use if necessary
Does any of that functionality fall within the servlet spec?


--
Yusuf

Moandji Ezana

unread,
Aug 20, 2013, 5:14:45 AM8/20/13
to ctjug...@googlegroups.com

You shouldn't have any issues.

Most web frameworks use a Filter to dispatch to custom controllers (Struts 2, Resteasy, off the top of my head), because it's more flexible than dispatching from a Servlet. I've written a web framework that does the same.

As long as you're returning a proper response, you're fine. It's generally easier if you force users to put your filter as the last one in the chain. So you're totally compatible with other filters, as long as you respect the chain.

You could even combine it with the GuiceFilter found in Guice-Servlet (or a CDI equivalent) to get DI for free. That's something I've done in the past.

--

Moandji Ezana

unread,
Aug 20, 2013, 5:19:50 AM8/20/13
to ctjug...@googlegroups.com

> To achieve this I had to skip chaining in my filter.

I'd missed this point. Could you elaborate on the reasons? A gzip filter, or even one that wraps the request or response (for example, I often use a filter that changes the http method based on a field in a form) shouldn't be a problem.

Gerrit Brink

unread,
Aug 21, 2013, 6:20:00 AM8/21/13
to ctjug...@googlegroups.com

Thanks for the feedback Moandji, I have set up a basic prototype doing the following:

Basic Example of implementation:

 

@Serv("/testserv")

public class TestMe {

               

                @AjaxJson("jsontest")

                public void getUsers(Rex rex){

                                rex.out("outcome", "success");

                }

               

                @AjaxText("texttest")

                public String getUsers(Rex rex){

                                return "Success";

                }

               

                @WMethod("testwmethod")

                public String getUsers(Rex rex){

                                rex.out("outcome", "success");

                                return "/index.jsp";

                }

}

 

The @Serv annotation basically contains the requests servletpath you would like to map the object to

 

The @AjaxJson annotation writes out all the Requests Attributes as a JSON Object via GSON (Thread safety confirmed)

The @AjaxText annotation writes the returned String to the Responses Out

The @WMethod annotation acts like a standard servlet returning the path to the jsp which a request dispatcher dispatches to

 

The values specified by all the web methods above are the attribute names in a request.

 

The Rex object passed to these methods is just a wrapper object containing the HttpServletrequest, HttpServletResponse and an EntityManager for conveniently getting objects from the database without filtering through request parameters (validation done on parameters first ofcourse).

The out method on the Rex object basically just calls the request.setAttribute so I could swop out the annotations and make basic changes if I require the methods to do something else.

 

The annotation mappings and their respective Classes and Methods are cached via a ServletContextListener at startup.

If a request mapping is not found in these custom mappings it just proceeds through the filter chain as per normal.

From: ctjug...@googlegroups.com [mailto:ctjug...@googlegroups.com] On Behalf Of Moandji Ezana
Sent: 20 August 2013 11:15 AM
To: ctjug...@googlegroups.com
Subject: Re: [CTJUG Forum] Bypassing Servlets - What are the implications involved?

 

You shouldn't have any issues.

Moandji Ezana

unread,
Aug 21, 2013, 6:27:42 AM8/21/13
to ctjug...@googlegroups.com

I'll avoid debating any of the design... ;).

Still, I'm curious to know what part of this required breaking the filter chain?

As an aside, I stopped writing my own web frameworks when JAX-RS 2 came out. It's surprisingly good for a Java web framework.

Gerrit Brink

unread,
Aug 21, 2013, 7:23:06 AM8/21/13
to ctjug...@googlegroups.com

I hope on converting some of this functionality to Rhino JS so I can create a front end where I can modify my “servlets” directly from there and then have it cached with it’s CompiledScript counterparts.

 

A system which allows for adjustments while its running kind of thing, also learning a lot as I go along.

Gerrit Brink

unread,
Aug 21, 2013, 2:00:36 PM8/21/13
to ctjug...@googlegroups.com
Oh and I am breaking the filter chain, because it is writing to the response. The filter handles the gson output and other types as indicated in previous "servlet" examples annotated methods. I do not have any static web resources or jsp's for the filter chain to finalize on. If you forward the chain after writing to the response with no static resources an IllegalStateException is triggered. I could wrap this in a try/catch, but it would seem like a messier approach. The oracle docs indicates that a filter can block climbing the chain, but then it is responsible for filling out the response.

Moandji Ezana

unread,
Aug 21, 2013, 6:24:14 PM8/21/13
to ctjug...@googlegroups.com

On Wed, Aug 21, 2013 at 8:00 PM, Gerrit Brink <gerritb...@gmail.com> wrote:
Oh and I am breaking the filter chain, because it is writing to the response.

Turns out you're right. In both my web framework and in Resteasy, the chain is broken if a matching route is found. Otherwise, FilterChain#doFilter is called, so that static or non-framework-managed resources can be called.

I'd totally forgotten about that!

Andrew van der Westhuizen

unread,
Aug 22, 2013, 2:05:46 AM8/22/13
to ctjug...@googlegroups.com
Can you not call doFilter first and put the code after the call? Effectively making it a post chain operation.

Warm regards,

Andrew van der Westhuizen (B.Sc, MBA)
Senior Analyst Programmer, Stellenbosch University


--

Gerrit Brink

unread,
Aug 22, 2013, 2:24:10 AM8/22/13
to ctjug...@googlegroups.com

Continuing the chain at that point would cause an exception to be triggered if it eventually finds no static web resources. I do not believe it will serve much of a purpose, because at that point a response would have already been generated.

 

I have also run some tests to compare the access speeds and this method seems to be on par with a standard servlets execution and lookup.

Please note however my tests only contained a few web methods and larger applications would contain many more.

 

From: ctjug...@googlegroups.com [mailto:ctjug...@googlegroups.com] On Behalf Of Andrew van der Westhuizen
Sent: 22 August 2013 08:06 AM
To: ctjug...@googlegroups.com
Subject: Re: [CTJUG Forum] Bypassing Servlets - What are the implications involved?

 

Can you not call doFilter first and put the code after the call? Effectively making it a post chain operation.

David Tinker

unread,
Aug 24, 2013, 6:31:42 AM8/24/13
to ctjug...@googlegroups.com
I used http://http://www.simpleframework.org/ to build QDB
(http://qdb.io/) for performance reasons. Its an HTTP server framework
that runs faster than servlet based alternatives (Jetty, Tomcat et
al). You lose portability but gain performance. Great option for
something designed to run standalone and use little memory.

All the libraries I chose for QDB are listed here: http://qdb.io/license
> --
> --
> You received this message because you are subscribed to the Google Groups
> "CTJUG Forum" group.
> To post to this group, send email to CTJUG...@googlegroups.com
> To unsubscribe from this group, send email to
> CTJUG-Forum...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/CTJUG-Forum
> For the ctjug home page see http://www.ctjug.org.za
> For jobs see http://jobs.gamatam.com/
> ---
> You received this message because you are subscribed to the Google Groups
> "CTJUG Forum" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ctjug-forum...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
http://qdb.io/ Persistent Message Queues With Replay and #RabbitMQ Integration
Reply all
Reply to author
Forward
0 new messages