More JSP Fundamentals

As we previously discussed, you shouldn’t put a lot of java code in your jsp page. It’s ok to add small bits of scriptlets and declaration but if you can’t avoid it, it is better to refactor the code and make separate classes for them. Make sure to refactor the extra code into separate Java classes and make use of MVC framework to get ahead of all the hassle. In this tutorial I am going to teach you how to call a Java class through a JSP page.

What we are going to do is that we are going to have a JSP file and a Java Class. The JSP File will contain all the code related to the front-end and it will call on the Java class, which will contain all the code and will do all the heavy lifting and return the processed result to the JSP File and it is displayed in the browser.

What we’ll do in this tutorial:

  • Create a Java class.
  • Call the created Java class from the JSP File.

Creating the class

For the purpose of this tutorial we are going to use the same project we created earlier “jspdemo”. Now open up Eclipse and the project will already be loaded. In the project explorer expand the “Java Resources” and you’ll see a src “source” folder. Right click “src” go to “New” and create a new package.

JSP_Creation

Naming convention for packages go as the following. You can either write it in a single string but the best practice is breaking down the package name into 3 parts, as such:

  • Intended use of the Project – If it commercial then name the first part as “com” or “edu” for educational purposes.
  • Your identity or a small handle – Here I am going to write “evolve” as it is my company’s name, you can name it however you want.
  • Last a specific handle for the package. This should be unique to the class as you might have a lot of classes in your project and keeping track of them could become disastrous.

So we’ll be naming the package as “edu.evolve.package”. Clicking on Finish will create the package. Now you are set to create a Java class. Right click on the package and select new and select class. Let’s just call the class “FunUtils”, leave the rest of the values set at their default and click on finish. The new class will be created with these basic lines:

package edu.evolve.package;

public class FunUtils{

}

Coding the class

And now we start writing code. I will create the makeItLower() method we created earlier. Let’s get going:

package edu.evolve.package;

public class FunUtils{
public static String makeItLower(String data){
returndata.toLowerCase();
    }
}

Although very trivial, this method is a great example for demonstrating the purpose of this tutorial. Go ahead save the file.

Calling the Class

Now go and create a new file in the “WebContent” folder which is going to call this class. Name the file whatever you want and click on finish. Open the newly create file and start writing the code.

<html>

<body>

Put some text here: <%= edu.evolve.package.FunUtils.make.ItLower(“FUN FUN FUN”) %>

</body>

</html>

Explanation

In this JSP page we have called the class through a JSP Expression. From the beginning, we just opened the expression and started the call by referring to the package and then consequently calling the class and then the method from it. The output will be:

Output

Put some text here: fun fun fun

Improving the code

Now in the above code there could be a lot of confusion for beginners so let’s improve this code. Do you know that you can actually import java classes in the jsp page? Let’s begin:

<%@ page import=”edu.evolve.package.FunUtils” %>

<html>
<body>

Put some text here: <%= make.ItLower(“FUN FUN FUN”) %>

</body>
</html>

Isn’t this more easy? To import additional packages you can just add a comma and keep on adding the required packages. You can even make use of a wildcard to include everything in a package. Like this:

<%@ page import=” edu.evolve.package.*[Including everything in the edu.evolve.package], java.util.ArrayList[including two packages at once]” %>

<html>

<body>

Put some text here: <%= make.ItLower(“FUN FUN FUN”) %>

</body>

</html>

So that is pretty much it. Save the file run it and see the result for yourself. In the next tutorial I am going to teach you about the JSP Built-in Objects and how to include files in JSP. See you later.

 

 

Series Navigation<< JSP Scripting ElementsJSP implicit objects >>
By | 2017-07-26T19:49:29+00:00 March 2nd, 2016|JSP|0 Comments

About the Author:

Leave A Comment