Today is Friday, 13th January 2017

Archive for the ‘Java Server Pages (JSP)’ Category


JSP Tutorial #11 – Java Server Pages Tutorial – Call Java class from JSP

In this video, I show you how to call a Java class from a JSP.

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 call a Java class from JSP. Now, in the previous videos I mentioned that you wanted to minimize the scriptlets and declarations in a JSP. You want to avoid dumping thousands of lines of code in your JPS. It’s OK to add small bits of scriptlet, small bits of declarations, but don’t over do it. In order to help with this problem you can re-factor your code into a separate Java class, or make use of MVC. In this video, in actually going to show you how to re-factor that into a separate Java class.

What we’re going to do is we’re going to have a JSP file and this JSP file is going to actually call a separate Java class, so the Java class will have all of our code, all of our business logic and so on, and the JSP can simply make a call, let the Java code or the Java class do the heavy lifting, and then the JSP can get the results and continue on with its processing. Whenever I build videos like this I always like to put together a to-do list. The first thing we need to do is create the Java class. Once we have the class created we’re going to call our Java class from the JSP page. A lot of good things in store here, let’s go ahead and dig in and start coding.

What I’d like to do is move into Eclipse and what we’re going to do is we’re going to continue to use that existing project, JSP Demo, and the first step here is creating a Java class. I’ll move into Java Resources, Source, and actually I’ll create a package first. I’ll create a package, just a location to place our Java class. The name of our package will be com.lovetocode.jsp, and you can do ahead and hit the ‘Finish’ button. This creates the package, now I’m going to actually create my Java class in this package. I’ll simply do a right click and I’ll say New > Class. For the name of the class, I’m going to call it ‘FunUtils’, and I’ll keep all the other defaults, and once I’m happy with this I’ll go ahead and click the ‘Finish’ button.

OK, great. We have our basic class here that’s lined up. What I want to do is create a method in this class, and it’s the actual method MakeItLower, so I’ll make this method static, so public static string, MakeItLower, we pass in a string parameter and then again, very trivial method here, returndata.tolowercase. Again, this is the same method we used in our declaration example. Here I’m simple going to re-factor it and put it into this Java class. I’ll save my file up top, and so I have this method here MakeItLower that’s part of this class FunUtils. Now what I’d like to do is create a JSP file that’s going to actually call this class. I’ll move down to my Web Content directory and I’ll do a right click and I’ll say New > File. The name of this file, I’ll actually call it FunTest.jsp, because we’re testing out the FunUtils. Once I’m happy with this name here I’ll go ahead and click the ‘Finish’ button.

I’ll go ahead and take care of my basic tags here for HTML and body, and let’s have some fun here. I’m going to make a call using a JSP expression, and I’ll call that method that’s defining that class, but I have to give the fully qualified class name. So I need to give the ... I’ll call MakeItLower and I’ll pass in fun, fun, fun, because we’re having a lot of fun here. All right, this looks OK. Well, not really. There’s a lot of stuff going on here, I’ll break it down for you. We have this JSP expression and we’re going to call this class that’s in a package called com.lovetocode.jsp, that’s the package name, and then the actual class name is FunUtils, and then the method name is MakeItLower. A lot of stuff.

We can actually clean this up a bit by making use of an import statement. What I’ll do up top is in the JSP page you can actually import a class, so I’ll use the angle bracket percent with an @, and you say page import and then you give the name of the package or the class that you want to import. Here I’m going to do an import on com.lovetocode.jsp.funutils, and now I can clean up the reference here in the JSP expression. Here’s my page import, com.lovetocode.jsp.funutils. The class is FunUtils, I could also make use of the wild card if I liked, just using a star if I had more items in there, and then also the other common question as well, what are importing additional packages? Well, you simply give a comma delimited list of packages or class that you want to import. And that’s it. If we wanted to use array list, that’s how I would go about it, but we’re not using array list here so I’ll just go ahead and take it out. But again, just a comma delimited list.

All right, that’s my FunTest.jsp. Save the file, now I can go ahead and run this fun test. I can do a right click, I’ll say Run on Server, and it will prompt me to restart the server. That’s because I added a new class and it needs to do some class loading. Let’s just go ahead and hit ‘OK’. It will go through its little process, but at the end here we’ll have our output. There it is, “let’s have some fun”, and then we have, “fun, fun, fun” all lowercase. Remember the code example it was in all caps, now it’s in all lowercase, so we know that we are actually calling that method that’s defined in that Java class. Great, this is really good. We were successful in having a JSP make a call to a Java class. Let’s go ahead and wrap up this video. We went through the development process of calling a Java class from a JSP. Good job.

Share


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.

Share


JSP Tutorial #9 – Java Server Pages Tutorial – JSP Scriptlets

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

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 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.

Then I want to print out some information included in the page, so I’ll make use of out dotprint line. Then again, we’re having fun here. I really love to code. Woo hoo! And I’ll add the loop parameter here. There we go. That’s our scriptlet here. We have a for loop, we need to print out some information, we make use of that out dot print line and there it is. Pretty simple.

Let’s go ahead and save this file. Let’s move over here and let’s run it. Let’s right click on this file, scriptlet test dot JSP. Right click, choose run as, run on server. Great. Here’s our output. We have our hello world of Java, then we have this code that was generated by our for loop. I really love to code. Woo hoo! We have the loop parameters going one through five. As you see, I’m having a lot of fun here with this really love to code, but that’s it.

This wraps up the video. I showed you how to make use of JSP scriptlets. You can add some Java code to generate content dynamically, on the fly. The following videos we’ll talk about JSP declarations, so stay tuned. A lot of good stuff coming up.

Share