aspose file tools*
The moose likes Servlets and the fly likes Top 10 Servlet/JSP misconceptions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Top 10 Servlet/JSP misconceptions." Watch "Top 10 Servlet/JSP misconceptions." New topic
Author

Top 10 Servlet/JSP misconceptions.

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It seems that there are certain categories of questions that get asked over and over in both the Servlets and JSP forums.

Many of these questions can be traced back to a fundamental misunderstanding about the way Servlet/JSPs function or a misunderstanding about how web-apps and HTTP in general function. Getting a handle on one of these rudimentary concepts can often lead to really big "Ah Ha" moments that makes all kinds of other concepts self-evident.

Instead of just waiting for the questions to come in and answering them one at a time, it might be useful to start a dialog that identifies these issues and tries to trace the origins of the misconceptions. It may also be helpful to have a thread to point people to when these questions come in.

I'm starting this in the Servlets forum because JSP is a subset of Servlet technology (although the jsp forum might have been a better place since I see more of these types of questions there).


To get started, I'll list 3 items:
[Moved into a new post]

====================================================================

As they come in, I'll add them here. I'll try to keep them in the order of importance. If you're reading this thread for the first time, look at all the subsequent posts for explanations.

  • The relationship between client and server and what gets executed on each.
  • That there is one Servlet instance serving multiple threads.
  • That sessions are not meant to be shared across contexts.
  • That servlets/JSPs are meant to respond to requests, not act as timers or initializers.
  • That HTTP is a stateless protocol (This could be the grand-daddy of them all! ).
  • That a session is not a container.
  • That J2EE can be learned without first learning Java
  • That the frameworks (Struts, Tapestry, etc..) are replacements for JSP and Servlets. (More importantly, that one can skip learning Servlets and start out learning from within one of the frameworks).
  • That the Servlet Lifecycle is managed by the container.
  • That the Servlet Spec has no build in capablility to manage multipart posts.
  • That Servlets and JSPs were designed primarily to return text..


  • [ February 12, 2005: Message edited by: Ben Souther ]

    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
    Jeroen Wenting
    Ranch Hand

    Joined: Oct 12, 2000
    Posts: 5093
    another one (again mainly concerned with JSP):
    The idea that Struts is a Sun standard and somehow a replacement for servlets and JSP.

    Or this one:
    The misconception that Servlets and JSPs are not part of J2EE at all.


    42
    Dan Novik
    Ranch Hand

    Joined: Jan 26, 2005
    Posts: 39
    >client machine from a servlet app or in questions about "back button"
    >or "refresh button" issues and why server side Java doesn't have specific >methods for handling them.
    At least this stuff (refresh, double posting) is addressed in web frameworks based on JSP and servlets (tokens in Struts, redirection in JSF)
    David O'Meara
    Rancher

    Joined: Mar 06, 2001
    Posts: 13459

    A little more generic than 'Servlets and JSPs', but there is a common misconception that web applications should run and interact like desktop applications.
    Adeel Ansari
    Ranch Hand

    Joined: Aug 15, 2004
    Posts: 2874
    One of the bigest problem is,

    Folks would like to start from the forum without prior knowledge. Sometimes I feel like they really dont want to do on their own at all. Please dont mind folks.

    We have many stuff for the begginers over the net, just need some googling around, and at suns site. They dont even try to read the docs and specs sometimes.

    Sometimes they just mess up javascript with scriptlets. And I can remember a topic it was "difference between JSP and struts", as Jeroen mentioned earlier.
    [ February 03, 2005: Message edited by: Adeel Ansari ]
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by David O'Meara:
    A little more generic than 'Servlets and JSPs', but there is a common misconception that web applications should run and interact like desktop applications.


    This sounds like an extension of my 3rd point.
    I think this stems from "not understanding" that all the client side functionality is written on the server and passed down to the client for interpretation. Once it reaches the browser, the serverside code no longer exists.
    The questions that arise from this misunderstanding usually run along the lines of:
  • Can I build tabs with JSP?
  • Do JSP/Servlets come with a tree control?
  • Can I access the user's Outlook messages from a JSP?

  • Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by Dan Novik:
    >client machine from a servlet app or in questions about "back button"
    >or "refresh button" issues and why server side Java doesn't have specific >methods for handling them.
    At least this stuff (refresh, double posting) is addressed in web frameworks based on JSP and servlets (tokens in Struts, redirection in JSF)


    Hmm... MS .NET also has server side objects that write both the HTML and the Javascript functions for the developer.
    My guess is that this will only feed the misconception that I stated in my third point.

    That one sounds like a good candidate for #1.
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by Adeel Ansari:
    One of the bigest problem is,

    Folks would like to start from the forum without prior knowledge. Sometimes I feel like they really dont want to do on their own at all. Please dont mind folks.

    We have many stuff for the begginers over the net, just need some googling around, and at suns site. They dont even try to read the docs and specs sometimes.

    Sometimes they just mess up javascript with scriptlets. And I can remember a topic it was "difference between JSP and struts", as Jeroen mentioned earlier.
    [ February 03, 2005: Message edited by: Adeel Ansari ]


    I don't disagree with you. A lot of people seem to make 'asking for help' the first course of action.

    I don't, however, think that really fits in with Servlet/JSP/WebProgramming misconceptions. Maybe a learning misconception...
    I was hoping we could put together a really informative 'Top 10' (or even 'Top 5') list of gotchas that would help a new web programmer avoid some of the pitfalls that we've all had to dig ourselves out of at one point or another.
    Adeel Ansari
    Ranch Hand

    Joined: Aug 15, 2004
    Posts: 2874
    Originally posted by Ben Souther:

    I don't disagree with you. A lot of people seem to make 'asking for help' the first course of action.

    I don't, however, think that really fits in with Servlet/JSP/WebProgramming misconceptions. Maybe a learning misconception...
    I was hoping we could put together a really informative 'Top 10' (or even 'Top 5') list of gotchas that would help a new web programmer avoid some of the pitfalls that we've all had to dig ourselves out of at one point or another.


    Yeah right. I just elaborated a main cause then in my last paragraph I mentioned one of the example of misconception.

    My main purpose was to address following problem.

    Would they really consider that information list, we are talking about? Because some of them want a quick solution, better say answer, without any hardwork.


    But now I think that by doing this we would be able to forward them to that link atleast.

    agreed with you Ben.

    thanks.
    [ February 03, 2005: Message edited by: Adeel Ansari ]
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Is there a common root cause for all these misconceptions? Overly complex architecture? Unclear doc? J2EE spec design fails to separate concerns?

    Is there anything we can do about it? Use the FAQ? Write a book?


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    Adeel Ansari
    Ranch Hand

    Joined: Aug 15, 2004
    Posts: 2874
    Originally posted by Stan James:
    Is there a common root cause for all these misconceptions? Overly complex architecture? Unclear doc? J2EE spec design fails to separate concerns?

    Is there anything we can do about it? Use the FAQ? Write a book?


    I dont think so. Some of the books i found very simple, straight forward, clear, helpful but require reading indeed.

    can say, servlet specification docs is complex.

    Oh! a new book in the market means one more reading material, the minimum would be 150 pages. FAQ would be better instead.
    steve souza
    Ranch Hand

    Joined: Jun 26, 2002
    Posts: 852
    This has been mentioned before, but is good advice to anyone that uses a forum

    1) Use google to find your answer before posting. Any question you can ask has probably been asked before.

    2) Search the forum before posting to it. Many questions have been asked over and over.

    3) And finally a servlet/jsp specific one. When coding servlets you must understand thread safety, and that a servlet instance variable can be shared by any process executing the servlet. Bits can hit the ceiling if you don't realize this one.

    4) Quit using property files in web apps and start using the more standard j2ee methods such as Environment variables.


    http://www.jamonapi.com/ - a fast, free open source performance tuning api.
    JavaRanch Performance FAQ
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by steve souza:

    And finally a servlet/jsp specific one. When coding servlets you must understand thread safety, and that a servlet instance variable can be shared by any process executing the servlet. Bits can hit the ceiling if you don't realize this one.


    This is an excellent one and definately belongs on the list.
    Not understanding that J2EE containers (for all practical purposes) keep one instance of a servlet which handles a new thread for each request is a major issue with which new JSP/Servlet developers have to grapple.


    I assume you meant init params and not environment variables in your fourth point.
    [ February 05, 2005: Message edited by: Ben Souther ]
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by Stan James:
    Is there a common root cause for all these misconceptions? Overly complex architecture? Unclear doc? J2EE spec design fails to separate concerns?
    Is there anything we can do about it? Use the FAQ? Write a book?


    ...Enumerate them in a forum and discuss them out in the open, in a place where beginners often come for help? ;-)

    I don't think there is a single cause but I think each misconception has some reasons. The threading issue (One Servlet/multiple threads) seems pretty obvious to me. Servlets and JSPs work differently than all the other platforms. Add to that, the fact that, the syntax for JSP looks very similar to ASP and PHP and it's easy to tell why someone would not see the problem with creating an instance variable in a servlet or JSP. They've been writing code this way (safely) for years in another language.

    What isn't obvious to me is why new developers so often jump right into complicated architectures involving multiple contexts that require shared sessions before learning the basic concepts of Servlet/JSP programming.
    [ February 11, 2005: Message edited by: Ben Souther ]
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    That a session object is not really a container.

    Questions such as: "How many objects can a session hold", or "How can I tell the size of my session" or "is there a size limit on my session" give this misconception away.

    A session holds nothing more than references to objects that live on the heap.
    Gregg Bolinger
    GenRocket Founder
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15292
        
        6

    This is a pretty interesting list. I think that as this list grows it would be a good list to put in our JavaEnterpriseEditionFaq on our wiki pages. This board doesn't have sticky threads so once this thread gets buried, no one will see it.

    Another common mistake, which has been eluded to but not mentioned specifically, is that you can learn J2EE without learning J2SE. New programmers think they are 2 unrelated API's which is not the case. J2EE is more of an extension on top of J2SE that allows web development. If someone thinks they can learn J2EE without knowing Java basics first, they are highly mistaken.


    GenRocket - Experts at Building Test Data
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by Gregg Bolinger:
    This is a pretty interesting list. I think that as this list grows it would be a good list to put in our JavaEnterpriseEditionFaq on our wiki pages. This board doesn't have sticky threads so once this thread gets buried, no one will see it.

    If it reaches 10 items, I'll put it up there.

    Originally posted by Gregg Bolinger:
    Another common mistake, which has been eluded to but not mentioned specifically, is that you can learn J2EE without learning J2SE. New programmers think they are 2 unrelated API's which is not the case. J2EE is more of an extension on top of J2SE that allows web development. If someone thinks they can learn J2EE without knowing Java basics first, they are highly mistaken.


    It's probably safe to say that nearly all the other misconceptions are born out of this one. Most victums are probably ASP or PHP converts. The fact that JSP looks and behaves so much like those scripting languages is a double edged sword.
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    [These points were originally in the opening post.]

    [list][*] The notion that a servlet should do something other than respond to requests.

    A lot of people write to this site looking for help in trying to get a servlet to do something like run as a timer, spawn long running threads at startup, or push data to a browser when an event other than a request from a client it triggered. There are some good reasons for this misconception. First, before context listeners (pre servlet 2.3), using load-on-startup in a servlet declaration was the only way to trigger context initialization code. Second is the, non-spec compliant "<run-at>" attribute in the Resin application server. I like the idea of a container managed timer but I wish they had not implemented it as a servlet.


    [*] The idea that sessions should work across contexts.

    I'm not sure why this is but a lot of people who are just starting out writing J2EE apps try to build apps with multiple contexts that need to communicate with one another. They are then frustrated when a session started in one app doesn't carry over to another one.

    I actually think this is a syptom of another, more broad, misunderstaning but I'm not sure what. An enterprize app can (ear file) can have multiple contexts but most of these types of posts don't seem to be coming from people with an EJB type of background.

    I'd love to see a post from somewhere who's 'been there, done that'.

    [*] A general lack of understanding as to what code get run on the server and what gets run on the client.

    This isn't so much a Servlet/JSP issue as it is a web programming issue but I've noticed questions like "Can I pass a bean from one frame to another?" or "Can I call forward() from a javascript event?". The idea that your servlet/jsp code, which is executed on the server, writes the javascript code that is then executed on the client machine seems to trip some people up. Evidence of this misconception often shows up in questions about working with Excel files on the client machine from a servlet app or in questions about "back button" or "refresh button" issues and why server side Java doesn't have specific methods for handling them.
    [ February 12, 2005: Message edited by: Ben Souther ]
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by Jeroen Wenting:
    another one (again mainly concerned with JSP):
    The idea that Struts is a Sun standard and somehow a replacement for servlets and JSP.


    I don't spend enough time in the frameworks forum to see this but I can certainly see it being an issue. I definitely wouldn't want to try to learn servlets/JSP from within one of the frameworks.
    Gregg Bolinger
    GenRocket Founder
    Ranch Hand

    Joined: Jul 11, 2001
    Posts: 15292
        
        6

    Originally posted by Ben Souther:


    I don't spend enough time in the frameworks forum to see this but I can certainly see it being an issue. I definitely wouldn't want to try to learn servlets/JSP from within one of the frameworks.


    Yeah, these misconceptions are really out of the scope of this discussion, as are points on how to ask questions correctly. But to back up Jeroen's point a bit, a *lot* of people come in and try to learn J2EE using a framework like Strus or Spring. This is a huge mistake and one that I *kind of* made. I had the basics of JSP's and Servlets down and then jumped into struts. Bad idea, for me anyway. I later went back to JSP/Servlets and got a firm grasp and then when I went back to Struts everything clicked.

    However, I've since abandonded struts, but that is a different thread and one that has been discussed to death by more people than just me.
    David O'Meara
    Rancher

    Joined: Mar 06, 2001
    Posts: 13459

    Originally posted by Ben Souther:
    If it reaches 10 items, I'll put it up there.


    No reason to define 10 as the limit, just go as far as you need

    Two more (may already be included, I didn't look too carefully)

    * How to limit/count the number of Servelt instances
    * Why use init(), why not use the constructor?
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Originally posted by David O'Meara:

    No reason to define 10 as the limit, just go as far as you need
    Two more (may already be included, I didn't look too carefully)
    * How to limit/count the number of Servelt instances
    * Why use init(), why not use the constructor?


    David,
    I think both of those questions would be the result of a fundemental misunderstanding rather than the cause.

    The first would be typical from someone who doesn't understand the 'One Servlet, multiple threads' concept.

    I think you're on to something with the second. That one would be the result of a lack of understanding about the Servlet Lifecycle and about the concept of container managed resources.

    Thanks

    PS: I don't see the need to stop at 10 either but I'm starting to suspect that the actual number of fundemental misconceptions may be much less than ten.
    [ February 12, 2005: Message edited by: Ben Souther ]
    David O'Meara
    Rancher

    Joined: Mar 06, 2001
    Posts: 13459

    Yeah, I'm dumping more 'FAQs' than misconceptions, but I figure it's better to have them listed
    David O'Meara
    Rancher

    Joined: Mar 06, 2001
    Posts: 13459

    Misconception: how does a Servlet handle file-uploads/multipart requests?
    Misconception: Servlets serve/prefer/'built for' text and not binary data.
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    I really like the first. I've seen a lot of posts about multipart posts not working.

    Do you think the second is true about servlets? Maybe HttpServlets, definitately with JSP...
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Misconception: Servlets serve/prefer/'built for' text and not binary data.
    Do you think the second is true about servlets? Maybe HttpServlets, definitately with JSP...

    Come to think of it, it is a lot of work just to stream an image.

    Except for people trying to stream images from JSPs (which has been happening a lot lately), can you give me any questions that result from this as a fundemental misconception?
    Adeel Ansari
    Ranch Hand

    Joined: Aug 15, 2004
    Posts: 2874
    I have one more idea. We could start a new thread like this on "Servet/JSP best practices". I think this should be done too.

    Or we can include best practices in this thread also, because it is not reaching our threshold, as Ben said for atleast 10.

    Thanks.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Top 10 Servlet/JSP misconceptions.
     
    Similar Threads
    Servlet Life Cycle Doubts
    Dynamic web page
    Sessions from multiple apps on single server...
    Learning Struts before Servlets and JSP (follow-on to Struts Survey post)
    how to restrict the user to see the authorised page