JSP Tutorial #12 – JSP Scriptlets



JSP Tutorial #12 – JSP Scriptlets

JSP Tutorial #12 - JSP Scriptlets

FULL COURSE: JSP, Servlets and JDBC (80+ videos)
http://www.luv2code.com/jsp (SPECIAL DISCOUNT)

—-

This JSP tutorial series will help you quickly get up to speed with JSP.

Download Tutorial Source Code: http://www.luv2code.com/downloads/youtube-jsp/jsp-tutorial-source-code-lite.zip

—-

View more videos on the playlist: JSP Tutorial: https://goo.gl/fFVMrj

Closed-Captioning and English subtitles available for this JSP Tutorial.

—-

Follow luv2code for more JSP tutorial:

Website: http://www.luv2code.com
YouTube: http://goo.gl/EV6Kwv
Twitter: http://goo.gl/ALMzLG
Facebook: http://goo.gl/8pDRdA

If you liked my JSP tutorial, then join my mailing list: Get exclusive access to new Java tutorials.

– http://www.luv2code.com/joinlist

Questions or problems about this JSP tutorial? Post them in the comments section below.

Want to suggest a video for my JSP tutorial? Leave a comment below. I’m always looking for new video ideas.

Let me know what video you’d like for me to create.

Premium JSP Course

Need More Details on JSP?
– See my Premium JSP and Servlets course (80+ videos)
– http://www.luv2code.com/jsp

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.

[snip] for complete JSP tutorial transcript, select “More … Transcript”

Comments are closed.