JSP Tutorial #13 – JSP Declarations



JSP Tutorial #13 – JSP Declarations

JSP Tutorial #13 - JSP Declarations

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

Hey. In this video I’m going to show you how to use JSP Declarations. JSP Declarations basically allow you to declare a method in the JSP page, and then you can call the method from the same JSP page. It’s very useful like any normal code that you create. If you need to execute some code over and over again, you simply encapsulate it in a method declaration, and you can do a similar thing here with JSP. The syntax is basically an angle bracket, percent, with an exclamation point, and then you declare your method like any normal JAVA method.

Let’s take a look at example here of a JSP declaration. Up top I’m going to declare a method. Again declarations angle bracket, percent, with an exclamation point. Once you’re inside of here this is just like a normal JAVA method declaration. You have the return type of string, the name of the method to make it lower, any input parameters, in this case I have string data. Then internally inside the body you write your actual implementation code. In this example my implementation code’s going to be real simple. If I make it lower I’ll simply take the data, and call “.toLowerCase.” Again, very trivial method here. I just wanted to show you how to declare a method in a JSP page.

Then at the bottom I can make use of that method. Here I’ll have a … Make use of a JSP expression. I’ll do angle bracket, percent, with an equal symbol, and I say, “makeItLower,” and I pass in the string that I want it to convert. That’s it. At the bottom here this is what the output will look like: Lower case “Hello World”, and then colon, and then I have the actual hello world. The result of making that expression, or that method call, the results will be displayed right here in that JSP page. That’s basically it.

Let’s talk about some best practices. This follows with what I discussed earlier with scriptlets. Similar thing here. You want to minimize the number of declarations in a JSP page, and also avoid dumping thousands of lines of code in the JSP. Also you’ll want to re-factor this into separate JAVA classes, or make use of MVC. I’ll cover all of that later in the course. In general don’t overdo it with JSP Declarations.

Let’s move into Eclipse, and I want to continue to use the existing project JSP Demo. What I’d like to do is move down to this web content folder, and I’m going to add a new file. I’ll simply do a right click, and I’ll say “New File.” I’m going to create this new file. The name of the file … I’ll call it, “declarationtest.jsp.” Once you’re happy with the file name you can go ahead and click finish. Here’s our file. It’s empty. We need to add some code here. I’ll start off with my normal HTML and body. Now what I’d like to do is actually declare a method, so I use the JSP syntax: the angle bracket, percent, with the exclamation point. At this point I can actually start writing normal JAVA code, so I’m going to write a JAVA method. Here the return type of string, method name of “makeItLower,” input data string, and inside I’ll actually write the real implementation code. Again this very trivial example, I’ll just say, “data.toLowerCase.” That’s really it for this method, “Declarations.” Again with JSP you make a declaration by using an angle bracket, percent, with the exclamation point.

Great. We have the method declared. Now let’s use this method. Here, I’ll just have some text, “Lower case” … Actually I’ll say, “Lower case “Hello World,”” and then I’ll make use of that JSP expression, so I can call that method, “Declaration.” I say, “makeItLower,” and I pass in some data, call it “Hello World,” and then there we go. That’s an example here of making a call to that method, and I call it by making use of a JSP expression.

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

Comments are closed.