BreadcrumbsHome / JSP Tutorial #11 – Java Server Pages Tutorial – Call Java class from JSP
JSP Tutorial #11 – Java Server Pages Tutorial – Call Java class from JSP
Last Updated on Thursday, 10 December 2015 Written by Chad Darby Thursday, 12 November 2015
In this video, I show you how to call a Java class from a JSP.
Please SUBSCRIBE to my YouTube channel.
Do You Need More Details?
- How to read form data with JSP?
- Develop MVC application with JSP and Servlets?
Read more about the premium course at http://www.luv2code.com/jsp
Follow luv2code with the links below:
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
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.