Skip to main content

Command Palette

Search for a command to run...

Struts JSP Example

Updated
Struts JSP Example
Y

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.

Date: 2017-09-25

Apache Struts 2: A Deep Dive into Building Java Web Applications

Apache Struts 2 is a robust and adaptable framework designed for creating sophisticated Java web applications. Its primary goal is to simplify the entire development lifecycle, from initial construction and deployment to long-term maintenance. This framework provides a structured approach, significantly reducing the complexity associated with building enterprise-level web applications. Instead of writing vast amounts of repetitive code to handle user requests, manage data, and render views, Struts 2 provides pre-built components and a clear architectural pattern to handle these tasks efficiently.

At the heart of Struts 2 lies the concept of a Model-View-Controller (MVC) architecture. This architectural pattern cleanly separates the application's concerns into three distinct parts:

  • Model: This represents the data and business logic of the application. In a Struts 2 application, the model often consists of Java classes that encapsulate data and the methods that operate on that data. These might handle database interactions, complex calculations, or other application-specific processes.

  • View: This is responsible for presenting the data to the user. In Struts 2, views are typically implemented using JSP (JavaServer Pages) or other template technologies. These views receive data from the controller and display it in a user-friendly format.

  • Controller: This acts as the intermediary between the model and the view. It receives user requests, processes them using the appropriate model components, and then selects the correct view to display the results. In Struts 2, the controller is primarily managed by the framework itself, with user interactions routed through configured actions.

The Struts 2 framework provides a central Controller Servlet, which acts as the main entry point for all incoming requests. This servlet is automatically registered within the application's deployment descriptor (web.xml), a configuration file essential for any Java web application running on a servlet container like Tomcat. Think of this servlet as a traffic controller directing web requests to the appropriate handlers.

The struts-config.xml file (now often replaced by struts.xml in newer versions) serves as the framework's central configuration file. This file maps incoming URLs to specific Action objects. These Action objects are essentially Java classes that contain the application's business logic. When a request matches a URL pattern defined in struts.xml, the corresponding Action object is invoked. The configuration file also defines any necessary ActionForm objects, which are used to temporarily store data submitted through HTML forms before it is processed by the Action object.

The Action object utilizes its execute() method to process the request. This method might access data from the ActionForm, interact with the model (for example, to update a database or perform calculations), and ultimately prepare the results to be sent back to the user. After processing, the Action object selects an appropriate result, which often specifies a JSP page or another view to render the output. This result is then forwarded to the user's browser, completing the request-response cycle.

Creating a simple "Hello World" application using Struts 2 demonstrates these core principles. Setting up a project usually involves creating a Maven project (a standardized way to manage project dependencies and build process). Maven handles the complexities of downloading and including all necessary Struts 2 libraries, including the core framework, OGNL (Object-Graph Navigation Language, used for data binding), and other supporting components. The project's structure is typically organized to separate Java source code, configuration files, and web resources like JSP pages.

The core component of a Struts 2 application is the Action class. This class handles user requests, performs any necessary processing, and then specifies which view to render. This class typically contains an execute() method, which contains the application's logic. The Action class may access data from a form, interact with the database (through a model class), and prepare data to be passed to the view.

Configuration files, particularly struts.xml, are crucial. This file maps URLs to specific Action classes and defines the results associated with each Action. For instance, a configuration entry might map a URL like /hello to a specific Action class, specifying that upon successful processing, the success result should render a particular JSP page.

JSP pages act as the views in a Struts 2 application. These pages use Struts 2 tags to seamlessly integrate with the framework, allowing easy access to data passed from the Action class. These tags simplify the process of displaying data, handling user input, and other view-related tasks. One crucial aspect is separating the presentation logic from the application logic, ensuring maintainability and scalability.

Finally, the web.xml file is essential for any Java web application deployed in a servlet container. This file is where the Struts 2 filter is configured. The StrutsPrepareAndExecuteFilter is a critical component of the framework, acting as the interceptor of incoming web requests. This filter intercepts requests matching certain patterns and hands them off to the appropriate Struts 2 mechanisms for processing.

Deployment of a Struts 2 application typically involves using a servlet container like Tomcat. The application, once packaged, is deployed to the container's webapps directory. Once deployed, the application is accessible through a URL provided by the servlet container, allowing users to interact with the application via their web browsers.

In essence, Apache Struts 2 provides a complete architecture for building robust and maintainable Java web applications. By providing a solid MVC framework and a clear set of conventions, it streamlines development, allowing developers to focus on business logic rather than low-level details of handling web requests. The framework's flexibility and extensibility make it suitable for a wide range of applications, from small websites to large-scale enterprise systems.

Read more

More from this blog

The Engineering Orbit

1174 posts

The Engineering Orbit shares expert insights, tutorials, and articles on the latest in engineering and tech to empower professionals and enthusiasts in their journey towards innovation.