How to call Java class, Java Method from JSP Page - onlyxcodes

Monday 19 February 2024

How to call Java class, Java Method from JSP Page

JavaServer Pages (JSP) is a server-side technology that enables the development of dynamic web pages with Java. In some cases, you might need to execute a Java class and its methods directly from a JSP page to perform particular tasks or obtain data.


In this post, we will guide you through the steps of calling a Java class and method from a JSP page.


how to call java class, java method from jsp page

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. 


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


Step 3: Import the Java Class in JSP

Use the <%@ page import %> directive to import the Java class into your 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 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

Using scriptlets, create an object of the Java class in your JSP page:


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


<%
    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 you have an object of it. 


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 tried this application and obtained the desired results in the browser.


Output:


Message from Java Class: Hello World

Full Codes of index.jsp page

You will find all the codes of "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 this guide, 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.


No comments:

Post a Comment

Post Bottom Ad