jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes Need help with Servlet style/design pattern choice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Need help with Servlet style/design pattern choice" Watch "Need help with Servlet style/design pattern choice" New topic
Author

Need help with Servlet style/design pattern choice

andrew ennamorato
Ranch Hand

Joined: Oct 03, 2007
Posts: 100
Writing a web app that is mainly for internal/testing use, but possibility in being used on a larger scale.

The goal is to use AJAX-y techniques in a web page/gui. The app will be talking to a set of web services running on a separate server. Servlet(s) will be used to talk to the services, the javascript will talk to the servlet(s).

The question is, how to 'arrange' these servlet(s)? Should it be one servlet that uses a set of utility classes to contact the services? The services are running on WebLogic and WebLogic provides a java proxy jar that I'll be using.
I essentially need 3 or 4 capabilities - checkUsername, queryForData, orderDataViaHTML, orderDataViaFTP.

I'm still fairly new to Servlets, so maybe I need something like Servlet Listeners or Filters.
What I'd like to be able to do is just call one servlet from the front end, but maybe it's better if there are several servlets - one for checking username/password, one for querying for data, etc.

Know I'm rambling here...but any thoughts (or questions)?
[ September 23, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

You can have separate servlets for various tasks, or you can use a front controller. I rather prefer the latter.

Authentication checking is best handled by a filter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
andrew ennamorato
Ranch Hand

Joined: Oct 03, 2007
Posts: 100
Thanks. I'll need to do some more reading/research on filters, but IIRC, they essentially 'capture' an incoming request or outgoing response.
If this is the case, I'll need to pass in credentials (username/pass) on each servlet call?

The web services is asynchronous and doesn't require credentials for each call - that depends upon which DB (or data library) you want to query.
i.e.
callWebService(credentials, dataLibrary, queryParameters) is valid.
callwebService(null, dataLibrary, queryParameters) is also valid.
I guess I just make the Filter smart enough to decide whether to accept or reject the request (sort of talking out loud here).

I currently have it where the servlet does all the error checking (i.e. if request.getParam("credentials") != null), but Filters sound like a better way to handle that. Thankfully, I don't have to handle sessions or a true 'login/logout' type feature.

OK, thanks for the thoughts. Research time...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Generally, once logged in, credentials are stored in the session. The filter checks that the credentials are there for requests that require authentication.

The filter is not used to log in, but to check of someone has logged in.
[ September 23, 2008: Message edited by: Bear Bibeault ]
andrew ennamorato
Ranch Hand

Joined: Oct 03, 2007
Posts: 100
Sounds like I can just use the Filter(s) to check if parameters are there for the service methods I'm going to call?
i.e. callWebService(username, password, datastore) - Filter checks username/password, then just passes datastore to Servlet.
callWebService(username, null, datastore) - Filter returns an error message/exception due to empty password field.

If so that will help break out some of the error checking I'm already doing, which would clean things up a bit. I guess I'll still have to chain together the methods/services I want to call in the servlet.
i.e.


That was kind of my question, whether to break out the individual services into separate servlets, or to use one servlet to process them all. I need to refresh my memory re: a controller.

Thanks again...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Servlet.callWebServiceA()
I'm not sure what you mean by this. You don't explicitly call methods on a servlet.

That was kind of my question, whether to break out the individual services into separate servlets, or to use one servlet to process them all. I need to refresh my memory re: a controller.
It's rarely a good idea to gang up disparate operations into a single element.
andrew ennamorato
Ranch Hand

Joined: Oct 03, 2007
Posts: 100
Originally posted by Bear Bibeault:
I'm not sure what you mean by this. You don't explicitly call methods on a servlet.


Was just trying to shorten the code.
Essentially, in the processRequest() portion of the servlet, should I be making a call to a 'chain' of service methods, use separate servlets as a wrapper around each of the service methods, etc. As I said, kind of just writing this out loud.



It's rarely a good idea to gang up disparate operations into a single element.


Yup. If this were just a standalone app, I'd probably do something with a set of utility classes.


Something along those lines. I'm wondering how this translates when using Servlets, which are prompted from Javascript.



That is basically what it looks like now (or will soon), but is there a better way to handle that? Sounds like I can at least use a Filter to handle the credential checking, but should I follow a similar technique of utility classes w/in the servlet?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Servlet style/design pattern choice