How to call Java Class from JSP Page in Eclipse - onlyxcodes

Tuesday 30 April 2024

How to call Java Class from JSP Page in Eclipse

Hi coders, In this tutorial I will show the six steps of calling a Java class and method from a JSP page.


In my project, I executed a Java class and its methods directly from a JSP page performed my custom logic, and obtained data.


I sharing this code in this tutorial to help you implement your Java class codes on your JSP pages and end of the tutorial I have sent full codes so you can quickly grasp the whole code.


how to call java class from jsp page in eclipse

Step 1: Create a Java Class

First, you need to create a Java class that contains methods that you want to call from the JSP page.


For instance, have a look at the simple class I created, MyClass.java, below. It is located under the "com.test" package, and it contains a string function that returns the message "Hello World."


MyClass.java


package com.test;

public class MyClass {

	public String getMessage() {
        return "Hello World";
    }
}

Step 2: Compile the Java Class

You must use the Java compiler to compile the Java class after you've created it.


In my case, I compiled this Java class in my JDK 8 version.


You ensure that the "com.test" package folder contains the compiled MyClass.java class file.


Step 3: Import the Java Class in JSP

Look here I used the <@ page import %> directive tag to import the Java class into my JSP page. Ensure that the package and class names are entered correctly.


As an illustration, 


As you can see below, I've made an easy JSP page called "index.jsp" and I used the <%@ page import %> directive to import the Java class "MyClass" into it.


<%@ page import="com.test.MyClass" %>

use the <%@ page import %> directive to import the java class into your jsp page

Step 4: Instantiate the Java Class

Take a look below In the "index.jsp" page, I have created an object of the MyClass Java class file using scriptlets.


<%
    MyClass obj = new MyClass();
%>

Step 5: Call the Java Class and Method

You can now use the Java class's methods within your JSP page as we have an object for it.


Look here, on my "index.jsp" page, I've called the MyClass Java class file's getMessage() method using the object as follows:


<%
	String message = obj.getMessage();
	out.println("Message from Java Class: " + message);
%>

Step 6: Deploy and Test the Application

Alright, our application is ready. Using the Apache Tomcat server, I executed these codes and obtained the desired results on my browser.


Output:


Message from Java Class: Hello World

Full Codes of index.jsp page

Look at the below I put all the code here so you can find all the codes of the "index.jsp" page that I explained above quickly.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<%@ page import="com.test.MyClass" %>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>How to call Java class, Java method from JSP Page : onlyxcodes</title>
</head>
<body>
	<%
    	MyClass obj = new MyClass();
		String message = obj.getMessage();
    	out.println("Message from Java Class: " + message);
	%>
</body>
</html>

Conclusion:

In the above tutorial , we've walked through calling a Java class and method from a JSP page. This approach allows you to leverage the power of Java within your dynamic web applications, enabling you to perform complex logic and interact with backend systems seamlessly.


Learn More:

Login and Register JSP MySQL Example

Insert Update Delete using JSP and MySQL

How to Display Data from Database in Modal

Json Encode In JSP - MySQL Data Encode In Json Example

Role Based Access Control in Java Web Application Example


No comments:

Post a Comment

Post Bottom Ad