Thursday 5 September 2013

jsp elements

jsp elements


jsp elements


  • Now let us discuss some of the jsp elements. I told you that you can write java code within jsp elements, jsp tags. So apart from that general html tags whatever you have you can even embeded some java elements.
  • How do you embeded that java elements? using these jsp elements.

The following are the 4 jsp elements:

1.scriptlets
2. Expressions
3. Declarations
4. Directives

So these are four different jsp elements, which You can embed jsp file apart from the html tags . So number 1. Scriptlets

So whatever java code you want to write within your jsp, you can put that in just scriptlet tag.
Example:
                <% int i=10; %>
                <%Dog d=new Dog();%>

  • when you say angular brackets percentile will be treated as a pure java code so whatever java code you want to write in your jsp's you can put them within scriptlet tags


2. Expressions:

<%=i%>
<%=d.getName()%>


  • Expressions looks something like this: angular brackets, persentile equal to some java code but what happens this values of expressions.
  • Whatever you put this within the expression tag goes directly to the output this "out.println() statement.

Ex:
<%=i%>                    is  goes to directly like this out.println(i)
<%=d.getName()%>  is  goes to directly like this out.println(d.getName())

Here out.println() means you are printing something to the UI. So basically it prints out this value of i and so on
Basically Expressions elements are input to out.println() statements

3. Declarations

Ex:
<%! int i=10;%>
<%! void display() { System.out.println("Hello");}%>

declarations again something like this angular brackets, percentile and explanation mark , again you write java code in that .
But whatever you want to perform initialization that has to be done only once you put that in the declarations elements

4. Directives

Ex:
Pages <%@page import="foo.*,bar.*"%>
include<%@include file="/foo/myJsp.jsp"%>
taglib<%@taglib url=Tags" prefix="cool"%>

We have different types of directives, those are :

pages,
include,
taglib


  • Basically when a jsp is translated and convreted back into your servlet.
  • Whats the first thing the Web Container does? The first thing the container does is it looks for the directives elements, that is it looks for all the directives. That is directive elements starts with : angular brackets percentile at the rate so it looks for all the directives and it checks whats the directive is ? and acts accordingly and it process the directive and then it goes in to the code and tries to converts all these that is whatever java scriptlets, expressions and declarations we write. It tries to converts thats into servlets. But the first things its look into and converts the directives ok
  • So here we have "page" directive, in the page directive we can write "import" statements  like general java imports whatever the classes you want to import, packages classes 
  • And the include directive, you can include files so whatever file you inlcude here will directly be replaced in your jsp files so for example, in myJsp, i say "include<%@include file="/foo/myJsp.jsp"%>"  so whereever i write this code in a.jsp the whole content of b.jsp is replaced as it is in my a.jsp at the location where I write this include statements


taglib :

  • you have this uri and prefix attribute for this taglib directive
  • So basically you can add taglibrary to your jsp and thats a little bit advanced concepts but right now we are not discussing in this session. taglibrary directive will be discussed in JSP class



  • So basically these are the 4 jsp elements you can put in a jsp. So you can have only scriptles, expressions, declarations, directives apart from the basic html tags which you have in your jsp's
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 objserved 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