JSP Tutorial #14 – Call Java class from JSP



JSP Tutorial #14 – Call Java class from JSP

JSP Tutorial #14 - Call Java class from JSP

FULL COURSE: JSP, Servlets and JDBC (80+ videos)
http://www.luv2code.com/jsp (SPECIAL DISCOUNT)

—-

This JSP tutorial series will help you quickly get up to speed with JSP.

Download Tutorial Source Code: http://www.luv2code.com/downloads/youtube-jsp/jsp-tutorial-source-code-lite.zip

—-

View more videos on the playlist: JSP Tutorial: https://goo.gl/fFVMrj

Closed-Captioning and English subtitles available for this JSP Tutorial.

—-

Follow luv2code for more JSP tutorial:

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

If you liked my JSP tutorial, then join my mailing list: Get exclusive access to new Java tutorials.

– http://www.luv2code.com/joinlist

Questions or problems about this JSP tutorial? Post them in the comments section below.

Want to suggest a video for my JSP tutorial? Leave a comment below. I’m always looking for new video ideas.

Let me know what video you’d like for me to create.

Premium JSP Course

Need More Details on JSP?
– See my Premium JSP and Servlets course (80+ videos)
– http://www.luv2code.com/jsp

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 package name.class name.method name. 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.

What I’ll do up top is in the JSP page you can actually import a class,

[snip] for complete JSP Tutorial transcript, select “More … Transcript”

Comments are closed.