• Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Instantiation

 
Dennis Hopfer
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I submit this may be the correct way to catch Servlet requests.

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if this is meant as a question, or what, exactly, you mean by "catch Servlet requests", but it is the wrong way to handle servlet requests. You never instantiate servlet classes - that is the job of the servlet container.
 
Dennis Hopfer
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naturally one wouldn't instantiate servlets directly but since only one instance of a servlet answers ALL requests for that servlet you can only use instance variables when they are shared and/or thread safe throughout the various HTTP requests.

This means you have to create ways to house data that should otherwise be part of the class variables. This can be quite involved.

My proposal would solve this problem, hence its being proposed. That I would have to create a new type of object to pass around my functions, rewriting existing functions just to mimic the function of having instance variables is absurd.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36032
432
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Hopfer wrote:My proposal would solve this problem, hence its being proposed. That I would have to create a new type of object to pass around my functions

Many frameworks do this. However they don't call the new object type a servlet. (That's confusing.) For example, Struts calls them actions and Spring calls them Controllers. The difference is that those frameworks also consider the new object type to be stateless so you'd have to create your own custom one for the action/controller to instantiate in order to have state.
 
Dennis Hopfer
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you a proponent of forcing people into frameworks in order to solve the simple problem of having instance data in a servlet? That sounds pretty silly to me.
 
Paul Clapham
Sheriff
Posts: 21980
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I may make a guess at what Dennis is talking about, I suggest that the problem is that his unnamed servlet would need some way for the doAuthentication() method and the doParameterVerification() method to communicate, if they were methods of the unnamed servlet. One way to do that would be through instance variables of the unnamed servlet, but as we all know that's a bad idea. Another way would be for those methods to communicate by receiving parameters from the doGet() method and returning results to it, but apparently that hasn't been considered. Or maybe it's more complicated than that. So the solution is to have another object which keeps track of temporary data while doAuthentication() and the other methods are being called. Which makes perfect sense to me. It isn't absurd at all. No "existing functions" would have to be rewritten, since you'd just be extending Object in the simplest case. What I (and the other responders, I think) consider absurd is to force that other object to be a servlet, even though its job isn't to handle an HTTP request. This idea does involve rewriting existing functions, or at least ignoring them.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36032
432
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Hopfer wrote:Are you a proponent of forcing people into frameworks in order to solve the simple problem of having instance data in a servlet? That sounds pretty silly to me.

No. I was saying how frameworks do it to show that the design pattern is common. There's nothing wrong with doing it yourself. Again, I would NOT name that new object a servlet though. It isn't one so that is a confusing name.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65678
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use scoped variables in place of instance variables, and place methods directly on the servlet if it's appropriate. Otherwise, as Jeanne points out, if you want to encapsulate the functionality in a new class, using a servlet isn't the way to do it.

It's also generally considered less than ideal to pass the request and response to other classes -- if the methods need servlet resources, I'd keep the methods in the servlet. If using another class, I'd pass the needed data to the methods rather than servlet resources. Keeps things more resilient and reusable.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Hopfer wrote:This means you have to create ways to house data that should otherwise be part of the class variables. This can be quite involved. ... My proposal would solve this problem, hence its being proposed. That I would have to create a new type of object to pass around my functions, rewriting existing functions just to mimic the function of having instance variables is absurd.


Are you a proponent of forcing people into frameworks in order to solve the simple problem of having instance data in a servlet? That sounds pretty silly to me.


These statements seem somewhat contradictory to me. Using a framework would obviate the need to do this, since you could avail yourself of the solutions the frameworks put in place. Since you don't seem to be in favor of that, your code would have to more of this on its own. My gut feeling is that if you're at the point where you think a more orderly approach is neeeded than tacking a few helper classes on the top of the Servlet API, investigating what frameworks can do for you might be a good step. In addition to the ones Jeanne mentioned, I recommend to check out Stripes, so you get a feeling of the various approaches out there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!