JSP Call Java Class Example
In Java-based web applications, JSP (JavaServer Pages) is used to present dynamic content to users. Often, you need to invoke Java classes from a JSP page to perform backend logic such as computations, database access, or formatting operations. Let us delve into understanding how a JSP can call a Java class using two common approaches.
1. Introduction to the Problem
JavaServer Pages (JSP) technology is primarily used for building the presentation layer of web applications — that is, the HTML, CSS, and front-end content shown to users. However, real-world applications often require business logic, data processing, and dynamic content generation. This backend functionality is usually implemented in Java classes, such as utility classes, service components, or JavaBeans.
At times, developers may need to invoke this backend logic directly within a JSP page. While this is possible, doing so without proper structure can lead to tightly coupled and unreadable code, defeating the purpose of Model-View-Controller (MVC) architecture. JSP is intended to act as the View, and embedding too much Java code within it breaks the clean separation of concerns.
There are two common approaches for calling Java code within a JSP page:
- Using JSP Scriptlets – This is the traditional approach where Java code is embedded inside special tags like
<% ... %>. Although simple, this method is outdated and not recommended for large-scale applications. You can read more about scriptlets in the official Java EE tutorial. - Using
<jsp:useBean>Action Tag – This approach uses predefined JSP tags to interact with JavaBeans. It promotes a cleaner, declarative way of accessing backend logic and is well-suited for applications following the MVC design pattern. Learn more about JSP action tags from Oracle’s JSP action tag documentation.
Choosing the right approach depends on your project requirements and architectural design. For modern web applications, the <jsp:useBean> method is preferred due to its simplicity, maintainability, and alignment with best practices.
2. Code Example
This section walks through a complete example demonstrating two ways to call Java code from a JSP page: using JSP Scriptlets and the <jsp:useBean> action. We’ll create a JavaBean and a helper class, integrate them into JSP files, configure the deployment descriptor, and view the output in a browser.
2.1 Creating web.xml
This is the deployment descriptor for the web application. It defines the default welcome file that loads when the application is accessed.
<web-app xmlns="http://jakarta.ee/xml/ns/jakartaee"
version="5.0">
<display-name>Java Call in JSP Example</display-name>
<welcome-file-list>
<welcome-file>jsp/scriptletExample.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.2 Creating the Java Bean (User.java)
This bean is a plain Java class with a property called name, along with standard getter/setter methods. It includes a method that returns a formatted welcome message.
// User.java
package com.example;
public class User {
private String name;
public User() {
this.name = "Guest";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWelcomeMessage() {
return "Welcome, " + name + "! This is a Bean Example.";
}
}
2.3 Creating the Helper Class (MessageHelper.java)
This helper class has a method that returns a greeting message. It will be called directly from the JSP using scriptlets.
// MessageHelper.java
package com.example;
public class MessageHelper {
public String getGreeting(String name) {
return "Hello, " + name + "! This is a Scriptlet Example.";
}
}
2.4 Creating the Scriptlet Example JSP (scriptletExample.jsp)
In this JSP file, we import the helper class and instantiate it within a scriptlet block. The message is then printed using the <%= %> expression.
<%@ page language="java" contentType="text/html; charset=UTF-8" import="com.example.MessageHelper" %>
<!DOCTYPE html>
<html>
<head>
<title>Scriptlet Example</title>
</head>
<body>
<h2>Calling Java Class using Scriptlet</h2>
<%
MessageHelper helper = new MessageHelper();
String message = helper.getGreeting("Yatin");
%>
<strong><%= message %></strong>
</body>
</html>
2.5 Creating the Bean Example JSP (beanExample.jsp)
This JSP demonstrates the use of the <jsp:useBean> and <jsp:setProperty> tags to interact with the JavaBean without writing scriptlets.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:useBean id="user" class="com.example.User" scope="page" />
<jsp:setProperty name="user" property="name" value="Akshaya" />
<!DOCTYPE html>
<html>
<head>
<title>Bean Example</title>
</head>
<body>
<h2>Calling Java Class using <jsp:useBean></h2>
<strong><%= user.getWelcomeMessage() %></strong>
</body>
</html>
2.6 Running the Code and Observing Output
Once the Java classes are compiled and the application is deployed to a servlet container like Apache Tomcat, you can test both JSP files using the URLs below.
http://localhost:8080/WebApp/jsp/scriptletExample.jsp -- output Calling Java Class using Scriptlet Hello, Yatin! This is a Scriptlet Example. http://localhost:8080/WebApp/jsp/beanExample.jsp -- output Calling Java Class using <jsp:useBean> Welcome, Akshaya! This is a Bean Example.
This example demonstrates both traditional and modern techniques of invoking Java code in JSP. While scriptlets offer flexibility, using beans with <jsp:useBean> promotes a cleaner, more maintainable approach aligned with MVC principles.
3. Conclusion
Calling Java classes in JSP can be done using scriptlets or the <jsp:useBean> approach. While scriptlets are quick to implement, they mix business logic with presentation and are discouraged. The useBean method offers a more organized and MVC-friendly way to separate logic from UI. In modern applications, consider using JSP only for view rendering, and move all business logic to servlets or controller classes to keep the application maintainable and scalable.




