diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/FrontControllerServlet.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/FrontControllerServlet.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/FrontControllerServlet.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/FrontControllerServlet.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/CheckoutCommand.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/CheckoutCommand.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/CheckoutCommand.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/CheckoutCommand.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/FrontCommand.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/FrontCommand.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/FrontCommand.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/FrontCommand.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/HomeCommand.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/HomeCommand.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/HomeCommand.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/HomeCommand.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/LoginCommand.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/LoginCommand.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/LoginCommand.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/LoginCommand.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/LogoutCommand.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/LogoutCommand.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/LogoutCommand.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/LogoutCommand.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/OrderCommand.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/OrderCommand.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/OrderCommand.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/OrderCommand.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/SearchCommand.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/SearchCommand.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/SearchCommand.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/SearchCommand.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/ShowCommand.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/ShowCommand.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/ShowCommand.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/ShowCommand.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/UnknownCommand.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/UnknownCommand.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/UnknownCommand.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/commands/UnknownCommand.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Book.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/Book.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Book.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/Book.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/BookImpl.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/BookImpl.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/BookImpl.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/BookImpl.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Bookshelf.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/Bookshelf.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Bookshelf.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/Bookshelf.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/BookshelfImpl.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/BookshelfImpl.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/BookshelfImpl.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/BookshelfImpl.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Order.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/Order.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Order.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/Order.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/OrderImpl.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/OrderImpl.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/OrderImpl.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/data/OrderImpl.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/AuthenticationFilter.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/AuthenticationFilter.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/AuthenticationFilter.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/AuthenticationFilter.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/BaseFilter.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/BaseFilter.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/BaseFilter.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/BaseFilter.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/EncodingFilter.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/EncodingFilter.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/EncodingFilter.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/EncodingFilter.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/FilterChainImpl.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/FilterChainImpl.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/FilterChainImpl.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/FilterChainImpl.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/FilterManager.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/FilterManager.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/FilterManager.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/FilterManager.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/LoggingFilter.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/LoggingFilter.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/LoggingFilter.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/LoggingFilter.java diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/OnIntercept.java b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/OnIntercept.java similarity index 100% rename from enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/filters/OnIntercept.java rename to enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/patterns/filters/OnIntercept.java diff --git a/patterns/pom.xml b/patterns/pom.xml index 7c23b6f55d..1b7340304b 100644 --- a/patterns/pom.xml +++ b/patterns/pom.xml @@ -4,7 +4,13 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung.enterprise.patterns + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + com.baeldung.patterns patterns war @@ -15,14 +21,14 @@ 3.1.0 provided + + org.slf4j + slf4j-api + 1.7.21 + provided + - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - @@ -34,13 +40,21 @@ 1.8 + + org.apache.maven.plugins + maven-war-plugin + 3.0.0 + + false + + org.eclipse.jetty jetty-maven-plugin - 9.4.0.M1 + 9.3.11.v20160721 - /front-controller + / diff --git a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/FrontControllerServlet.java b/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/FrontControllerServlet.java deleted file mode 100644 index a8962f5108..0000000000 --- a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/FrontControllerServlet.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.enterprise.patterns.front.controller; - -import com.baeldung.enterprise.patterns.front.controller.commands.FrontCommand; -import com.baeldung.enterprise.patterns.front.controller.commands.UnknownCommand; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -public class FrontControllerServlet extends HttpServlet { - @Override - protected void doGet( - HttpServletRequest request, - HttpServletResponse response - ) throws ServletException, IOException { - FrontCommand command = getCommand(request); - command.init(getServletContext(), request, response); - command.process(); - } - - private FrontCommand getCommand(HttpServletRequest request) { - try { - Class type = Class.forName( - String.format( - "com.baeldung.enterprise.patterns.front.controller.commands.%sCommand", - request.getParameter("command") - ) - ); - return (FrontCommand) type - .asSubclass(FrontCommand.class) - .newInstance(); - } catch (Exception e) { - return new UnknownCommand(); - } - } -} diff --git a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/FrontCommand.java b/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/FrontCommand.java deleted file mode 100644 index 12a008faeb..0000000000 --- a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/FrontCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.enterprise.patterns.front.controller.commands; - -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - -public abstract class FrontCommand { - protected ServletContext context; - protected HttpServletRequest request; - protected HttpServletResponse response; - - public void init( - ServletContext servletContext, - HttpServletRequest servletRequest, - HttpServletResponse servletResponse - ) { - this.context = servletContext; - this.request = servletRequest; - this.response = servletResponse; - } - - public abstract void process() throws ServletException, IOException; - - protected void forward(String target) throws ServletException, IOException { - target = String.format("/WEB-INF/jsp/%s.jsp", target); - RequestDispatcher dispatcher = context.getRequestDispatcher(target); - dispatcher.forward(request, response); - } -} diff --git a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/SearchCommand.java b/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/SearchCommand.java deleted file mode 100644 index 0c5bd64bbc..0000000000 --- a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/SearchCommand.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.enterprise.patterns.front.controller.commands; - -import com.baeldung.enterprise.patterns.front.controller.data.Book; -import com.baeldung.enterprise.patterns.front.controller.data.BookshelfImpl; - -import javax.servlet.ServletException; -import java.io.IOException; - -public class SearchCommand extends FrontCommand { - @Override - public void process() throws ServletException, IOException { - Book book = new BookshelfImpl().getInstance() - .findByTitle(request.getParameter("title")); - if (book != null) { - request.setAttribute("book", book); - forward("book-found"); - } else { - forward("book-notfound"); - } - } -} diff --git a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Bookshelf.java b/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Bookshelf.java deleted file mode 100644 index 524e000bd9..0000000000 --- a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Bookshelf.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.enterprise.patterns.front.controller.data; - -public interface Bookshelf { - - default void init() { - add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99)); - add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88)); - } - - Bookshelf getInstance(); - - boolean add(E book); - - Book findByTitle(String title); -} diff --git a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/BookshelfImpl.java b/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/BookshelfImpl.java deleted file mode 100644 index 3862418857..0000000000 --- a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/BookshelfImpl.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.enterprise.patterns.front.controller.data; - -import java.util.ArrayList; - -public class BookshelfImpl extends ArrayList implements Bookshelf { - private static Bookshelf INSTANCE; - - @Override - public Bookshelf getInstance() { - if (INSTANCE == null) { - INSTANCE = new BookshelfImpl(); - INSTANCE.init(); - } - return INSTANCE; - } - - @Override - public Book findByTitle(String title) { - return this.stream() - .filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase())) - .findFirst() - .orElse(null); - } -} diff --git a/patterns/src/main/java/com/baeldung/patterns/FrontControllerServlet.java b/patterns/src/main/java/com/baeldung/patterns/FrontControllerServlet.java new file mode 100644 index 0000000000..beba1d66e5 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/FrontControllerServlet.java @@ -0,0 +1,66 @@ +package com.baeldung.patterns; + +import com.baeldung.patterns.commands.FrontCommand; +import com.baeldung.patterns.commands.UnknownCommand; +import com.baeldung.patterns.data.Bookshelf; +import com.baeldung.patterns.data.BookshelfImpl; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet(name = "front-controller", urlPatterns = "/index") +public class FrontControllerServlet extends HttpServlet { + @Override + public void init(ServletConfig config) throws ServletException { + super.init(config); + Bookshelf bookshelf = new BookshelfImpl(); + bookshelf.init(); + getServletContext().setAttribute("bookshelf", bookshelf); + } + + @Override + protected void doGet( + HttpServletRequest request, + HttpServletResponse response + ) throws ServletException, IOException { + doCommand(request, response); + } + + @Override + protected void doPost( + HttpServletRequest request, + HttpServletResponse response + ) throws ServletException, IOException { + doCommand(request, response); + } + + private void doCommand( + HttpServletRequest request, + HttpServletResponse response + ) throws ServletException, IOException { + FrontCommand command = getCommand(request); + command.init(request, response); + command.process(); + } + + private FrontCommand getCommand(HttpServletRequest request) { + try { + Class type = Class.forName( + String.format( + "com.baeldung.patterns.commands.%sCommand", + request.getParameter("command") + ) + ); + return (FrontCommand) type + .asSubclass(FrontCommand.class) + .newInstance(); + } catch (Exception e) { + return new UnknownCommand(); + } + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/commands/CheckoutCommand.java b/patterns/src/main/java/com/baeldung/patterns/commands/CheckoutCommand.java new file mode 100644 index 0000000000..be6dbe84bd --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/commands/CheckoutCommand.java @@ -0,0 +1,27 @@ +package com.baeldung.patterns.commands; + +import com.baeldung.patterns.data.Order; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +public class CheckoutCommand extends FrontCommand { + @Override + public void process() throws ServletException, IOException { + super.process(); + HttpSession session = request.getSession(false); + if (request.getMethod().equals("POST")) { + session.removeAttribute("order"); + response.sendRedirect("index?command=Home&message=Thank you for buying!"); + } else { + Order order = (Order) session.getAttribute("order"); + Double total = order.getItems().entrySet().stream() + .map(entry -> entry.getKey().getPrice() * entry.getValue()) + .reduce((p1, p2) -> p1 + p2) + .orElse(0.00); + request.setAttribute("total", total); + forward("shopping-cart"); + } + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/commands/FrontCommand.java b/patterns/src/main/java/com/baeldung/patterns/commands/FrontCommand.java new file mode 100644 index 0000000000..efc0ee4612 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/commands/FrontCommand.java @@ -0,0 +1,43 @@ +package com.baeldung.patterns.commands; + +import com.baeldung.patterns.filters.FilterManager; +import com.baeldung.patterns.filters.OnIntercept; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public abstract class FrontCommand implements OnIntercept { + protected HttpServletRequest request; + protected HttpServletResponse response; + private boolean intercept; + + public FrontCommand() { + } + + public void init(HttpServletRequest request, HttpServletResponse response) { + this.request = request; + this.response = response; + } + + public void process() throws ServletException, IOException { + FilterManager.process(request, response, this); + } + + public void forward(String target) throws ServletException, IOException { + if (intercept) { + return; + } + String path = String.format("/WEB-INF/jsp/%s.jsp", target); + RequestDispatcher dispatcher = request.getServletContext() + .getRequestDispatcher(path); + dispatcher.forward(request, response); + } + + @Override + public void intercept() { + intercept = true; + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/commands/HomeCommand.java b/patterns/src/main/java/com/baeldung/patterns/commands/HomeCommand.java new file mode 100644 index 0000000000..f2358400b9 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/commands/HomeCommand.java @@ -0,0 +1,17 @@ +package com.baeldung.patterns.commands; + +import com.baeldung.patterns.data.Bookshelf; + +import javax.servlet.ServletException; +import java.io.IOException; + +public class HomeCommand extends FrontCommand { + @Override + public void process() throws ServletException, IOException { + super.process(); + Bookshelf bookshelf = (Bookshelf) request.getServletContext() + .getAttribute("bookshelf"); + request.setAttribute("books", bookshelf); + forward("home"); + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/commands/LoginCommand.java b/patterns/src/main/java/com/baeldung/patterns/commands/LoginCommand.java new file mode 100644 index 0000000000..1a7795eb3c --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/commands/LoginCommand.java @@ -0,0 +1,24 @@ +package com.baeldung.patterns.commands; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +public class LoginCommand extends FrontCommand { + @Override + public void process() throws ServletException, IOException { + if (request.getMethod().equals("POST")) { + HttpSession session = request.getSession(true); + session.setAttribute("username", request.getParameter("username")); + response.sendRedirect(request.getParameter("redirect")); + } else { + String queryString = request.getQueryString(); + if (queryString == null) { + queryString = "command=Home"; + } + request.setAttribute("redirect", request.getRequestURL() + .append("?").append(queryString).toString()); + forward("login"); + } + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/commands/LogoutCommand.java b/patterns/src/main/java/com/baeldung/patterns/commands/LogoutCommand.java new file mode 100644 index 0000000000..82f4e753c9 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/commands/LogoutCommand.java @@ -0,0 +1,16 @@ +package com.baeldung.patterns.commands; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +public class LogoutCommand extends FrontCommand { + @Override + public void process() throws ServletException, IOException { + super.process(); + HttpSession session = request.getSession(false); + session.removeAttribute("username"); + session.removeAttribute("order"); + response.sendRedirect("index?command=Home"); + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/commands/OrderCommand.java b/patterns/src/main/java/com/baeldung/patterns/commands/OrderCommand.java new file mode 100644 index 0000000000..5926dfddb5 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/commands/OrderCommand.java @@ -0,0 +1,33 @@ +package com.baeldung.patterns.commands; + +import com.baeldung.patterns.data.Book; +import com.baeldung.patterns.data.Bookshelf; +import com.baeldung.patterns.data.Order; +import com.baeldung.patterns.data.OrderImpl; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +public class OrderCommand extends FrontCommand { + @Override + public void process() throws ServletException, IOException { + super.process(); + if (request.getMethod().equals("POST")) { + HttpSession session = request.getSession(false); + Order order = (Order) session.getAttribute("order"); + if (order == null) { + String username = (String) session.getAttribute("username"); + order = new OrderImpl(username); + } + Bookshelf bookshelf = (Bookshelf) request.getServletContext() + .getAttribute("bookshelf"); + String isbn = request.getParameter("isbn"); + Integer quantity = Integer.parseInt(request.getParameter("quantity")); + Book book = bookshelf.get(isbn); + order.add(book, quantity); + session.setAttribute("order", order); + response.sendRedirect(String.format("index?command=Show&isbn=%s", isbn)); + } + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/commands/SearchCommand.java b/patterns/src/main/java/com/baeldung/patterns/commands/SearchCommand.java new file mode 100644 index 0000000000..30e401e83a --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/commands/SearchCommand.java @@ -0,0 +1,25 @@ +package com.baeldung.patterns.commands; + +import com.baeldung.patterns.data.Book; +import com.baeldung.patterns.data.Bookshelf; + +import javax.servlet.ServletException; +import java.io.IOException; +import java.util.List; + +public class SearchCommand extends FrontCommand { + @Override + public void process() throws ServletException, IOException { + super.process(); + Bookshelf bookshelf = (Bookshelf) request.getServletContext() + .getAttribute("bookshelf"); + String q = request.getParameter("q"); + List books = bookshelf.find(q); + if (books.size() > 0) { + request.setAttribute("books", books); + forward("book-found"); + } else { + forward("book-notfound"); + } + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/commands/ShowCommand.java b/patterns/src/main/java/com/baeldung/patterns/commands/ShowCommand.java new file mode 100644 index 0000000000..3226d9969f --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/commands/ShowCommand.java @@ -0,0 +1,21 @@ +package com.baeldung.patterns.commands; + +import com.baeldung.patterns.data.Book; +import com.baeldung.patterns.data.Bookshelf; + +import javax.servlet.ServletException; +import java.io.IOException; +import java.util.Collections; + +public class ShowCommand extends FrontCommand { + @Override + public void process() throws ServletException, IOException { + super.process(); + Bookshelf bookshelf = (Bookshelf) request.getServletContext() + .getAttribute("bookshelf"); + String title = request.getParameter("isbn"); + Book book = bookshelf.get(title); + request.setAttribute("books", Collections.singletonList(book)); + forward("book-found"); + } +} diff --git a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/UnknownCommand.java b/patterns/src/main/java/com/baeldung/patterns/commands/UnknownCommand.java similarity index 77% rename from patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/UnknownCommand.java rename to patterns/src/main/java/com/baeldung/patterns/commands/UnknownCommand.java index 90103c8f42..71634e5263 100644 --- a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/UnknownCommand.java +++ b/patterns/src/main/java/com/baeldung/patterns/commands/UnknownCommand.java @@ -1,4 +1,4 @@ -package com.baeldung.enterprise.patterns.front.controller.commands; +package com.baeldung.patterns.commands; import javax.servlet.ServletException; import java.io.IOException; @@ -6,6 +6,7 @@ import java.io.IOException; public class UnknownCommand extends FrontCommand { @Override public void process() throws ServletException, IOException { + super.process(); forward("unknown"); } } diff --git a/patterns/src/main/java/com/baeldung/patterns/data/Book.java b/patterns/src/main/java/com/baeldung/patterns/data/Book.java new file mode 100644 index 0000000000..ab417aa93b --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/data/Book.java @@ -0,0 +1,19 @@ +package com.baeldung.patterns.data; + +public interface Book { + String getIsbn(); + + void setIsbn(String isbn); + + String getAuthor(); + + void setAuthor(String author); + + String getTitle(); + + void setTitle(String title); + + Double getPrice(); + + void setPrice(Double price); +} diff --git a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Book.java b/patterns/src/main/java/com/baeldung/patterns/data/BookImpl.java similarity index 55% rename from patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Book.java rename to patterns/src/main/java/com/baeldung/patterns/data/BookImpl.java index 634e05c3a0..adecfd1d3a 100644 --- a/patterns/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Book.java +++ b/patterns/src/main/java/com/baeldung/patterns/data/BookImpl.java @@ -1,39 +1,57 @@ -package com.baeldung.enterprise.patterns.front.controller.data; +package com.baeldung.patterns.data; -public class Book { +public class BookImpl implements Book { + private String isbn; private String author; private String title; private Double price; - public Book() { + public BookImpl() { } - public Book(String author, String title, Double price) { + public BookImpl(String isbn, String author, String title, Double price) { + this.isbn = isbn; this.author = author; this.title = title; this.price = price; } + @Override + public String getIsbn() { + return isbn; + } + + @Override + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + @Override public String getAuthor() { return author; } + @Override public void setAuthor(String author) { this.author = author; } + @Override public String getTitle() { return title; } + @Override public void setTitle(String title) { this.title = title; } + @Override public Double getPrice() { return price; } + @Override public void setPrice(Double price) { this.price = price; } diff --git a/patterns/src/main/java/com/baeldung/patterns/data/Bookshelf.java b/patterns/src/main/java/com/baeldung/patterns/data/Bookshelf.java new file mode 100644 index 0000000000..66a1da9272 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/data/Bookshelf.java @@ -0,0 +1,18 @@ +package com.baeldung.patterns.data; + +import java.util.List; + +public interface Bookshelf { + + default void init() { + add(new BookImpl("001", "Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99)); + add(new BookImpl("002", "Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88)); + add(new BookImpl("003", "Unknown", "Something about German Umlauts (äüö) and ß", 5.49)); + } + + boolean add(E book); + + Book get(String isbn); + + List find(String q); +} diff --git a/patterns/src/main/java/com/baeldung/patterns/data/BookshelfImpl.java b/patterns/src/main/java/com/baeldung/patterns/data/BookshelfImpl.java new file mode 100644 index 0000000000..a7cbda140d --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/data/BookshelfImpl.java @@ -0,0 +1,23 @@ +package com.baeldung.patterns.data; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class BookshelfImpl extends ArrayList implements Bookshelf { + @Override + public Book get(String isbn) { + return this.stream() + .filter(book -> book.getIsbn().equals(isbn)) + .findFirst() + .orElse(null); + } + + @Override + public List find(String q) { + return this.stream() + .filter(book -> book.getTitle().toLowerCase().contains(q.toLowerCase()) + || book.getAuthor().toLowerCase().contains(q.toLowerCase())) + .collect(Collectors.toList()); + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/data/Order.java b/patterns/src/main/java/com/baeldung/patterns/data/Order.java new file mode 100644 index 0000000000..6dc67e4e58 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/data/Order.java @@ -0,0 +1,11 @@ +package com.baeldung.patterns.data; + +import java.util.Map; + +public interface Order { + String getUsername(); + + Map getItems(); + + void add(Book item, Integer quantity); +} diff --git a/patterns/src/main/java/com/baeldung/patterns/data/OrderImpl.java b/patterns/src/main/java/com/baeldung/patterns/data/OrderImpl.java new file mode 100644 index 0000000000..73232ab11c --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/data/OrderImpl.java @@ -0,0 +1,40 @@ +package com.baeldung.patterns.data; + +import java.util.HashMap; +import java.util.Map; + +public class OrderImpl implements Order { + private String username; + private Map items = new HashMap<>(); + + public OrderImpl(String username) { + this.username = username; + } + + @Override + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + @Override + public Map getItems() { + return items; + } + + public void setItems(Map items) { + this.items = items; + } + + @Override + public void add(Book item, Integer quantity) { + Integer q = 0; + if (this.items.containsKey(item)) { + q = this.items.get(item); + } + this.items.put(item, quantity + q); + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/filters/AuthenticationFilter.java b/patterns/src/main/java/com/baeldung/patterns/filters/AuthenticationFilter.java new file mode 100644 index 0000000000..fa7655434e --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/filters/AuthenticationFilter.java @@ -0,0 +1,40 @@ +package com.baeldung.patterns.filters; + +import com.baeldung.patterns.commands.FrontCommand; +import com.baeldung.patterns.commands.LoginCommand; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +public class AuthenticationFilter extends BaseFilter { + private OnIntercept callback; + + public AuthenticationFilter(OnIntercept callback) { + this.callback = callback; + } + + @Override + public void doFilter( + ServletRequest request, + ServletResponse response, + FilterChain chain + ) throws IOException, ServletException { + HttpServletRequest httpServletRequest = (HttpServletRequest) request; + HttpServletResponse httpServletResponse = (HttpServletResponse) response; + HttpSession session = httpServletRequest.getSession(false); + if (session == null || session.getAttribute("username") == null) { + callback.intercept(); + FrontCommand command = new LoginCommand(); + command.init(httpServletRequest, httpServletResponse); + command.process(); + } else { + chain.doFilter(request, response); + } + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/filters/BaseFilter.java b/patterns/src/main/java/com/baeldung/patterns/filters/BaseFilter.java new file mode 100644 index 0000000000..670039a62c --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/filters/BaseFilter.java @@ -0,0 +1,25 @@ +package com.baeldung.patterns.filters; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.Filter; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; + +public abstract class BaseFilter implements Filter { + private static final Logger log = LoggerFactory.getLogger(BaseFilter.class); + + protected FilterConfig filterConfig; + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + log.info("Initialize filter: {}", getClass().getSimpleName()); + this.filterConfig = filterConfig; + } + + @Override + public void destroy() { + log.info("Destroy filter: {}", getClass().getSimpleName()); + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/filters/EncodingFilter.java b/patterns/src/main/java/com/baeldung/patterns/filters/EncodingFilter.java new file mode 100644 index 0000000000..1277ca726d --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/filters/EncodingFilter.java @@ -0,0 +1,30 @@ +package com.baeldung.patterns.filters; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import java.io.IOException; + +@WebFilter(servletNames = "front-controller") +public class EncodingFilter extends BaseFilter { + private String encoding; + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + super.init(filterConfig); + this.encoding = filterConfig.getInitParameter("encoding"); + } + + @Override + public void doFilter( + ServletRequest request, + ServletResponse response, + FilterChain chain + ) throws IOException, ServletException { + String encoding = request.getParameter("encoding"); + if (encoding == null) { + encoding = this.encoding; + } + response.setCharacterEncoding(encoding); + chain.doFilter(request, response); + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/filters/FilterChainImpl.java b/patterns/src/main/java/com/baeldung/patterns/filters/FilterChainImpl.java new file mode 100644 index 0000000000..d2b64c7d06 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/filters/FilterChainImpl.java @@ -0,0 +1,28 @@ +package com.baeldung.patterns.filters; + +import javax.servlet.Filter; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import java.io.IOException; +import java.util.Arrays; +import java.util.Iterator; + +public class FilterChainImpl implements javax.servlet.FilterChain { + private Iterator filters; + + public FilterChainImpl(Filter... filters) { + this.filters = Arrays.asList(filters).iterator(); + } + + @Override + public void doFilter( + ServletRequest request, + ServletResponse response + ) throws IOException, ServletException { + if (filters.hasNext()) { + Filter filter = filters.next(); + filter.doFilter(request, response, this); + } + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/filters/FilterManager.java b/patterns/src/main/java/com/baeldung/patterns/filters/FilterManager.java new file mode 100644 index 0000000000..ae945107fd --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/filters/FilterManager.java @@ -0,0 +1,21 @@ +package com.baeldung.patterns.filters; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class FilterManager { + public static void process( + HttpServletRequest request, + HttpServletResponse response, + OnIntercept callback + ) throws ServletException, IOException { + FilterChain filterChain = new FilterChainImpl( + new AuthenticationFilter(callback), + new VisitorCounterFilter() + ); + filterChain.doFilter(request, response); + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/filters/LoggingFilter.java b/patterns/src/main/java/com/baeldung/patterns/filters/LoggingFilter.java new file mode 100644 index 0000000000..f6ffc20585 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/filters/LoggingFilter.java @@ -0,0 +1,33 @@ +package com.baeldung.patterns.filters; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + +@WebFilter(servletNames = "front-controller") +public class LoggingFilter extends BaseFilter { + private static final Logger log = LoggerFactory.getLogger(LoggingFilter.class); + + @Override + public void doFilter( + ServletRequest request, + ServletResponse response, + FilterChain chain + ) throws IOException, ServletException { + chain.doFilter(request, response); + HttpServletRequest httpServletRequest = (HttpServletRequest) request; + String username = (String) httpServletRequest.getAttribute("username"); + if (username == null) { + username = "guest"; + } + log.info("Request from '{}@{}': {}?{}", username, request.getRemoteAddr(), + httpServletRequest.getRequestURI(), request.getParameterMap()); + } +} diff --git a/patterns/src/main/java/com/baeldung/patterns/filters/OnIntercept.java b/patterns/src/main/java/com/baeldung/patterns/filters/OnIntercept.java new file mode 100644 index 0000000000..73be3509f0 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/filters/OnIntercept.java @@ -0,0 +1,5 @@ +package com.baeldung.patterns.filters; + +public interface OnIntercept { + void intercept(); +} diff --git a/patterns/src/main/java/com/baeldung/patterns/filters/VisitorCounterFilter.java b/patterns/src/main/java/com/baeldung/patterns/filters/VisitorCounterFilter.java new file mode 100644 index 0000000000..0abfac7cd6 --- /dev/null +++ b/patterns/src/main/java/com/baeldung/patterns/filters/VisitorCounterFilter.java @@ -0,0 +1,30 @@ +package com.baeldung.patterns.filters; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; + +public class VisitorCounterFilter extends BaseFilter { + private static Set users = new HashSet<>(); + + @Override + public void doFilter( + ServletRequest request, + ServletResponse response, + FilterChain chain + ) throws IOException, ServletException { + HttpSession session = ((HttpServletRequest) request).getSession(false); + String username = (String) session.getAttribute("username"); + if (!users.contains(username)) { + users.add(username); + } + request.setAttribute("counter", users.size()); + chain.doFilter(request, response); + } +} diff --git a/patterns/src/main/webapp/WEB-INF/jsp/book-found.jsp b/patterns/src/main/webapp/WEB-INF/jsp/book-found.jsp index 42e08b4a46..95a74b9e2e 100644 --- a/patterns/src/main/webapp/WEB-INF/jsp/book-found.jsp +++ b/patterns/src/main/webapp/WEB-INF/jsp/book-found.jsp @@ -1,12 +1,24 @@ +<%@ page import="com.baeldung.patterns.data.Book" %> +<%@ page import="java.util.List" %> Bookshelf: Title found -

Our Bookshelf contains this title:

-

${book.getTitle()}

-

Author: ${book.getAuthor()}

- +

Our Bookshelf contains following titles:

+ <% for (Book book : (List) request.getAttribute("books")) { %> +

<%= book.getTitle() %>

+

Author: <%= book.getAuthor() %>

+
+ + + + + Go back... +
+ <% } %> + <%@ include file="shopping-cart-hint.jsp"%> + <%@ include file="visitor-counter.jsp"%> diff --git a/patterns/src/main/webapp/WEB-INF/jsp/book-notfound.jsp b/patterns/src/main/webapp/WEB-INF/jsp/book-notfound.jsp index 2f8ac01755..284b1e4759 100644 --- a/patterns/src/main/webapp/WEB-INF/jsp/book-notfound.jsp +++ b/patterns/src/main/webapp/WEB-INF/jsp/book-notfound.jsp @@ -1,10 +1,11 @@ - - Bookshelf: Title not found - + + Bookshelf: Title not found +

Our Bookshelf doesn't contains this title:

-

${param.get("title")}

+

<%= request.getParameter("q") %>

+ <%@ include file="visitor-counter.jsp"%> diff --git a/patterns/src/main/webapp/WEB-INF/jsp/home.jsp b/patterns/src/main/webapp/WEB-INF/jsp/home.jsp new file mode 100644 index 0000000000..e7bf1562ea --- /dev/null +++ b/patterns/src/main/webapp/WEB-INF/jsp/home.jsp @@ -0,0 +1,27 @@ +<%@ page import="com.baeldung.patterns.data.Book" %> +<%@ page import="java.util.List" %> + + + + Bookshelf: Home + + +
+ + +
+ <% if (request.getParameter("message") != null) { %> +

<%= request.getParameter("message") %>

+ <% } else { %> +

Welcome to the Bookshelf!

+ <% } %> + <% for (Book book : (List) request.getAttribute("books")) { %> +
+

<%= book.getAuthor() %>:

+

<%= book.getTitle() %>

+ More... + <% } %> + <%@ include file="shopping-cart-hint.jsp"%> + <%@ include file="visitor-counter.jsp"%> + + diff --git a/patterns/src/main/webapp/WEB-INF/jsp/login.jsp b/patterns/src/main/webapp/WEB-INF/jsp/login.jsp new file mode 100644 index 0000000000..6983fba949 --- /dev/null +++ b/patterns/src/main/webapp/WEB-INF/jsp/login.jsp @@ -0,0 +1,16 @@ + + + + Bookshelf: Login + + +

Please input a username:

+

Login

+
+ + "> + +
+ <%@ include file="visitor-counter.jsp" %> + + diff --git a/patterns/src/main/webapp/WEB-INF/jsp/shopping-cart-hint.jsp b/patterns/src/main/webapp/WEB-INF/jsp/shopping-cart-hint.jsp new file mode 100644 index 0000000000..9173c35711 --- /dev/null +++ b/patterns/src/main/webapp/WEB-INF/jsp/shopping-cart-hint.jsp @@ -0,0 +1,16 @@ +<%@ page import="com.baeldung.patterns.data.Order" %> +<% if (session != null && session.getAttribute("order") != null) { %> + <% Order order = ((Order) session.getAttribute("order")); %> + <% if (order != null && order.getItems().size() > 0) { %> +
+

+ Your shopping cart is holding + <% if (order.getItems().size() == 1) { %> + 1 item. + <% } else { %> + <%= (order.getItems().size()) %> items. + <% } %> + Checkout +

+ <% } %> +<% } %> diff --git a/patterns/src/main/webapp/WEB-INF/jsp/shopping-cart.jsp b/patterns/src/main/webapp/WEB-INF/jsp/shopping-cart.jsp new file mode 100644 index 0000000000..024a3bac3e --- /dev/null +++ b/patterns/src/main/webapp/WEB-INF/jsp/shopping-cart.jsp @@ -0,0 +1,29 @@ +<%@ page import="com.baeldung.patterns.data.Book" %> +<%@ page import="com.baeldung.patterns.data.Order" %> +<%@ page import="java.util.Map" %> + + + + Bookshelf: Checkout + + +

You are about to buy the following books:

+

Shopping Cart

+ <% Order order = (Order) session.getAttribute("order"); %> +
    + <% for (Map.Entry entry : order.getItems().entrySet()) { %> +
  • + <%= entry.getValue() %> x <%= entry.getKey().getPrice() %> +

    <%= entry.getKey().getTitle() %>

    + by <%= entry.getKey().getAuthor()%> +
  • + <% } %> +
+

+ Total: <%= request.getAttribute("total") %> +

+
+ +
+ + diff --git a/patterns/src/main/webapp/WEB-INF/jsp/visitor-counter.jsp b/patterns/src/main/webapp/WEB-INF/jsp/visitor-counter.jsp new file mode 100644 index 0000000000..397364e2b3 --- /dev/null +++ b/patterns/src/main/webapp/WEB-INF/jsp/visitor-counter.jsp @@ -0,0 +1,5 @@ +<% Integer counter = (Integer) request.getAttribute("counter"); %> +<% if (counter != null && counter > 0) { %> +
+

You are visitor #<%= counter %>. Logout

+<% } %> diff --git a/patterns/src/main/webapp/WEB-INF/web.xml b/patterns/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 77113db09b..0000000000 --- a/patterns/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - front-controller - com.baeldung.enterprise.patterns.front.controller.FrontControllerServlet - - - front-controller - / - -