Thursday, 5 September 2013

Where does the JSP code is placed in the Servlet?

Where does the JSP code is placed in the Servlet?




Where does the JSP code is placed in the Servlet?

Ans: I told you that a jsp is nothing but a servlet so how do you determine what elemlents goes into which part of the code in your servlet thats very important for you to know.
The first thing we have seen is Scriptlet: I told you that scriptlet is nothing but java code

<% int i=10; %>


And where did this go into your java code (servlet), so the above code goes to this part that is jspservice() in the following code:
public void _jspService(req,res)
{
 int i=0;
 out.println("<html>\r<body>");
 out.println("Hello Welcome");
}

jspService is nothing but an implementation of the service() method of the servlets
The Container will not override the doGet() or doPost() methods. Container simply overrides the jsp service() methods and puts all the business in that jsp service() method
So your scriptlets goes into the _jspService() method so whatever scriptlet I had written it has gone into the _jspService() method
Now Where did this declaration code will go?

<% int count=0;%> had gone the out side the service() method in servlets: int count=0; so whatever you put your declarations does not go into _jspService() method, only goes to outside the _jspService() method.
The declaration the following whole goes to outside the jsp service() method. The whole declaration which we had added in jsp will go to outside the service() method in servlet, which we can see clearly
<%!public void display()
{
 out.println("Hello");
}%>

  • Again we discussed about the page directives, here which i have imported foo.* has gone the import statement in the Servlet class i.e: import foo.*;
  • Ultimate final Servlet class. Now you apart from these jsp element scriptlets, desclaratons, ... and I also have the html that is my general html so where does this html code goes 
  • In the above jsp code we observed that "Hello Welcome" is nothing but a html code so all the html code of the jsp code goes to the _jspService() method


public void _jspService(req,res)
{
 int i=0; //scriptlet
//html code comes here
 out.println("<html>\r<body>");
 out.println("Hello Welcome");
 out.println("</html>\r</body>;
}


  • So jsp code has been converted into servlet code by the Web Container
  • So here you can see the Container converted the out.println() statement

No comments:

Post a Comment