JSP Tutorial #10 – Java Server Pages Tutorial – JSP Declarations

In this video, I show you how to use JSP Declarations.

Please SUBSCRIBE to my YouTube channel.

 


 

# Video
1 Introduction
2 JSP Overview
3 Setting up your Development Environment - Overview
4 Installing Tomcat
5 Installing Eclipse
6 Connecting Eclipse to Tomcat
7 JSP Hello World
8 JSP Expressions
9 JSP Scriptlets
10 JSP Declarations
11 Importing Java Classes
12 JSP Built-In Objects
13 Including Files

Do You Need More Details?

  • How to read form data with JSP?
  • How to use cookies and sessions?
  • Develop MVC application with JSP and Servlets?
I have a premium course that shows you how to complete all of these tasks.

Read more about the premium course at http://www.luv2code.com/jsp

Follow luv2code with the links below:

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

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. The result of this method call here will be included right here in this page.

Let’s go ahead and run the application. I’ll move over here to this file, “declaration.jsp.” I’ll do a right click, and I’ll say, “Run As/Run On Server.” Then the browser will come up with our result. We have “Lower case “Hello World,” and then we have of course the lower case version of the hello world string. That’s it. That’s the result of our JSP expression making a call to our method that we declared using a JSP Declaration. Good. We pulled it all together with a nice little example. Good job. Let’s go ahead, and wrap up this video. In this video I showed you how to use JSP Declarations. We created a declaration, and then we called that declaration from a JSP page.

Did you like my blog post?

Signup now and receive an email when I publish new luv2code content.

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Share

Leave a Reply