So, lets get started!!!
The JSP Scriptlets are those tiny bits of Java code that you can embed in your JSP page to add extra functionality to your JSP page. Though there are a lot of things you can accomplish using them, the most important use of these fellows are:
* Implementing Conditions
* To Iterate through a set of values
Implementing conditions is so trivial that, we don't need to explain it. Any programmer who has written some basic java code knows how to use an if-else block and all you need to do is to place them within the scriptlet tags and then write your conditions the way you want it.
Iterating through values too is easy but still lets look at an example because it is not as straightforward as implementing conditions.
1 < html >
2 < body >
3 < % 4 java.util.Random randomInt = new java.util.Random(); 5 int limit = 10; 6 % >
7 < ol type='i' >
8 < % for (int count=0; count
9 < li > < % = randomInt.nextInt() % > < / li >
10 < % } % >
11 < / ol >
12 < / body >
13 < / html >
In the above example, the iteration construct begins on line 8 and ends on line 10. However, if the developer left out line 10, then the iteration statement would not be correctly structured.
The scripting language is mostly Java.
You might be wondering, why did I put a separate chapter for such a trivial topic. I did that intentionally because, you can expect one or two questions that would test your knowledge of these scriptlets. So I thought it’d be better to give it a separate chapter so that you’ll remember it better !!!
The code inside the scriptlet tags are inserted into the service method of the Servlet that gets created from the JSP and all your logic inside the scriptlets get translated to proper java code.
Previous Chapter: Chapter 36 - JSP Implicit Objects
Next Chapter: Quick Recap - Chapters 31 to 37