Tuesday, April 12, 2011

Chapter 37: JSP Scriptlets

In the previous chapters, we have covered various aspects of the JSP Technology and to be honest there is still a lot more to be covered about the JSP Technology. However, for now, we are going to take a look at JSP Scriptlets. This is going to be a small chapter, which we can wrap up quickly…

So, lets get started!!!

JSP Scriptlets

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 !!!

Exam Trivia:
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

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.


Google+ Followers