Model View Controller Three Jsps And A Servlet Calculator Example

MVC Calculator with Three JSPs and Servlet

Compute performance metrics for your Model-View-Controller architecture with JSP and Servlet implementation

Comprehensive Guide: Model-View-Controller with Three JSPs and Servlet Implementation

The Model-View-Controller (MVC) architectural pattern remains one of the most effective approaches for developing Java web applications. When implemented with JavaServer Pages (JSP) for the view layer and Servlets for the controller, this pattern provides excellent separation of concerns, maintainability, and scalability. This guide explores how to implement an MVC application using three JSP pages and a servlet, with practical performance considerations.

Core Components of MVC with JSP/Servlet

  1. Model: Contains the business logic and data. Typically implemented as JavaBeans or POJOs.
  2. View: Handles the presentation layer. In this case, implemented using three JSP files:
    • Input form (input.jsp)
    • Results display (results.jsp)
    • Error handling (error.jsp)
  3. Controller: Manages the flow between model and view. Implemented as a single Servlet that:
    • Receives HTTP requests
    • Invokes model operations
    • Selects appropriate view
    • Forwards requests with data

Performance Characteristics by Architecture

Architecture Avg Response Time (ms) Throughput (req/sec) Memory Usage (MB) Development Time
Single JSP (No MVC) 180 450 120 Low
MVC with 1 JSP 120 680 95 Medium
MVC with 3 JSPs 95 820 110 High
MVC with Framework (Spring) 85 950 140 Very High

Implementation Steps for Three JSPs and Servlet

  1. Project Structure Setup

    Create the following directory structure in your web application:

    WEB-INF/
    ├── classes/
    ├── lib/
    ├── views/
    │   ├── input.jsp
    │   ├── results.jsp
    │   └── error.jsp
    ├── web.xml
                    
  2. Servlet Configuration (web.xml)

    Configure your controller servlet with proper mappings:

    <servlet>
        <servlet-name>CalculatorServlet</servlet-name>
        <servlet-class>com.example.CalculatorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CalculatorServlet</servlet-name>
        <url-pattern>/calculate</url-pattern>
    </servlet-mapping>
                    
  3. Input JSP (input.jsp)

    Create a form that collects user input:

    <form action="calculate" method="post">
        <input type="number" name="operand1" required>
        <input type="number" name="operand2" required>
        <select name="operation">
            <option value="add">Add</option>
            <option value="subtract">Subtract</option>
        </select>
        <button type="submit">Calculate</button>
    </form>
                    
  4. Servlet Controller

    Implement request handling and business logic:

    public class CalculatorServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request,
                             HttpServletResponse response)
                throws ServletException, IOException {
    
            try {
                double op1 = Double.parseDouble(request.getParameter("operand1"));
                double op2 = Double.parseDouble(request.getParameter("operand2"));
                String operation = request.getParameter("operation");
    
                double result = calculate(op1, op2, operation);
    
                request.setAttribute("result", result);
                request.setAttribute("operand1", op1);
                request.setAttribute("operand2", op2);
                request.setAttribute("operation", operation);
    
                RequestDispatcher rd = request.getRequestDispatcher("/views/results.jsp");
                rd.forward(request, response);
    
            } catch (Exception e) {
                request.setAttribute("error", e.getMessage());
                RequestDispatcher rd = request.getRequestDispatcher("/views/error.jsp");
                rd.forward(request, response);
            }
        }
    
        private double calculate(double op1, double op2, String operation) {
            switch(operation) {
                case "add": return op1 + op2;
                case "subtract": return op1 - op2;
                default: throw new IllegalArgumentException("Invalid operation");
            }
        }
    }
                    
  5. Results JSP (results.jsp)

    Display the calculation results:

    <%@ page isELIgnored="false" %>
    <h1>Calculation Results</h1>
    <p>
        ${operand1} ${operation} ${operand2} = ${result}
    </p>
    <a href="input.jsp">New Calculation</a>
                    
  6. Error JSP (error.jsp)

    Handle and display errors gracefully:

    <h1>Error Occurred</h1>
    <p style="color: red;">
        ${error}
    </p>
    <a href="input.jsp">Try Again</a>
                    

Performance Optimization Techniques

  • JSP Fragment Caching: Cache static portions of your JSP pages using:
    <jsp:include page="/fragments/header.jsp" flush="false"/>
                    
    This reduces compilation overhead for repeated requests.
  • Servlet Response Compression: Enable GZIP compression in your servlet:
    response.setHeader("Content-Encoding", "gzip");
                    
    This can reduce response sizes by 60-80% for text-based content.
  • Connection Pooling: Configure your server’s connection pool (example for Tomcat):
    <Resource name="jdbc/YourDB"
        auth="Container"
        type="javax.sql.DataSource"
        maxTotal="100"
        maxIdle="30"
        maxWaitMillis="10000"/>
                    
  • Asynchronous Processing: For long-running operations, use async servlets:
    @WebServlet(asyncSupported = true)
    public class AsyncServlet extends HttpServlet {
        protected void doGet(HttpServletRequest request,
                            HttpServletResponse response) {
            AsyncContext ctx = request.startAsync();
            ctx.start(() -> {
                // Long-running operation
                ctx.complete();
            });
        }
    }
                    

Security Considerations

Vulnerability Risk Level MVC Mitigation Strategy
Cross-Site Scripting (XSS) High Use JSTL <c:out> with escapeXml=”true” in JSPs
SQL Injection Critical Use PreparedStatement in model layer
Cross-Site Request Forgery (CSRF) Medium Include SyncTokenizer in forms
Information Leakage Medium Configure error.jsp to show generic messages

Advanced Patterns for Enterprise Applications

For more complex applications with three or more JSPs, consider these advanced patterns:

  1. Front Controller Pattern: Use a single servlet that delegates to command objects:
    public class FrontController extends HttpServlet {
        private Map<String, Command> commands = new HashMap<>();
    
        public void init() {
            commands.put("/calculate", new CalculateCommand());
            commands.put("/report", new ReportCommand());
        }
    
        protected void service(HttpServletRequest request,
                              HttpServletResponse response) {
            String path = request.getPathInfo();
            Command cmd = commands.get(path);
            if (cmd != null) {
                cmd.execute(request, response);
            }
        }
    }
                    
  2. View Helper Pattern: Move complex view logic to helper classes:
    public class ResultsHelper {
        public static String formatResult(double value) {
            return NumberFormat.getCurrencyInstance().format(value);
        }
    
        public static String getOperationSymbol(String op) {
            return Map.of("add", "+", "subtract", "-").get(op);
        }
    }
                    
    Then in results.jsp:
    <%= ResultsHelper.formatResult(result) %>
                    
  3. Composite View Pattern: Build views from multiple JSP fragments:
    <%-- main.jsp --%>
    <jsp:include page="${headerFragment}" />
    <jsp:include page="${bodyFragment}" />
    <jsp:include page="${footerFragment}" />
                    

Authoritative Resources

Performance Benchmarking Methodology

To accurately measure the performance of your three-JSP MVC application:

  1. Load Testing Setup
    • Use Apache JMeter or Gatling to simulate user load
    • Configure test plan with:
      • 100-1000 virtual users
      • Ramp-up period of 60 seconds
      • Loop count of 10-20 iterations
    • Test from multiple geographic locations if possible
  2. Key Metrics to Capture
    Metric Target Value Measurement Tool
    Response Time (90th percentile) < 500ms JMeter Summary Report
    Throughput (requests/sec) > 500 JMeter Aggregate Report
    Error Rate < 0.1% JMeter View Results Tree
    Heap Memory Usage < 500MB VisualVM or JConsole
    GC Pause Time < 100ms GC Logs with -Xloggc
  3. Optimization Iterations

    Follow this optimization cycle:

    1. Establish baseline metrics
    2. Identify top 3 bottlenecks (use profiling)
    3. Implement targeted optimizations
    4. Re-test and compare with baseline
    5. Document changes and results

Common Pitfalls and Solutions

Pitfall Symptoms Solution
Scriptlets in JSP Hard to maintain, mixing logic with presentation Use JSTL/EL, move logic to servlets or helpers
Overusing session Memory bloat, scalability issues Store only essential data, use short timeouts
Tight coupling Changes in one layer affect others Use interfaces, dependency injection
Ignoring HTTP methods Security vulnerabilities, unexpected behavior Properly implement doGet/doPost, use @WebServlet
No input validation Security risks, data corruption Validate in both client (JS) and server (Java)

Future Trends in Java Web Development

While the three-JSP-and-servlet pattern remains valuable for learning, modern Java web development is evolving:

  • Jakarta EE 10: The evolution of Java EE with improved MVC support through:
    • Enhanced Servlet 6.0 API
    • Better integration with CDI (Contexts and Dependency Injection)
    • Improved security APIs
  • Microservices Architecture: Breaking down MVC applications into:
    • Frontend services (React/Angular)
    • Backend microservices (Quarkus/Spring Boot)
    • API gateways for routing
  • Serverless Java: Running MVC components as:
    • AWS Lambda functions
    • Azure Functions
    • Google Cloud Run services
  • Reactive Programming: Using frameworks like:
    • Vert.x for event-driven architecture
    • Spring WebFlux for reactive MVC
    • RxJava for asynchronous data flows

However, the fundamental MVC principles learned through this three-JSP-and-servlet pattern remain applicable across all these modern architectures. The separation of concerns, clear responsibility division, and testability benefits continue to be valuable in any web application development approach.

Leave a Reply

Your email address will not be published. Required fields are marked *