-1

I am new to JSP and I was looking at the JSP code getting converted to Servlet code. When the JSPC(compiler) converts the code into Servlet code and calls the JAVAC compiler to generate the .class file. Then doesn't JSP compiler hand it over to the Servlet Container for further execution?

Also if it goes to the Servlet container,then how does Servlet API recognize the lifecycle methods as they are named as JSPInit, JSPDestroy and so on because in Servlet Interface the method names are different.I am a little confused as to what happens.

Sesha Swarup
  • 97
  • 1
  • 2
  • 11
  • Take a look here: http://www.beingjavaguys.com/2013/07/jsp-and-servlet-tutorials-jsp-lifecycle.html – meskobalazs Jan 09 '15 at 08:21
  • Hi meskobalazs I had taken a look at the website.There it says the JSP engine takes care of the life cycle methods.But in various other websites and textbooks it says it gets compiled as a Java Servlet.And the Servlet container takes care of that. – Sesha Swarup Jan 09 '15 at 08:24
  • Well, the article is not the best one out there, that's why I only just commented the link. – meskobalazs Jan 09 '15 at 08:29
  • This may be of relevance: http://stackoverflow.com/questions/10607415/how-does-jsp-work – meskobalazs Jan 09 '15 at 08:36
  • Thanks for the help.I have gone through the link and it is still not clear to me.Because the user gpeche had said "If the JSP is not compiled yet, the JSP servlet translates the JSP to some Java source code implementing the Servlet interface. Then it compiles this Java source code to a .class file. This .class file usually is located somewhere in the Servlet container's work directory for the application."Then according to this explanation the JSP servlet code should have method names similar to the Servlet Interface. – Sesha Swarup Jan 09 '15 at 08:39
  • The JSP source is translated to a Servlet class (a `.java` file), this is compiled to a `.class` file, and the servlet container loads it using its `ClassLoader`. After this, it is equivalent to a servlet you have written yourself. – meskobalazs Jan 09 '15 at 08:45

1 Answers1

2

This is from an Oracle article:

When a JSP page is called, it will be compiled (by the JSP engine) into a Java servlet. At this point the servlet is handled by the servlet engine, just like any other servlet. The servlet engine then loads the servlet class (using a class loader) and executes it to create dynamic HTML to be sent to the browser. The servlet creates any necessary object, and writes any object as a string to an output stream to the browser.

This is a pretty general description, however it gives you the basic idea. This image basically summarizes the text:

enter image description here

meskobalazs
  • 15,741
  • 2
  • 40
  • 63