Wednesday, 18 September 2013

jsp elements in details


 JSP elements


Lets we talk about jsp elements now in details
we learnt that jsp is nothing but html with some java code
we know how to build html pages,
How do you put the java code in the html page? Answer is we can do that by using jsp elements
with the introduction of jsp elements, we can put java code in  your static html page, which will makes yours static pages, dynamic web pages
we can build dynamic web pages using jsp elements in your static html files which static html pages, jsp pages means dynamic pages

TYPES OF JSP ELEMENTS

1. JSP Declarations
2. JSP Scriptlets
3. JSP Expressions
4. JSP Directives

1. Why do we have 4 different jsp elements?
Ans:
 Whenever you want to put java in your html page we use jsp elements. Could not be nice to have only one jsp element? and whenever i use java in my html page again simple i put that jsp element in my html page.  Basically each one of JSP Element has its own significance.

JSP Declarations:

Whatever you put the code in JSP Declarations that code goes out side the service() method.
Where did the service() method come from? we know that service () is present in servlets then How did we get service() method in jsp? becoz jsp is nothing but a servlet, so whatever code you put in your jsp at the end it is going to be converted servlet. And whatever you put in JSP  , it goes outside the service() method so what does it mean? Let's say that you have some method you want to define that can be used by at many different places. You dont want to put that same logic at multiple places in your code so in turn you want to reuse your code, so you put that reusable code in that a different method from different places so how can you achieve that? you can achieve that using jsp declarations. So declare a method using jsp declarations which will put that method outside the service method so basically it is a class level method and alternatively you can even have certain varaibles define in outside the service() method which will be used by through out the class.

1. What are JSP Declarations?
Ans: Whatever goes inside the "<%!{java code here}%>" tags  is called a declaration. Everything you write in a declarations goes outside the service(). Treat them as instance methods and instance variables.

2. Advantages of using JSP Declarations?
Ans: variable declaration is you can reuse the value across the multiple places.

3. What do you declare in declarations?
Ans: you can declare methods and variables in declarations.

4. Why do you need JSP Declarations?
Ans: If you have a variable that needs to be shared across different requests. You have repeated common code in you jsp, declare with in a method.

Ex: <%! int instanceVar=10;%>

Here the same value of 10 is reflected  when someother requests comes as well and how is it possible, how does the value retained across multiple requests you know that ultimately a jsp converts into a servlet. So whenever a new request servlet comes in, the service() method is invoked only. And  the state of that particular class remains the same that particular object remains the same we know that servlet is a multithreading environment so whenever you make a new request a new thread is born and the service() is invoked. And whatever the outside the service() whatever instance variable in your class will be shared across multiple requests.
you have repeated common code in your jsp, declare it in a method.. Ex: <%! int instanceVar=10;%>, so lets see how really works.


Figure: jsp2.jpg


  • Here I have 3 requests, and lets assume that this page is one jsp page, and ultimately it got converted into a servlet, we know that servlets are multithreaded for every new request one new thread is called but the same servlet object.
  • So when request1 comes in it sponsores a new thread so basically I have a new service() method. When request2 comes in again I have new service() and ultimately when request 2 comes in, Im going to have a new service() method for request 3 as well. 
  • For each request it has it own service() method. So whatever is there within the service() method is not been shared across these three requests, different for each of the requests. Each request is unique now.
  • but when you come to declarations whatever you have in declarations is being shared by all these three requests . That is request 1 is going to share the same value, request 2 is going to share the same value, and request3 is going to shared the same value which you have in declarations.
  • So lets say that I have declared variable i and its value is 10 when its request 1 now when request 3 comes in tries to get the value of i , that is going to see the value of i that is 10 because declarations shared multiple requests.
  • Similarly if I would have put this value i=10 in service2() method, when i make the request 3 and I will not see the value 10 again that would be the default value probably zero.. Here if you declared any variables in side the service() methods that can not be shared multiple requests



JSP Scriptlets




  • Whatever code you put in Scriptlet it goes into the service() method so most of the time you would be writing scriptlet in your jsp pages because you know that when you are coding servlet at that time what you are doing is you are overriding the doGet() method or doPost() method which is similar to overriding the service() method.
  • Whatever you code write within these tags i.e: <%........> are treated as scriptlets.
  • In servlets most of the time you are overriding the doGet() or doPost() methods.
  • What do you do in the doGet() or doPost) method? you basically add your business logic to achieve the functionality that is to achieve the dynamic functionality. Similarly even in JSP whenever you want to add some java code most of the time you will be adding some business logic in your jsp pages and all your business logic is kept in the service() method because every new request, the service() method will be invoked and you want particular code to be executed whenever new request comes to the jsp page.


Explanations about the Scriptlets:

All the business logic which you want to put in java code in your jsp it could be in the scriptlets. We know that everything you write in scriptlets goes in the service(). Treat variables in scriptlets as method/local varialbes.
If I define any variables inside the scriptlets it could have gone in the service() so when I define variable i =10  in service2 , this variable i scope only in service2 only and it will not be visible in other service () methods  thus treat them as local variable or method level .

What do you put in scriptlets?
Ans:
Business logic in JSP are in scriptlets. Set of java statements.

Why do you need scriptlets?
Ans: 1. Need to perform some small business logic (if logic is complex, do in java). 2. Need to perform some basic validations.


So only reason is introduced java, in html was to produce dynamic pages content and when we say when we want to produce some dynamic content we need to perform some kind of validations, some kind of busines logic processing we can generate an output based on the the user input for that we need some kind of business logic that is the whole picture of scriptlets.

Example: <% int localVar=10;%>

JSP Expressions:


Basically whatever you put in JSP Expressions the code is evaluated and you get the value as a output so let's  say that I have a variable called int i=10 when I put variable i in JSP Expressions tags it is going to print 10 as a output but not variable i. So basically expressions whatver you put expressions in the expression tags and displays output value.

Explanation about the Expressions:

What are Expressions?
Ans: Whatever goes inside the "<%={java code here}>". Onething you have to note in jsp Expressions is whenever you put some in jsp expressions you do not put a semi colon( ;) at the end so that is not allowed.
Lets say if you want to print the value of variable i you can not say like this . <%=i;%>//invalid because of semicolon,
<%=i%>//ok
Code inside jsp expressions is evaluated, and output is displayed
Whatever you put inside expressions should evaluate to a value.
Basically sometimes you will need to print a certain texts on to the page, on to your web page, lets say the user logs in and after successfully login in on the login page or main page, we are going to display "Welcome username" , so while going to display this dynamic username you can not do it in static html because whatever you put in static html page will remain same for each and every user and you can not really change that value there so inorder to do that you need to insert dynamic content, whatever you enter the username we need to display that username in the main page so you can do that using scriptlets since you said that scriptlets whatever you code you put in scriptlets the expression is evaluated as a output  is displayed .

Example: <%=localVar %>
So eventhough we saw that how we can completely perform all the business logic processing and my java code within jsps it is not really recommended to do bulk of business logic of in your jsps because as we talk many times main role of jsp is it should be for presentation logic and not business logic. The role of the business logic is played by the servlets and its supporting classes and not by jsps . So jsp is only should use for presentation logic so inorder to present the jsp a webpage the user , inorder to present the dynamic web page to the user whatever dynamic certain amount of logic or certain amount of business logic we need to write in your jsp you can put that otherwise all the hard code business logic process should be done in servlets and other supporting java classes and we are going to simply call those methods from jsps .


JSP Directives

It is one of the important jsp element we have. JSP Directives basically commands to the JSP Engine. We will have complete details in future class.


Example for jsp elements:

<html><head><title>JSP Elements Example</title>
</head>
<body>
<%! int userCnt=0;%>  //declaration element

//scriptles
<%
    String name="Rajendra";
     userCnt++;
 %> //end of the scriptlet

<table>
 <tr><td>
  Welcome <%=name%> //here within <%=..%> is Expression element
     Your are user number<%=userCnt%>//Expression element
  </td></tr>
 </table>
</body>
</html>

/* output*/
Welcome Rajendra you are user number2

Explanation: here im the second person who has accessed web page it is going to display welcome Rajendra and you are user  number 2, lets say that some other guys comes in access this web page, is going to display: Welcome Rajendra you are user number 3. So for each request that comes it into this web page for everyone who login simple this web page and it is going to increase the userCont by one. By this time you would have triggered  at which place in this dynamic web page and it is use jsp elements and what type of jsp elements .

So we know that the variable userCnt is shared across multiple requests , in the same web page somebody else has to remember whats the previous count and it has to increase that count. So we know that using declaration elements we can share the variable across multiple requests . So im going to declare the userCnt variable  in the jsp declarations .
Next I have defined the value of Rajendra the variable called name, and this goes into the service() method, so for every new request this will be a new name, here simply I have defined Rajendra, but in real time, we need to fetch, whatever the user enter his username, that has to be displayed. And after each request increasing the userCnt by one.
And ultimately Iam goint to display the username and the userCnt. We know that we have to display value on to the screen we have to use JSP Expressions so I used jsp Expressions to display the name and the userCnt.


I hope you understood the jsp elements concepts.

No comments:

Post a Comment