BAEL-325: Added intercepting filters.

This commit is contained in:
Christian Rädel 2016-10-01 04:29:36 +02:00 committed by slavisa-baeldung
parent ae3d9760c9
commit dcc1889909
61 changed files with 756 additions and 167 deletions

View File

@ -4,7 +4,13 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.enterprise.patterns</groupId>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.baeldung.patterns</groupId>
<artifactId>patterns</artifactId>
<packaging>war</packaging>
@ -15,14 +21,14 @@
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
<scope>provided</scope>
</dependency>
</dependencies>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
@ -34,13 +40,21 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.0.M1</version>
<version>9.3.11.v20160721</version>
<configuration>
<webApp>
<contextPath>/front-controller</contextPath>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>

View File

@ -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();
}
}
}

View File

@ -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);
}
}

View File

@ -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");
}
}
}

View File

@ -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();
<E extends Book> boolean add(E book);
Book findByTitle(String title);
}

View File

@ -1,24 +0,0 @@
package com.baeldung.enterprise.patterns.front.controller.data;
import java.util.ArrayList;
public class BookshelfImpl extends ArrayList<Book> 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);
}
}

View File

@ -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();
}
}
}

View File

@ -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");
}
}
}

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}
}

View File

@ -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");
}
}

View File

@ -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));
}
}
}

View File

@ -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<Book> books = bookshelf.find(q);
if (books.size() > 0) {
request.setAttribute("books", books);
forward("book-found");
} else {
forward("book-notfound");
}
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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));
}
<E extends Book> boolean add(E book);
Book get(String isbn);
List<Book> find(String q);
}

View File

@ -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<Book> implements Bookshelf {
@Override
public Book get(String isbn) {
return this.stream()
.filter(book -> book.getIsbn().equals(isbn))
.findFirst()
.orElse(null);
}
@Override
public List<Book> find(String q) {
return this.stream()
.filter(book -> book.getTitle().toLowerCase().contains(q.toLowerCase())
|| book.getAuthor().toLowerCase().contains(q.toLowerCase()))
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.patterns.data;
import java.util.Map;
public interface Order {
String getUsername();
Map<Book, Integer> getItems();
void add(Book item, Integer quantity);
}

View File

@ -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<Book, Integer> 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<Book, Integer> getItems() {
return items;
}
public void setItems(Map<Book, Integer> 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);
}
}

View File

@ -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);
}
}
}

View File

@ -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());
}
}

View File

@ -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);
}
}

View File

@ -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<Filter> 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);
}
}
}

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.patterns.filters;
public interface OnIntercept {
void intercept();
}

View File

@ -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<String> 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);
}
}

View File

@ -1,12 +1,24 @@
<%@ page import="com.baeldung.patterns.data.Book" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html>
<head>
<title>Bookshelf: Title found</title>
</head>
<body>
<p>Our Bookshelf contains this title:</p>
<h2>${book.getTitle()}</h2>
<p>Author: ${book.getAuthor()}</p>
<input type="submit" value="Buy it: ${book.getPrice()}$">
<p>Our Bookshelf contains following titles:</p>
<% for (Book book : (List<Book>) request.getAttribute("books")) { %>
<h2><%= book.getTitle() %></h2>
<p>Author: <%= book.getAuthor() %></p>
<form action="index?command=Order" method="POST">
<label for="quantity">Quantity:</label>
<input type="text" id="quantity" name="quantity" value="1" required>
<input type="hidden" name="isbn" value="<%= book.getIsbn() %>">
<input type="submit" value="Buy it: <%= book.getPrice() %>$">
<a href="index?command=Home">Go back...</a>
</form>
<% } %>
<%@ include file="shopping-cart-hint.jsp"%>
<%@ include file="visitor-counter.jsp"%>
</body>
</html>

View File

@ -1,10 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Bookshelf: Title not found</title>
</head>
<head>
<title>Bookshelf: Title not found</title>
</head>
<body>
<p>Our Bookshelf doesn't contains this title:</p>
<h2>${param.get("title")}</h2>
<h2><%= request.getParameter("q") %></h2>
<%@ include file="visitor-counter.jsp"%>
</body>
</html>

View File

@ -0,0 +1,27 @@
<%@ page import="com.baeldung.patterns.data.Book" %>
<%@ page import="java.util.List" %>
<!DOCTYPE>
<html>
<head>
<title>Bookshelf: Home</title>
</head>
<body>
<form action="index?command=Search" method="POST">
<input type="text" id="search" name="q">
<input type="submit" value="Go!">
</form>
<% if (request.getParameter("message") != null) { %>
<h2><%= request.getParameter("message") %></h2>
<% } else { %>
<h2>Welcome to the Bookshelf!</h2>
<% } %>
<% for (Book book : (List<Book>) request.getAttribute("books")) { %>
<hr/>
<p><b><%= book.getAuthor() %>:</b></p>
<h3><%= book.getTitle() %></h3>
<a href="index?command=Show&isbn=<%= book.getIsbn() %>">More...</a>
<% } %>
<%@ include file="shopping-cart-hint.jsp"%>
<%@ include file="visitor-counter.jsp"%>
</body>
</html>

View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Bookshelf: Login</title>
</head>
<body>
<p>Please input a username:</p>
<h2>Login</h2>
<form action="index?command=Login" method="POST">
<input type="text" name="username" placeholder="Username">
<input type="hidden" name="redirect" value="<%= (String) request.getAttribute("redirect") %>">
<input type="submit" value="Proceed">
</form>
<%@ include file="visitor-counter.jsp" %>
</body>
</html>

View File

@ -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) { %>
<hr/>
<p>
Your shopping cart is holding
<% if (order.getItems().size() == 1) { %>
1 item.
<% } else { %>
<%= (order.getItems().size()) %> items.
<% } %>
<a href="index?command=Checkout">Checkout</a>
</p>
<% } %>
<% } %>

View File

@ -0,0 +1,29 @@
<%@ page import="com.baeldung.patterns.data.Book" %>
<%@ page import="com.baeldung.patterns.data.Order" %>
<%@ page import="java.util.Map" %>
<!DOCTYPE html>
<html>
<head>
<title>Bookshelf: Checkout</title>
</head>
<body>
<p>You are about to buy the following books:</p>
<h2>Shopping Cart</h2>
<% Order order = (Order) session.getAttribute("order"); %>
<ul>
<% for (Map.Entry<Book, Integer> entry : order.getItems().entrySet()) { %>
<li>
<b><%= entry.getValue() %> x <%= entry.getKey().getPrice() %></b>
<h3><%= entry.getKey().getTitle() %></h3>
<i> by <%= entry.getKey().getAuthor()%></i>
</li>
<% } %>
</ul>
<p>
<b>Total: <%= request.getAttribute("total") %></b>
</p>
<form action="index?command=Checkout" method="POST">
<input type="submit" value="Donate :)">
</form>
</body>
</html>

View File

@ -0,0 +1,5 @@
<% Integer counter = (Integer) request.getAttribute("counter"); %>
<% if (counter != null && counter > 0) { %>
<hr/>
<p>You are visitor #<%= counter %>. <a href="index?command=Logout">Logout</a></p>
<% } %>

View File

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>front-controller</servlet-name>
<servlet-class>com.baeldung.enterprise.patterns.front.controller.FrontControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>front-controller</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>