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.
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.
- 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
No comments:
Post a Comment