
JavaServer Pages (JSP), now part of Jakarta EE as Jakarta Server Pages, remains a widely used technology for building dynamic web pages in Java-based enterprise applications. JSP lets developers mix HTML with server-side Java logic to generate content that changes based on user input, database records, or session data. If you are learning how to create a dynamic web page in JSP, this guide walks you through the setup, core syntax, best practices, and a complete working example.
Enterprise Web Solutions by AAMAX.CO
Organizations running Java-based systems often need experienced developers to build, modernize, or extend their web applications, and AAMAX.CO fits that need well. They are a full-service digital marketing company offering web development, digital marketing, and SEO services worldwide. Their team works across a range of server-side technologies, building secure, scalable applications and modernizing legacy interfaces with contemporary design. Through their web application development services, they help their clients get more value from existing systems while preparing for future growth.
What Is JSP?
JSP is a server-side technology that lets you embed dynamic content in HTML pages. When a user requests a JSP page, the servlet container, such as Apache Tomcat, translates the JSP into a Java servlet, compiles it, and executes it to produce HTML. The browser receives only the final HTML, so users never see your Java code.
Step 1: Set Up Your Environment
To create JSP pages, you need:
- A Java Development Kit (JDK), version 17 or later is a good choice.
- A servlet container like Apache Tomcat, Jetty, or a full application server.
- An IDE such as IntelliJ IDEA, Eclipse, or VS Code with Java extensions.
- A build tool like Maven or Gradle to manage dependencies.
Create a new web application project, and place your JSP files in the src/main/webapp directory.
Step 2: Create Your First JSP Page
Create a file named welcome.jsp:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<!DOCTYPE html>
<html lang="en">
<head><title>Welcome</title></head>
<body>
<h1>Hello, <c:out value="${param.name}" default="Guest" />!</h1>
<p>Today is ${today}.</p>
</body>
</html>
Visiting welcome.jsp?name=Sara displays a personalized greeting. The c:out tag safely escapes the value to prevent cross-site scripting.
Understanding Expression Language and JSTL
Early JSP pages relied on scriptlets, blocks of raw Java code inside <% %> tags. Modern best practice avoids scriptlets in favor of:
- Expression Language (EL): The
${...}syntax accesses data from request attributes, session values, and beans cleanly. - JSTL (Jakarta Standard Tag Library): Tags like
c:if,c:forEach, andc:outhandle logic and loops without Java code in the view.
This approach keeps your pages readable and separates presentation from business logic.
Step 3: Use the MVC Pattern With Servlets
The recommended architecture uses a servlet as the controller, Java classes as the model, and JSP as the view. The servlet processes the request, fetches data, and forwards it to the JSP:
@WebServlet("/products")
public class ProductServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
List<Product> products = productService.findAll();
req.setAttribute("products", products);
req.getRequestDispatcher("/WEB-INF/views/products.jsp").forward(req, res);
}
}
Placing JSP files inside WEB-INF prevents users from accessing them directly, so every request goes through your controller.
Step 4: Display Dynamic Data
In products.jsp, loop through the list with JSTL:
<ul>
<c:forEach var="p" items="${products}">
<li><c:out value="${p.name}" /> - $<c:out value="${p.price}" /></li>
</c:forEach>
</ul>
Each time the page loads, it reflects the current data in your database, making it truly dynamic.
Step 5: Handle Forms
To process user input, create an HTML form that posts to a servlet. The servlet reads parameters with request.getParameter(), validates them, saves data using JDBC with prepared statements or a framework like JPA, and then redirects or forwards to a results page. Prepared statements protect your application from SQL injection.
Sessions and Personalization
JSP integrates with the HTTP session, allowing you to store user-specific data like login status or shopping cart contents. Access session attributes in EL with ${sessionScope.user.name}. Always invalidate sessions on logout and avoid storing sensitive data unnecessarily.
Best Practices
- Avoid scriptlets; use EL and JSTL instead.
- Escape all user-provided output with
c:outor equivalent. - Keep business logic in Java classes, not in JSP files.
- Use include directives or tag files for reusable headers and footers.
- Configure custom error pages for a polished user experience.
Deploying Your JSP Application
When your pages are ready, package the application as a WAR file using Maven or Gradle and deploy it to your servlet container. On Tomcat, you can copy the WAR into the webapps directory or use the manager interface. For production, place the application behind a reverse proxy such as Nginx to handle HTTPS, compression, and caching of static files. Containerizing the application with Docker makes deployments repeatable across development, staging, and production environments.
Modernizing JSP Applications
Many organizations maintain JSP applications while gradually modernizing them. A common approach is to expose business logic through REST APIs and build new interfaces with modern front-end frameworks, while existing JSP pages continue to serve stable features. This incremental strategy reduces risk while improving the user experience over time.
Conclusion
Creating a dynamic web page in JSP involves setting up a Java web environment, writing JSP views with Expression Language and JSTL, and using servlets to handle requests and data following the MVC pattern. By keeping logic out of your views, escaping output, and following security best practices, you can build reliable, maintainable dynamic pages for enterprise Java applications.