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