BreadcrumbsHome / JSP Tutorial #9 – Java Server Pages Tutorial – JSP Scriptlets
JSP Tutorial #9 – Java Server Pages Tutorial – JSP Scriptlets
Last Updated on Thursday, 10 December 2015 Written by Chad Darby Wednesday, 11 November 2015
In this video, I show you how to use JSP Scriptlets.
Please SUBSCRIBE to my YouTube channel.
Do You Need More Details?
- How to read form data with JSP?
- Develop MVC application with JSP and Servlets?
Read more about the premium course at http://www.luv2code.com/jsp
Follow luv2code with the links below:
JSP Tutorial Transcript:
Hello. In this video, we’re going to learn about JSP Scriptlets.
What exactly is a scriptlet? Well, a scriptlet is a JSP concept that allows you to add one to many lines of Java code. Once you have this set up, then you can also include content in the page by making use of out dot print line. The basic syntax is angle bracket percent, you write your x number of lines of code, then percent angle bracket. That code will be executed top down when the page is processed.
That’s the back ground. Let’s look at a code example. Very basic example here. Hello world of Java. Then I’ll add a scriptlet. Again, angle bracket percent. Once inside of the angle bracket percent, then you can start writing real Java code. What I’d like to do here is set up a for loop. For int i equals one, i is less than or equal to five, i plus plus. I’d like to print out “I really love to code.”
Now, instead of doing a system out print line, I’ll make use of out dot print lines, so this content will be included in the HTML page that’s returned. Here, I say out dot print line, “I really love to code” and I can add in the value of I.
Now, here’s the output at the bottom of what we’ll see once we run this JSP. We’ll have our header, hello world of Java. Here we’ll have our statement printed five times along with the actual loop index. That’s the basics there on setting up a JSP scriptlet. Angle bracket percent, and then you go ahead and you write your Java code.
Before we move into the actual coding demo, I want to just let you know about the best practice. You want to minimize the amount of scriptlet code in a JSP, so don’t go overboard. Avoid dumping thousands of lines of Java code in a JSP, because it makes it hard to maintain, it’s poor design, and it’s poor architecture. What you want to do if you have a lot of code that you need to use in a JSP, I recommend that you refac this into a separate Java class, or make use of MVC. I’ll show you how to do both of those later in the course. I’ll show you how to make use of a separate Java class, I’ll also show you how to make use of a MVC framework when we have servlets and JSPs working together. But anyway, I just had to say this upfront as a best practice so you don’t go off and do things the wrong way.
Let’s go ahead and move into eclipse. Let’s write some code. What I like to do is continue to use the previous project, JSP demo. What we’re going to do here is actually move into our web content directory, and we’re going to create a new file. I’ll just right-click on web content and I’ll say new, and I’ll choose file. The actual name of the file that I’m going to set up here is called scriptlet test dot JSP. That’s the file name. Once you’re happy with this file name, you can go ahead and click the finish button.
Great, so we have this blank file, and we need to start writing some code. First off, let me expand the window here. I’ll set up the basic HTML. I’ll go through and set up the basic body. I’ll set up that header three, hello world of Java. Great. Now I’m going to make use of a scriptlet, so again, angle bracket percent, then we can write our Java code here. I’m going to do something very similar to what we had on the slide. I’ll simply set up a for loop for i equals one, i is less than or equal to five, i plus plus.
Then I want to print out some information included in the page, so I’ll make use of out dotprint line. Then again, we’re having fun here. I really love to code. Woo hoo! And I’ll add the loop parameter here. There we go. That’s our scriptlet here. We have a for loop, we need to print out some information, we make use of that out dot print line and there it is. Pretty simple.
Let’s go ahead and save this file. Let’s move over here and let’s run it. Let’s right click on this file, scriptlet test dot JSP. Right click, choose run as, run on server. Great. Here’s our output. We have our hello world of Java, then we have this code that was generated by our for loop. I really love to code. Woo hoo! We have the loop parameters going one through five. As you see, I’m having a lot of fun here with this really love to code, but that’s it.
This wraps up the video. I showed you how to make use of JSP scriptlets. You can add some Java code to generate content dynamically, on the fly. The following videos we’ll talk about JSP declarations, so stay tuned. A lot of good stuff coming up.