Repository: iluwatar/java-design-patterns
Issue: Composite View refactoring needed
Labels: info: good first issue
type: refactoring

Servlets are components in Java web development, responsible for processing HTTP requests and generating responses. In this context, exceptions are used to handle and manage unexpected errors or exceptional conditions that may occur during the execution of a servlet.

Exceptions should not be thrown from servlet methods. These 4 hotspots need to be fixed:

AppServlet.java:64
AppServlet.java:71
AppServlet.java:81
AppServlet.java:90


Acceptance criteria

- The aforementioned exceptions have been caught properly

Issue comments:
assign it to me!! I resolved it locally
Hi @iluwatar I have made the pr. ps review it. #2886 

Pull request review comments:

Patch that resolved the issue:
diff --git a/composite-view/src/main/java/com/iluwatar/compositeview/AppServlet.java b/composite-view/src/main/java/com/iluwatar/compositeview/AppServlet.java
index 0cef25d2dd22..d2ec90675eb5 100644
--- a/composite-view/src/main/java/com/iluwatar/compositeview/AppServlet.java
+++ b/composite-view/src/main/java/com/iluwatar/compositeview/AppServlet.java
@@ -25,18 +25,17 @@
 package com.iluwatar.compositeview;
 
 import jakarta.servlet.RequestDispatcher;
-import jakarta.servlet.ServletException;
 import jakarta.servlet.http.HttpServlet;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
-import java.io.IOException;
 import java.io.PrintWriter;
+import lombok.extern.slf4j.Slf4j;
 
 /**
  * A servlet object that extends HttpServlet.
  * Runs on Tomcat 10 and handles Http requests
  */
-
+@Slf4j
 public final class AppServlet extends HttpServlet {
   private static final String CONTENT_TYPE = "text/html";
   private String msgPartOne = "<h1>This Server Doesn't Support";
@@ -56,39 +55,44 @@ public AppServlet() {
   }
 
   @Override
-  public void doGet(HttpServletRequest req, HttpServletResponse resp)
-          throws ServletException, IOException {
-    RequestDispatcher requestDispatcher = req.getRequestDispatcher(destination);
-    ClientPropertiesBean reqParams = new ClientPropertiesBean(req);
-    req.setAttribute("properties", reqParams);
-    requestDispatcher.forward(req, resp);
+  public void doGet(HttpServletRequest req, HttpServletResponse resp) {
+    try {
+      RequestDispatcher requestDispatcher = req.getRequestDispatcher(destination);
+      ClientPropertiesBean reqParams = new ClientPropertiesBean(req);
+      req.setAttribute("properties", reqParams);
+      requestDispatcher.forward(req, resp);
+    } catch (Exception e) {
+      LOGGER.error("Exception occurred GET request processing ", e);
+    }
   }
 
   @Override
-  public void doPost(HttpServletRequest req, HttpServletResponse resp)
-          throws ServletException, IOException {
+  public void doPost(HttpServletRequest req, HttpServletResponse resp) {
     resp.setContentType(CONTENT_TYPE);
     try (PrintWriter out = resp.getWriter()) {
       out.println(msgPartOne + " Post " + msgPartTwo);
+    } catch (Exception e) {
+      LOGGER.error("Exception occurred POST request processing ", e);
     }
-
   }
 
   @Override
-  public void doDelete(HttpServletRequest req, HttpServletResponse resp)
-          throws ServletException, IOException {
+  public void doDelete(HttpServletRequest req, HttpServletResponse resp) {
     resp.setContentType(CONTENT_TYPE);
     try (PrintWriter out = resp.getWriter()) {
       out.println(msgPartOne + " Delete " + msgPartTwo);
+    } catch (Exception e) {
+      LOGGER.error("Exception occurred DELETE request processing ", e);
     }
   }
 
   @Override
-  public void doPut(HttpServletRequest req, HttpServletResponse resp)
-          throws ServletException, IOException {
+  public void doPut(HttpServletRequest req, HttpServletResponse resp) {
     resp.setContentType(CONTENT_TYPE);
     try (PrintWriter out = resp.getWriter()) {
       out.println(msgPartOne + " Put " + msgPartTwo);
+    } catch (Exception e) {
+      LOGGER.error("Exception occurred PUT request processing ", e);
     }
   }
 }


Test patch: