Skip to content

Commit 3d7ccfd

Browse files
BAEL-305 - front controller pattern source
1 parent 4c561c3 commit 3d7ccfd

14 files changed

Lines changed: 314 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>front-controller-pattern</artifactId>
8+
<packaging>war</packaging>
9+
10+
<parent>
11+
<artifactId>enterprise-patterns-parent</artifactId>
12+
<groupId>com.baeldung.enterprise.patterns</groupId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>javax.servlet</groupId>
19+
<artifactId>javax.servlet-api</artifactId>
20+
<version>3.1.0</version>
21+
<scope>provided</scope>
22+
</dependency>
23+
</dependencies>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
</plugin>
31+
<plugin>
32+
<groupId>org.eclipse.jetty</groupId>
33+
<artifactId>jetty-maven-plugin</artifactId>
34+
<version>9.4.0.M1</version>
35+
<configuration>
36+
<webApp>
37+
<contextPath>/front-controller</contextPath>
38+
</webApp>
39+
</configuration>
40+
</plugin>
41+
</plugins>
42+
</build>
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.enterprise.patterns.front.controller;
2+
3+
import com.baeldung.enterprise.patterns.front.controller.commands.FrontCommand;
4+
import com.baeldung.enterprise.patterns.front.controller.commands.UnknownCommand;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
12+
public class FrontControllerServlet extends HttpServlet {
13+
@Override
14+
protected void doGet(
15+
HttpServletRequest request,
16+
HttpServletResponse response
17+
) throws ServletException, IOException {
18+
FrontCommand command = getCommand(request);
19+
command.init(getServletContext(), request, response);
20+
command.process();
21+
}
22+
23+
private FrontCommand getCommand(HttpServletRequest request) {
24+
try {
25+
return (FrontCommand) getCommandClass(request)
26+
.asSubclass(FrontCommand.class)
27+
.newInstance();
28+
} catch (Exception e) {
29+
throw new RuntimeException("Failed to get command!", e);
30+
}
31+
}
32+
33+
private Class getCommandClass(HttpServletRequest request) {
34+
try {
35+
return Class.forName(
36+
String.format(
37+
"com.baeldung.enterprise.patterns.front.controller.commands.%sCommand",
38+
request.getParameter("command")
39+
)
40+
);
41+
} catch (ClassNotFoundException e) {
42+
return UnknownCommand.class;
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.enterprise.patterns.front.controller.commands;
2+
3+
import javax.servlet.RequestDispatcher;
4+
import javax.servlet.ServletContext;
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
import java.io.IOException;
9+
10+
public abstract class FrontCommand {
11+
protected ServletContext context;
12+
protected HttpServletRequest request;
13+
protected HttpServletResponse response;
14+
15+
public void init(
16+
ServletContext servletContext,
17+
HttpServletRequest servletRequest,
18+
HttpServletResponse servletResponse
19+
) {
20+
this.context = servletContext;
21+
this.request = servletRequest;
22+
this.response = servletResponse;
23+
}
24+
25+
public abstract void process() throws ServletException, IOException;
26+
27+
protected void forward(String target) throws ServletException, IOException {
28+
target = String.format("/WEB-INF/jsp/%s.jsp", target);
29+
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
30+
dispatcher.forward(request, response);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.enterprise.patterns.front.controller.commands;
2+
3+
import com.baeldung.enterprise.patterns.front.controller.data.Book;
4+
import com.baeldung.enterprise.patterns.front.controller.data.BookshelfImpl;
5+
6+
import javax.servlet.ServletException;
7+
import java.io.IOException;
8+
9+
public class SearchCommand extends FrontCommand {
10+
@Override
11+
public void process() throws ServletException, IOException {
12+
Book book = new BookshelfImpl().getInstance()
13+
.findByTitle(request.getParameter("title"));
14+
if (book != null) {
15+
request.setAttribute("book", book);
16+
forward("book-found");
17+
} else {
18+
forward("book-notfound");
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.enterprise.patterns.front.controller.commands;
2+
3+
import javax.servlet.ServletException;
4+
import java.io.IOException;
5+
6+
public class UnknownCommand extends FrontCommand {
7+
@Override
8+
public void process() throws ServletException, IOException {
9+
forward("unknown");
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.enterprise.patterns.front.controller.data;
2+
3+
public class Book {
4+
private String author;
5+
private String title;
6+
private Double price;
7+
8+
public Book() {
9+
}
10+
11+
public Book(String author, String title, Double price) {
12+
this.author = author;
13+
this.title = title;
14+
this.price = price;
15+
}
16+
17+
public String getAuthor() {
18+
return author;
19+
}
20+
21+
public void setAuthor(String author) {
22+
this.author = author;
23+
}
24+
25+
public String getTitle() {
26+
return title;
27+
}
28+
29+
public void setTitle(String title) {
30+
this.title = title;
31+
}
32+
33+
public Double getPrice() {
34+
return price;
35+
}
36+
37+
public void setPrice(Double price) {
38+
this.price = price;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.enterprise.patterns.front.controller.data;
2+
3+
public interface Bookshelf {
4+
5+
default void init() {
6+
add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
7+
add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
8+
}
9+
10+
Bookshelf getInstance();
11+
12+
<E extends Book> boolean add(E book);
13+
14+
Book findByTitle(String title);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.enterprise.patterns.front.controller.data;
2+
3+
import java.util.ArrayList;
4+
5+
public class BookshelfImpl extends ArrayList<Book> implements Bookshelf {
6+
private static Bookshelf INSTANCE;
7+
8+
@Override
9+
public Bookshelf getInstance() {
10+
if (INSTANCE == null) {
11+
INSTANCE = new BookshelfImpl();
12+
INSTANCE.init();
13+
}
14+
return INSTANCE;
15+
}
16+
17+
@Override
18+
public Book findByTitle(String title) {
19+
return this.stream()
20+
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
21+
.findFirst()
22+
.orElse(null);
23+
}
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Bookshelf: Title found</title>
5+
</head>
6+
<body>
7+
<p>Our Bookshelf contains this title:</p>
8+
<h2>${book.getTitle()}</h2>
9+
<p>Author: ${book.getAuthor()}</p>
10+
<input type="submit" value="Buy it: ${book.getPrice()}$">
11+
</body>
12+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Bookshelf: Title not found</title>
5+
</head>
6+
<body>
7+
<p>Our Bookshelf doesn't contains this title:</p>
8+
<h2>${param.get("title")}</h2>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)