products = service.getProducts();
+
+ context.put("products", products);
+
+ Template template = null;
+
+ try {
+ template = getTemplate("templates/index.vm");
+ response.setHeader("Template Returned", "Success");
+ } catch (Exception e) {
+ logger.error("Error while reading the template ", e);
+ }
+
+ return template;
+
+ }
+}
diff --git a/apache-velocity/src/main/resources/logback.xml b/apache-velocity/src/main/resources/logback.xml
new file mode 100644
index 0000000000..70a420a57a
--- /dev/null
+++ b/apache-velocity/src/main/resources/logback.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+ %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/apache-velocity/src/main/webapp/WEB-INF/velocity.properties b/apache-velocity/src/main/webapp/WEB-INF/velocity.properties
new file mode 100644
index 0000000000..00e0b7e410
--- /dev/null
+++ b/apache-velocity/src/main/webapp/WEB-INF/velocity.properties
@@ -0,0 +1,4 @@
+resource.loader=webapp
+webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader
+webapp.resource.loader.path = .
+webapp.resource.loader.cache = true
\ No newline at end of file
diff --git a/apache-velocity/src/main/webapp/WEB-INF/web.xml b/apache-velocity/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000000..95b41b36dd
--- /dev/null
+++ b/apache-velocity/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,49 @@
+
+
+
+ apache-velocity
+
+ ProductServlet
+ com.baeldung.apache.velocity.servlet.ProductServlet
+
+
+
+ LayoutServlet
+ com.baeldung.apache.velocity.servlet.LayoutServlet
+
+
+ velocityLayout
+ org.apache.velocity.tools.view.VelocityLayoutServlet
+
+
+ org.apache.velocity.properties
+ /WEB-INF/velocity.properties
+
+
+
+ ProductServlet
+ /
+
+
+
+ LayoutServlet
+ /layout
+
+
+ velocityLayout
+ *.vm
+
+
+
+
+ 30
+
+
+
+
+
+ index.html
+
+
diff --git a/apache-velocity/src/main/webapp/fragments/footer.vm b/apache-velocity/src/main/webapp/fragments/footer.vm
new file mode 100644
index 0000000000..41bb36ce5e
--- /dev/null
+++ b/apache-velocity/src/main/webapp/fragments/footer.vm
@@ -0,0 +1,4 @@
+
+ @Copyright baeldung.com
+
\ No newline at end of file
diff --git a/apache-velocity/src/main/webapp/fragments/header.vm b/apache-velocity/src/main/webapp/fragments/header.vm
new file mode 100644
index 0000000000..96700d3baf
--- /dev/null
+++ b/apache-velocity/src/main/webapp/fragments/header.vm
@@ -0,0 +1,5 @@
+
\ No newline at end of file
diff --git a/apache-velocity/src/main/webapp/layout/Default.vm b/apache-velocity/src/main/webapp/layout/Default.vm
new file mode 100644
index 0000000000..39a8b277a5
--- /dev/null
+++ b/apache-velocity/src/main/webapp/layout/Default.vm
@@ -0,0 +1,22 @@
+
+
+ Velocity
+
+
+
+ #parse("/fragments/header.vm")
+
+
+
+
+
+
+ $screen_content
+
+
+
+
+ #parse("/fragments/footer.vm")
+
+
+
\ No newline at end of file
diff --git a/apache-velocity/src/main/webapp/templates/index.vm b/apache-velocity/src/main/webapp/templates/index.vm
new file mode 100644
index 0000000000..0ca07caf42
--- /dev/null
+++ b/apache-velocity/src/main/webapp/templates/index.vm
@@ -0,0 +1,63 @@
+
+
+ Online Electronic Store
+
+
+
+
+
+ Today's Offers
+
+
+ $products.size() Products on Sale!
+
+ We are proud to offer these fine products
+ at these amazing prices.
+
+
+ #set( $count = 1 )
+
+
+ | Serial # | Product Name | Price |
+
+ #foreach( $product in $products )
+
+ | $count) |
+ $product.getName() |
+ $product.getPrice() |
+
+ #set( $count = $count + 1 )
+ #end
+
+
+
+
+
+
diff --git a/apache-velocity/src/main/webapp/templates/layoutdemo.vm b/apache-velocity/src/main/webapp/templates/layoutdemo.vm
new file mode 100644
index 0000000000..0626b655c9
--- /dev/null
+++ b/apache-velocity/src/main/webapp/templates/layoutdemo.vm
@@ -0,0 +1,27 @@
+#set( $layout = "layout.vm" )
+
+ Today's Offers
+
+
+ $products.size() Products on Sale!
+
+ We are proud to offer these fine products
+ at these amazing prices.
+
+
+ #set( $count = 1 )
+
+
+ | Serial # | Product Name | Price |
+
+ #foreach( $product in $products )
+
+ | $count) |
+ $product.getName() |
+ $product.getPrice() |
+
+ #set( $count = $count + 1 )
+ #end
+
+
+
\ No newline at end of file
diff --git a/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/LayoutServletLiveTest.java b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/LayoutServletLiveTest.java
new file mode 100644
index 0000000000..f1f166b119
--- /dev/null
+++ b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/LayoutServletLiveTest.java
@@ -0,0 +1,26 @@
+package com.baeldung.apache.velocity.servlet;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+
+public class LayoutServletLiveTest {
+
+ @Test
+ public void whenRequestUsingHttpClient_thenCorrectResponse() throws Exception {
+
+ HttpClient client = new DefaultHttpClient();
+ HttpGet method= new HttpGet("http://localhost:8080/layout");
+
+ HttpResponse httpResponse = client.execute(method);
+
+ assertEquals("Success", httpResponse.getHeaders("Template Returned")[0].getValue());
+
+ }
+
+}
diff --git a/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/ProductServletLiveTest.java b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/ProductServletLiveTest.java
new file mode 100644
index 0000000000..397e575d4d
--- /dev/null
+++ b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/ProductServletLiveTest.java
@@ -0,0 +1,24 @@
+package com.baeldung.apache.velocity.servlet;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ProductServletLiveTest {
+
+ @Test
+ public void whenRequestUsingHttpClient_thenCorrectResponse() throws Exception {
+
+ HttpClient client = new DefaultHttpClient();
+ HttpGet method= new HttpGet("http://localhost:8080/");
+
+ HttpResponse httpResponse = client.execute(method);
+
+ assertEquals("Success", httpResponse.getHeaders("Template Returned")[0].getValue());
+
+ }
+}
diff --git a/pom.xml b/pom.xml
index 9eb4e78dbf..ecb07e987b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -192,6 +192,8 @@
xstream
struts2
+ apache-velocity
+