Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP becomes Servlet  RSS feed

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the use of letting some part of JSP code to become Servlet?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not 'some part', it's the whole JSP document that gets translated into a plain old Servlet.

Try to look into a translated JSP, and you will find out the reason.

It should be somewhere in Catalina/Work if you use Tomcat.
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I was thinking about JSP is just a way to display dynamic output generated by Servlets.
So why it is again translated to Servlet?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because if you think about it all a JSP does is what a Servlet does too. It processes a request. After all it is a Servlet that takes up the results of a previous servlet and continues on doing it's presentation. So when you write your servlet and forward to a JSP, eventually the forward is dispatched to the translated servlet.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would propose an alternate implementation that is superior?
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's like asking why Java source code is compiled into bytecode. It's because it is a reasonable thing to do.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the purpose of the two technologies: JSP is for creating the View (HTML page) of the application, while the Servlet is to provide application logic/control (via Java programming).

What would happen if you decided to use the Servlet to output the HTML code directly? Let's have a look:What happens when you want to change the look of your HTML page? Can you use an HTML editor to make changes? Not easily.

The JSP page looks a lot more like regular HTML. You can even use your favorite editor to create/edit the pages. However, when the page is actually used by the application, it is processed into a Servlet (which looks a lot like what we showed above). This give the JSP page access to things like the session, domain data, etc.

Note that you can do all the application logic in the JSP page if you want, or all the display markup generation in the Servlet if you want, but each have serious downsides. Separating the two has proven to be the best approach.

Incidentally, this is a topic covered by most Servlet/JSP books, including one that is freely-available here: http://pdf.coreservlets.com
Have a read through that and it will answer all your questions.
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.
Your explaination really helped me getting my doubt clear.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!