diff --git a/enterprise-patterns/intercepting-filter-pattern/pom.xml b/enterprise-patterns/intercepting-filter-pattern/pom.xml index ffe597a813..0e1008657a 100644 --- a/enterprise-patterns/intercepting-filter-pattern/pom.xml +++ b/enterprise-patterns/intercepting-filter-pattern/pom.xml @@ -37,7 +37,7 @@ org.eclipse.jetty jetty-maven-plugin - 9.3.11.v20160721 + 9.4.0.M1 /intercepting-filter 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/enterprise/patterns/front/controller/FrontControllerServlet.java index 728caca27e..67400897c4 100644 --- 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/enterprise/patterns/front/controller/FrontControllerServlet.java @@ -2,7 +2,10 @@ 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 com.baeldung.enterprise.patterns.front.controller.data.Bookshelf; +import com.baeldung.enterprise.patterns.front.controller.data.BookshelfImpl; +import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -10,8 +13,16 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -@WebServlet(name = "front-controller", urlPatterns = "/") +@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, 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/enterprise/patterns/front/controller/commands/CheckoutCommand.java new file mode 100644 index 0000000000..876e439139 --- /dev/null +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/CheckoutCommand.java @@ -0,0 +1,27 @@ +package com.baeldung.enterprise.patterns.front.controller.commands; + +import com.baeldung.enterprise.patterns.front.controller.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/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/enterprise/patterns/front/controller/commands/HomeCommand.java new file mode 100644 index 0000000000..49b62a29bd --- /dev/null +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/HomeCommand.java @@ -0,0 +1,17 @@ +package com.baeldung.enterprise.patterns.front.controller.commands; + +import com.baeldung.enterprise.patterns.front.controller.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/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/enterprise/patterns/front/controller/commands/LoginCommand.java index 719605108e..beef42d269 100644 --- 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/enterprise/patterns/front/controller/commands/LoginCommand.java @@ -7,14 +7,14 @@ import java.io.IOException; public class LoginCommand extends FrontCommand { @Override public void process() throws ServletException, IOException { - if (request.getMethod().equals("GET")) { - request.setAttribute("redirect", request.getRequestURL() - .append("?").append(request.getQueryString()).toString()); - forward("login"); - } else { + if (request.getMethod().equals("POST")) { HttpSession session = request.getSession(true); session.setAttribute("username", request.getParameter("username")); response.sendRedirect(request.getParameter("redirect")); + } else { + request.setAttribute("redirect", request.getRequestURL() + .append("?").append(request.getQueryString()).toString()); + forward("login"); } } } 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/enterprise/patterns/front/controller/commands/LogoutCommand.java new file mode 100644 index 0000000000..b502112558 --- /dev/null +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/LogoutCommand.java @@ -0,0 +1,16 @@ +package com.baeldung.enterprise.patterns.front.controller.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/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/enterprise/patterns/front/controller/commands/OrderCommand.java new file mode 100644 index 0000000000..91ed909a77 --- /dev/null +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/OrderCommand.java @@ -0,0 +1,33 @@ +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.Bookshelf; +import com.baeldung.enterprise.patterns.front.controller.data.Order; +import com.baeldung.enterprise.patterns.front.controller.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/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/enterprise/patterns/front/controller/commands/SearchCommand.java index 93dac4b363..517092508a 100644 --- 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/enterprise/patterns/front/controller/commands/SearchCommand.java @@ -1,19 +1,22 @@ 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 com.baeldung.enterprise.patterns.front.controller.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(); - Book book = new BookshelfImpl().getInstance() - .findByTitle(request.getParameter("title")); - if (book != null) { - request.setAttribute("book", book); + 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/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/enterprise/patterns/front/controller/commands/ShowCommand.java new file mode 100644 index 0000000000..eb615f075d --- /dev/null +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/commands/ShowCommand.java @@ -0,0 +1,21 @@ +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.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/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/enterprise/patterns/front/controller/commands/UnknownCommand.java index 90103c8f42..439ae0c8da 100644 --- 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/enterprise/patterns/front/controller/commands/UnknownCommand.java @@ -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/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/enterprise/patterns/front/controller/data/Book.java index abadcc0d76..472ba4c2fa 100644 --- 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/enterprise/patterns/front/controller/data/Book.java @@ -1,6 +1,10 @@ package com.baeldung.enterprise.patterns.front.controller.data; public interface Book { + String getIsbn(); + + void setIsbn(String isbn); + String getAuthor(); void setAuthor(String author); 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/enterprise/patterns/front/controller/data/BookImpl.java index b270bc7985..c633607f41 100644 --- 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/enterprise/patterns/front/controller/data/BookImpl.java @@ -1,6 +1,7 @@ package com.baeldung.enterprise.patterns.front.controller.data; public class BookImpl implements Book { + private String isbn; private String author; private String title; private Double price; @@ -8,12 +9,23 @@ public class BookImpl implements Book { public BookImpl() { } - public BookImpl(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; 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/enterprise/patterns/front/controller/data/Bookshelf.java index 8def45cb69..e479d27d0e 100644 --- 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/enterprise/patterns/front/controller/data/Bookshelf.java @@ -1,16 +1,18 @@ package com.baeldung.enterprise.patterns.front.controller.data; +import java.util.List; + public interface Bookshelf { default void init() { - add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99)); - add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88)); - add(new BookImpl("Unknown", "Something about German Umlauts (äüö) and ß", 5.49)); + 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)); } - Bookshelf getInstance(); - boolean add(E book); - Book findByTitle(String title); + Book get(String isbn); + + List find(String q); } 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/enterprise/patterns/front/controller/data/BookshelfImpl.java index 3862418857..26090acf74 100644 --- 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/enterprise/patterns/front/controller/data/BookshelfImpl.java @@ -1,24 +1,23 @@ package com.baeldung.enterprise.patterns.front.controller.data; import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; 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) { + public Book get(String isbn) { return this.stream() - .filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase())) + .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/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/enterprise/patterns/front/controller/data/Order.java new file mode 100644 index 0000000000..2b6b36568a --- /dev/null +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/Order.java @@ -0,0 +1,11 @@ +package com.baeldung.enterprise.patterns.front.controller.data; + +import java.util.Map; + +public interface Order { + String getUsername(); + + Map getItems(); + + void add(Book item, Integer quantity); +} 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/enterprise/patterns/front/controller/data/OrderImpl.java new file mode 100644 index 0000000000..eb4a6674cc --- /dev/null +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/java/com/baeldung/enterprise/patterns/front/controller/data/OrderImpl.java @@ -0,0 +1,40 @@ +package com.baeldung.enterprise.patterns.front.controller.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/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/enterprise/patterns/front/controller/filters/AuthenticationFilter.java index 66f9887980..2d43116faa 100644 --- 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/enterprise/patterns/front/controller/filters/AuthenticationFilter.java @@ -29,10 +29,10 @@ public class AuthenticationFilter extends BaseFilter { 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(); - callback.intercept(); } else { chain.doFilter(request, response); } 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/enterprise/patterns/front/controller/filters/FilterManager.java index efa8511f2a..e374b241c4 100644 --- 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/enterprise/patterns/front/controller/filters/FilterManager.java @@ -14,7 +14,7 @@ public class FilterManager { ) throws ServletException, IOException { FilterChain filterChain = new FilterChainImpl( new AuthenticationFilter(callback), - new AuditFilter() + new VisitorCounterFilter() ); filterChain.doFilter(request, response); } 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/enterprise/patterns/front/controller/filters/LoggingFilter.java index 089a7807cf..8617fba581 100644 --- 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/enterprise/patterns/front/controller/filters/LoggingFilter.java @@ -24,16 +24,10 @@ public class LoggingFilter extends BaseFilter { chain.doFilter(request, response); HttpServletRequest httpServletRequest = (HttpServletRequest) request; String username = (String) httpServletRequest.getAttribute("username"); - if (username != null && username.length() > 0) { - username = username.concat("@"); - } else { - username = ""; + if (username == null) { + username = "guest"; } - String intro = "Request"; - if (username.length() > 0) { - intro = "Authenticated request"; - } - log.info("{} from '{}{}': {}?{}", intro, username, request.getRemoteAddr(), + log.info("Request from '{}@{}': {}?{}", username, request.getRemoteAddr(), httpServletRequest.getRequestURI(), request.getParameterMap()); } } diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-found.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-found.jsp index 36f94b0933..5a03fe92ad 100644 --- a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-found.jsp +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-found.jsp @@ -1,13 +1,24 @@ +<%@ page import="com.baeldung.enterprise.patterns.front.controller.data.Book" %> +<%@ page import="java.util.List" %> Bookshelf: Title found -

Our Bookshelf contains this title:

-

${book.getTitle()}

-

Author: ${book.getAuthor()}

- - <%@include file="visitor-counter.jsp"%> +

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/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-notfound.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-notfound.jsp index 02d1307e00..284b1e4759 100644 --- a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-notfound.jsp +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/book-notfound.jsp @@ -1,11 +1,11 @@ - - Bookshelf: Title not found - + + Bookshelf: Title not found +

Our Bookshelf doesn't contains this title:

-

${param.get("title")}

- <%@include file="visitor-counter.jsp"%> +

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

+ <%@ include file="visitor-counter.jsp"%> diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/home.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/home.jsp new file mode 100644 index 0000000000..f892738c63 --- /dev/null +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/home.jsp @@ -0,0 +1,27 @@ +<%@ page import="com.baeldung.enterprise.patterns.front.controller.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/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/login.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/login.jsp index a1609ecaff..6983fba949 100644 --- a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/login.jsp +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/login.jsp @@ -6,10 +6,11 @@

Please input a username:

Login

-
+ - + ">
+ <%@ include file="visitor-counter.jsp" %> diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/shopping-cart-hint.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/shopping-cart-hint.jsp new file mode 100644 index 0000000000..5b7c1d9849 --- /dev/null +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/shopping-cart-hint.jsp @@ -0,0 +1,16 @@ +<%@ page import="com.baeldung.enterprise.patterns.front.controller.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/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/shopping-cart.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/shopping-cart.jsp new file mode 100644 index 0000000000..31b25968ee --- /dev/null +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/shopping-cart.jsp @@ -0,0 +1,29 @@ +<%@ page import="com.baeldung.enterprise.patterns.front.controller.data.Book" %> +<%@ page import="com.baeldung.enterprise.patterns.front.controller.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/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/unknown.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/unknown.jsp index 010cb7ffb4..b52b2de8d5 100644 --- a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/unknown.jsp +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/unknown.jsp @@ -5,6 +5,5 @@

Sorry, this command is not known!

- <%@include file="visitor-counter.jsp"%> diff --git a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/visitor-counter.jsp b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/visitor-counter.jsp index 7dc1d78196..397364e2b3 100644 --- a/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/visitor-counter.jsp +++ b/enterprise-patterns/intercepting-filter-pattern/src/main/webapp/WEB-INF/jsp/visitor-counter.jsp @@ -1,2 +1,5 @@ -
-

You are visitor #${counter}!

+<% Integer counter = (Integer) request.getAttribute("counter"); %> +<% if (counter != null && counter > 0) { %> +
+

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

+<% } %>