eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

When developing web applications with JSP, there often arises a need to pass data from the server-side JSP to the client-side JavaScript. This allows for dynamic interactions and customization on the client side.

In this tutorial, we’ll explore different approaches to accessing a JSP variable from JavaScript.

2. Setup

Before we begin, we’ll need to set up our environment to include the JSTL library for supporting JSTL tags in our JSP pages:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

We’ll need the commons-text library to handle text manipulation, including escape Javascript statements as well:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.10.0</version>
</dependency>

3. Convert to a JavaScript Variable

Let’s start by considering a scenario where we have a JSP variable and want to embed it as a JavaScript variable:

<% 
    String jspMsg = StringEscapeUtils.escapeEcmaScript("Hello! This is Sam's page.");
    request.setAttribute("scopedMsg", jspMsg);
%>

To handle JavaScript literals properly, we utilize the StringEscapeUtils.escapeEcmaScript() method from the commons-text library for sanitization. This method helps us escape any single-quote or double-quote characters that might cause issues when we embed the variable in a JavaScript statement.

If we neglect to escape these characters, it may lead to JavaScript errors due to syntax conflicts. JavaScript treats single quotes and double quotes as special characters that can potentially disrupt the structure of a JavaScript statement. Therefore, it’s crucial to escape them to ensure the JavaScript code remains intact.

In this example, we aim to convert the JSP variable jspMsg into a JavaScript variable jsMsg, such that we can access the JSP variable on the client side:

<script type="text/javascript">
    var jsMsg = // conversion implementation here
    console.info(jsMsg);
</script>

We’d expect to see the message “Hello! This is Sam’s page.” in the browser console. Next, let’s explore the different approaches that we can apply for conversion.

3.1. Using JSP Expression Tag

The simplest way to convert a JSP variable to a JavaScript variable is by using the JSP expression tag <%= %>. We can directly embed the JSP variable within the JavaScript code:

var jsMsg = '<%=jspMsg%>';

When dealing with a scoped variable, such as one stored in the request scope, we can retrieve the attribute using the implicit request object:

var jsMsg = '<%=request.getAttribute("jspMsg")%>';

3.2. Using JSTL

JSTL can only access the scoped variable. We’ll use JSTL’s <c:out> tag to convert the scoped variable for JavaScript usage:

var jsMsg = '<c:out value="${scopedMsg}" scope="request" escapeXml="false"/>';

The scope attribute is optional but useful when dealing with duplicate variable names in different scopes. It instructs JSTL to fetch the variable from the designated scope.

If the scope isn’t specified, JSTL follows the order of page, request, session, and application scopes to fetch the scoped variable. It’s generally a good practice to explicitly specify the scope in the tag.

The escapeXml attribute controls whether the value should be escaped for XML/HTML entities or not. Since we’re converting it to JavaScript instead of HTML, we set this attribute to false.

3.3. Using JSP Expression Language (EL)

With the same scoped variable as the previous section, we could simplify the statement by using EL:

var jsMsg = '${jspName}';

We can see there’s no scope supplied in the previous statement as the simplest form. The fetch order without specifying scope is the same as what we’ve described in JSTL. We can prepend the EL implicit scope object to the variable name if we want to explicitly specify the scope:

var jsMsg = '${requestScope.jspName}';

4. Convert to HTML

Sometimes, we may want to convert a JSP variable containing HTML tags into an actual HTML tag representation that displays to users:

<% request.setAttribute("jspTag", "<h1>Hello</h1>"); %>

In this example, we’ll convert the JSP variable to HTML contents within <div> tag. We’ll use the JSP expression tag from before to display the HTML tag:

<div id="fromJspTag"><%=jspTag%></div>

Once the JSP variable has been converted to an HTML tag, we can access its content using JavaScript. We could simply access the content as a DOM element using JavaScript:

var tagContent = document.getElementById("fromJspTag").innerHTML;

5. Conclusion

In this article, we explored different techniques to access JSP variables from JavaScript. We discussed using JSP expressions, JSTL tags, and JSP Expression Language (EL) to convert and access variables.

It’s important to sanitize JSP variables before converting them to JavaScript variables. Additionally, we briefly discussed converting variables to HTML tags dynamically.

By understanding these methods, we can effectively pass data from JSP to JavaScript, enabling dynamic and interactive web application development.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)