• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are JSPs Servlets?

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
My perception is that JSPs are a shorthand way of creating Servlets. The JSP is fist "compiled" into a .java file, which in turn is compiled into a .class file.
However, I noticed that the generated .java file does not conform to a typical Servlet. For instance, no doGet or doPost methods are created.
What gives? Are JSPs Servlets or not?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSPs are compiled into servlets.
 
Sandeep Jain
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
JSP is used for dynamic generation of html pages . This was the problem with servlets. Every time u had to go and change the code in servlet compile it and dump it back in the required directory. This problem was eased with JSP.
JSP pages are compiled into servlets. They work much faster than the servlet. Only for the first compilation it takes some time but once it is done next time the request becomes very fast.
This is bcoz of the page compile feature of JSP.
In JSP, u will not find do get or do post but u have jspservice() method.

------------------
Sandeep Jain
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandeep Jain:
JSP is used for dynamic generation of html pages . This was the problem with servlets. Every time u had to go and change the code in servlet compile it and dump it back in the required directory. This problem was eased with JSP.

It makes development a little easier. Sometimes. And only if you don't use a Java development environment. At a cost: compiler errors can be pretty hard to interpret. Making development a litte bit more difficult again...
Whatever the problem was with servlets, this was not it. In fact it wouldn't be all that difficult to create a facility for servlets that works like that for jsps. Nobody has thought it important enough apparently.
JSP pages are compiled into servlets. They work much faster than the servlet.

Take a deep breath, and read again what you wrote here Does it still make sense?
In JSP, u will not find do get or do post but u have jspservice() method.

Servlets are protocol-independent and just implement a service() method. For web servers, you do not use a raw Servlet but a HttpServlet. This class has a default implementation for service() that analyses the request and dispatches it to doGet(), doPost(), etc.
Now JSPs work differently. The statement that a JSP page compiles to a servlet actually was only half the truth. In fact, every request for a .jsp is routed to a single servlet which does the following things:

  • Check if the .jsp has already been processed and compiled. If so, it compares the timestamp of the .jsp file with the timestamp of the compiled class.
  • Processes and compiles the .jsp into a servlet if the class is obsolete.
  • Forwards the request to the generated servlet.
  • The generated servlet does whatever housekeeping is necessary and then calls _jspService where your generated code lives.

  • The statement that JSPs are as fast as, let alone faster than, servlets is false. The above steps - the timestamp check, the servlet chaining - obviously incur overheads that are absent in a plain servlet.
    Like servlets, there's a difference between a general JSP (implementing JspPage) and a HTTP JSP (HttpJspPage). HTTP JSPs do not bother to split out get and post requests like servlets do, because the request has to be handled by a single body of code anyway. That's why you don't have doGet() and doPost() methods.
    Why no service() method? Well, since a JSP is a servlet, there is a service() method. But by requiring that the generated JSP code lives in _jspService() rather than directly in service(), Sun gave JSP engine implementers the flexibility do do whatever they need to do in the service() method before calling _jspService().
    HTH
    - Peter

    [This message has been edited by Peter den Haan (edited February 21, 2001).]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic