JSP Tutorial #12 - JSP Scriptlets

preview_player
Показать описание
FULL COURSE: JSP, Servlets and JDBC (80+ videos)

----

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

----

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

----

Follow luv2code for more JSP tutorial:

---

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

---

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)

---

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"
Рекомендации по теме
Комментарии
Автор

You are the best instructor!! many thanks

SyarifZapata
Автор

That's my best JSP lesson ever since!

victorpinasarnault
Автор

"As a best practice... ....so you don't go off and do things the wrong way <heh heh>"
That was classic, thanks

stephanieezat-panah
Автор

ohh man, I swear you're amazing. Thankk you so much for this hight level tutorials

outhounazakaria
Автор

How to send data from the java servlet to the HTML page

AnilKumar-nqlp
Автор

Waw, simple and easy. You are great. Luv2code

cherifnadjib
Автор

Why did we not include System (class name)? The loop statement was not printed when I added the "System."

anasali
Автор

And that's the most funny lesson that I ever heard!

victorpinasarnault
Автор

ha ha very easy to understand.Thank you bro.

hamzaKhan-kvxv
Автор

What's the point of the jsp expression? It seems redundant. Is it just to reduce the need for out.print() ?

cwash
Автор

out.println("<br/>I love to code"+i);
Why <br/> not gets printed?

balajivenkatesan
Автор

Hi for code
<% for(int i=5; i<=5;i++){
out.println("<br/>Hello: ", + i);
}
%>
I am getting error
Multiple annotations found at this line:
- The method println(int) in the type JspWriter is not applicable for the arguments
(String, int)

RenuSharma-nchx
Автор

why do we have to use <br/>code in the first example of JSP Scriplets
plz answer luv2code

arulchudiwale