JSP Tutorial #8 – Java Server Pages Tutorial – JSP Expressions

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

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:

Hello. In this video we’re going to learn how to use JSP expressions. In JSP there’s actually different types of scripting elements. There’s three actually: JSP expressions, JSP scriptlets, and declarations. Now, what we’ll do is we’ll actually have a deep dive on each one of these topics in separate videos, but I wanted to give you just an overview real quick.

A JSP expression is basically a small java expression that you can include in the page. They syntax is angle bracket, percent, with an equal symbol, and we’ll see a lot of that in this video. There is also what we call a JSP scriptlet. A JSP scriptlet is simply an angle bracket, percent, and then you add some java code. You can add one to many lines of java code, and we’ll see that in the following video. Then finally, there’s a JSP declaration. This is angle bracket, percent, with an exclamation point. Here you can define an java variable or method. Again, we’ll do a deep dive on that.

This next slide here is on JSP expressions. Basically you can compute some type of expression and the result of that is included in the HTML page that’s returned to the browser. Here is an example. We saw this from one of our previous videos, actually the previous hello world video. In your JSP file, you can say, “The time on the server is,” and then you can add an expression. Again, remember angle bracket, percent, with an equal symbol, and we can place a java object there. Once this is processed by the server, then the generated HTML is simply the two string version of that given date object.

Let’s take a look at some more examples of using JSP expressions. In this next example I’m going to show you how we can convert a string. Here I have this new string, hello world. Then I can say dot to upper case. I’m going to basically convert this string to all caps, or to all upper case, and the result of that will be included right here in the page. That’s for strings and objects. You can also make use of mathematical expressions. Here I can multiply a value, 25 times 4 equals … You can put any math expression in there that you’d like. You can also have Boolean expressions. Here I could say, “Is 75 less than 69?” It will give me a value of true or false. That’s an example, here, of using JSP expressions. We saw objects, mathematical expressions, and also Boolean expressions.

Okay, great. Let’s move into Eclipse and let’s try this stuff out. I’d like to continue to use the same project from the previous video, JSP Demo. I’d like to move down to my web content folder. What I want to do here is actually create a new file. I’ll just right click, I’ll say, “New file.” For the name of this file, I’ll simply call it ExpressionTest.jsp. Once your happy with that file name, you can go ahead and click the finish button. Okay, great. We have a very basically file here, actually an empty file. We need to add something in it. Let’s go ahead and start off with our basic HTML. I’ll set up my body tag. Now I’ll go ahead and type out that first bit of converting a string to upper case. Now what I’d like to do is add a JSP expression. Again, angle bracket, percent. Here is say, “New string, hello world,” and I do a dot and I get the code completion here and I’ll say dot to upper case. I’ll select that item there.

It’s kind of cool. Eclipse can kind of help you out with some of the code completion there in a JSP file. All right, so that’s it. That’s the file. I can go ahead and save it. What I’d like to do is go ahead and run this file, I’ll just do a right click, I’ll say, “Run as,” and then I’ll choose, “Run on server.” All right, so this will start up my Tomcat server if it’s not running already. Then right here in the browser it will give us the result. Converting a string to upper case. Remember, we had hello world as normal case, and now this basically converted hello world to all upper case. That’s the result of our JSP expression running, which is really cool.

All right, so let’s take this a little bit further. I’m just going to add some line breaks in here. Now I want to do an example using a mathematical expression here. Here I’ll say 25 multiplied by 4 equals … Now again, I’ll make use of my JSP expression. Angle bracket, percent, with an equal symbol, 25 times 4. The result of this, of course, is 100. Right? The value of 100 will be included right there. I simply save the file. Now here, I can simply go back to my browser. I don’t have to run it again. All I have to do is simply reload this page, and the results will show up. I’ll simply hit the reload or the refresh button here, and there it is, 25 multiplied by 4 equals 100. Mission accomplished. Very good. Again, a nice thing here with JSP, if you make any changes to the file, you don’t have to rerun it or redeploy it, you simply just reload that page and you’re ready to go.

Okay, let’s do one more thing. Let’s play around with the Boolean expressions. Let me just move down here, add a line break. Let me just add some extra white space here just so you can see everything. Now I’ll just have a very basic Boolean expression example, is 75 less than 69, and I’ll let java compute this for me. Again, very trivial, right? We’re just showing an example here of a Boolean expression. Is 75 less than 69? This will evaluate if it’s true or false. Of course, we know this will return false, but we’ll see how the JSP program will help us out with this. All right, so I’ll save the file here. I’ll move back over to the browser. Again, all I have to do is just reload or refresh. Then there it is. Is 75 less than 69? False. Very good. We saw a really good example there of using JSP expressions for Booleans, also for math and also for string objects.

All right, so let’s go ahead and wrap up this video. We did a deep dive on JSP expressions. I showed you different types of expressions, using objects, math expressions, and also Boolean expressions. In the following videos we’ll dive into JSP scriptlets, and we’ll also dive into JSP declarations. Stay tuned. See you there.

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