Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Why does JSP's code run inside jsp_Service() and not in either doGet() or doPost() like Servlets do?

 
Stefan Jankovic
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After learning Servlets, I jumped straight to JSPs. One thing I learnt from Servlets is that whether request is GET or POST, Container would run either Servlet's doGet() or doPost() method. Technically, they are invoked by Servlet's service() method.

Then I came to JSP and saw jsp_Service() that executes entire JSP's code. Why is that? Meaning why are they behaving differently from that aspect?

To sum, if I said everything correctly, why does Container run Servlet's doGet() or doPost(), while it runs everything from JSP?
 
Tim Moores
Saloon Keeper
Posts: 6928
164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand the question. The container runs ALL the code it needs to run, regardless of whether it is a servlet or a JSP. Some of it is the developer's code, some is code the JSP engine added, and some is code that's part of the container. Given that servlets and JSPs take different approaches to executable content, it should be no surprise that they work differently.

jsp_Service() ... executes entire JSP's code.


No - there are also the jspInit() and jspDestroy() methods (and some scaffolding) which can actually be overwritten, although that'd be bad design.
 
Tim Holloway
Saloon Keeper
Posts: 23540
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSP's are primarily intended as View Templates (if you're using the Model/View/Controller paradigm). Or, in other words, they should be mostly HTML with just enough dynamic content to do what's necessary.

They're designed to operate as linear code and they don't usually concern themselves with whether they were invoked by a GET, a POST, or something else. If your main purpose in life is to render the values found in JavaBeans in an aesthetically-pleasing setting, it doesn't matter,

More to the point. If you're worried about internal operations of JSPs you're almost certainly trying to do something that would better be done via an actual servlet.

It's a common mistake made by novices to do lots of work in JSPs using scriptlets. Scriplets these days are pretty useless, since JSTL can handle the simple stuff and the complex stuff is  :censored: to debug. And to edit, since smart text editors have to switch between JSP/HTML editing mode and Java mode every time you move the cursor in and out of a scriptlet.

Plus if you use scriptlets, Bear will growl at you.
 
Bear Bibeault
Sheriff
Posts: 67528
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Plus if you use scriptlets, Bear will growl at you.



/sharpens-claws
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic