Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

which servlet will run first?  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In HFS&J, it is mentioned again and again that:


Why??

(In fact if we are using MVC then it is very obvious that a partular servlet will always run first for any request...)

Please help.

Thanks.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rathi,

If you're going to keep posting quotes and asking us why, you should also include the context in which it is written. Was that sentence a part of a discussion about server startup or about responding to requests?

Assume that nobody else owns the book that you are reading.
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please atleast give the REAL quote so that we can google it.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Rathi,

If you're going to keep posting quotes and asking us why, you should also include the context in which it is written. Was that sentence a part of a discussion about server startup or about responding to requests?

Assume that nobody else owns the book that you are reading.


I am sorry Ben,

The context is, context init parameter (or any other) can only be string. But if we want to have an object that is available to all parts of my web application then what we can do? because it is not possible to have object in context init parameter.

So one option is, get the string from context parameter then create object from this string (assume it is possible) then store it into context attribute, now it is available to whole application. But the issue is which servlet should do this because you can't guarantee which servlet will always run first.....

So they have introduced listener (I haven't read this yet)

Now I think my question is clear...

Please answer.
Thanks.
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is that quote in context...
Context parameters can't be anything except Strings... What is you really want all the parts of your web app to have acces to a shared database connection. You can certainly put the DataSource lookup name in a context init parameter, and that's probably the most common use of context parameters today.

But then who does the work of turning the String parameter into an actual DataSource reference that all parts of the web app can share?

You can't really put that code in a servlet, because which servlet would you choose to be The One To Lookup The DataSource And Store It In An Attribute? Do you really want to try to guarantee that one servlet in particular will always run first?
This is a lead in to Listeners, which will make sense if you keep reading.

It is nearly impossible to guarantee that one particular servlet runs first. You mentioned MVC, but unless you have a front end controller and absolutely no other servlet mappings, you can easily bypass the initialization code by simply typing in a different url. You could also just go directly to a JSP, thus avoiding the servlet. Even the load-on-startup option has its draw backs, as Ben explained in another post.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!