Skip to content

Commit 9c5404e

Browse files
committed
BAEL-305: Runnable demo application
1 parent 442d991 commit 9c5404e

13 files changed

Lines changed: 282 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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>servlet-api</artifactId>
20+
<version>2.5</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.apache.maven.plugins</groupId>
33+
<artifactId>maven-war-plugin</artifactId>
34+
<version>3.0.0</version>
35+
<configuration>
36+
<outputDirectory>${env.DEPLOYMENTS}</outputDirectory>
37+
</configuration>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
</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,22 @@
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.Bookshelf;
5+
6+
import javax.servlet.ServletException;
7+
import java.io.IOException;
8+
9+
public class BookCommand extends FrontCommand {
10+
@Override
11+
public void process() throws ServletException, IOException {
12+
Book book = Bookshelf.getInstance()
13+
.findByTitle(request.getParameter("title"));
14+
if (book != null) {
15+
request.setAttribute("book", book);
16+
forward("book-found");
17+
} else {
18+
request.setAttribute("books", Bookshelf.getInstance().getBooks());
19+
forward("book-notfound");
20+
}
21+
}
22+
}
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,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,32 @@
1+
package com.baeldung.enterprise.patterns.front.controller.data;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Bookshelf {
7+
private static Bookshelf INSTANCE = new Bookshelf();
8+
private List<Book> books = new ArrayList<>();
9+
10+
public static Bookshelf getInstance() {
11+
if (INSTANCE.books.size() == 0) {
12+
INSTANCE.init();
13+
}
14+
return INSTANCE;
15+
}
16+
17+
private void init() {
18+
books.add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
19+
books.add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
20+
}
21+
22+
public Book findByTitle(String title) {
23+
return books.stream()
24+
.filter(book -> book.getTitle().equalsIgnoreCase(title))
25+
.findFirst()
26+
.orElse(null);
27+
}
28+
29+
public List<Book> getBooks() {
30+
return books;
31+
}
32+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN"
3+
"http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd">
4+
<jboss-web>
5+
<context-root>/front-controller/</context-root>
6+
</jboss-web>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p>Our Bookshelf contains this title:</p>
2+
<h2>${book.getTitle()}</h2>
3+
<p>Author: ${book.getAuthor()}</p>
4+
<input type="submit" value="Buy it: ${book.getPrice()}$">
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<p>Our Bookshelf doesn't contains this title:</p>
2+
<h2>${param.get("title")}</h2>

0 commit comments

Comments
 (0)