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.

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