Importing a Class to JSP (JAVA) using Eclipse
One of the errors that I have come across when working on the Eclipse IDE and Macbook and very common throughout the JAVA Community while working with JSPs is the error below:
Before getting to how I tackeled this issue, I have noted specs about my computer and the software I have installed.
Eclipse Version: 2022-09 (4.25.0)
Eclipse IDE for Enterprise Java and Web Developers
Server: Tomcat v10.0 Server
openjdk version “22.214.171.124” 2022–08–18
OpenJDK Runtime Environment Homebrew (build 126.96.36.199+1)
OpenJDK 64-Bit Server VM Homebrew (build 188.8.131.52+1, mixed mode, sharing)
MacBook Pro 15-inch, 2017
Processor: 2.9 GHz Quad-Core Intel Core i7
Memory: 16 GB 2133 MHz
macOS: Ventura 13.0
The Error and Issue
The error states that the class can not be compiled. When we try to use a class that we have created within the JSP, it either states that is is a package or that it cannot be resolved. The reason for this could be the way how we created the project within Eclipse at first, and a missing step we also have to implement in out JSP. Here is the procedure.
We first want to make sure we create the project correctly. Make sure to go through each step carefully.
Step 1: Go to File -> New -> Dynamic Web Project. This is where we are going to create a new project with the correct configurations. Make sure to give the project a name and select “Apache Tomcat v9.0” for “Target Runtime”. After Click on Next.
Step 2: Configure project for building a Java application. The source folders should look like this: src/main/java . This is the default within the version of Eclipse I stated above. (You many not have to make any changes here). Click Next.
Step 3: Configure Web Module Settings. The only thing you will checkmark is “Generate web.xml deployment descriptor”. Once this is checked, click on finish.
Here is an example of the file structure you will establish once the above steps are done.
Great! Now we are going to create a Java Class and use it in a JSP.
Creating a JAVA Class
In order for us to create a new class, we have to create a package that contains the class. Here is the procedure.
Step 1: Right click on the “src/main/java” directory and choose New Package. For the name, I suggest to start it with “com” and end with “.jsp”. For example: com.luv2code.jsp . Once you are done, click on Finish.
Step 2: Right Click on the package you have created, and click New Class. Make sure to add the name of the class on input field with the label of name. Click Finish when it is done.
Step 3: Construct the class with the methods you need to create. Once you are done creating your class, make sure to save it.
**You will notice the package name will be on top on line 1 of the class file.
Creating a JSP File
Before moving forward on to creating a JSP File, make sure that all the above steps were followed precisely. We will be creating a JSP file and importing the class to use that within the JSP File.
Step 1: Within the “src/main/java” directory Right Click on the “webapp” folder and create New JSP File. Call the filename whatever you want and click on Next
Step 2: Uncheck “Use JSP Template”. Click Finish. Click on the JSP File and add simple html and body tags.
Importing the Class to the JSP
We have created a class and we want to use the method from that class inside of the JSP File. We have to make sure that we have a couple things checked, to compile and render successfully for the user’s to view. Here is the procedure:
Step 1: IMPORTANT!! Make sure you add a import with the package name and the class name. You could also use the wildcard to import all the classes. This would look similar to this:
Step 2: Create a scriplet declaration to instantiate a new class object within the body tags of the HTML. Similar to this:
<%! Fun one = new Fun(); %>
Step 3: Use the variable to use any method from the class. For Example:
Let’s enjoy Fright Fest at <%= one.makeItUppercase(“six flags”) %>
Step 4: If you have not set up the server, you can click here to see a video on how to set up the Tomcat Server.
Step 5: Open up the file on the Tomcat Server and should see that the error is no longer there.
You can see we can import a class into a JSP File using Eclipse Developer IDE. Making sure that we properly set up our file structure and create ouur servers correctly. You will also see a walkthrough video below explaining these steps and see how this error was resolved.