diff --git a/libraries-3/pom.xml b/libraries-3/pom.xml
index d345ff8f19..b20ae73de4 100644
--- a/libraries-3/pom.xml
+++ b/libraries-3/pom.xml
@@ -78,6 +78,28 @@
takes
1.19
+
+ org.apache.velocity
+ velocity-engine-core
+ 2.0
+
+
+ com.zaxxer
+ HikariCP
+
+
+ com.h2database
+ h2
+
+
+ javax.json
+ javax.json-api
+
+
+ org.glassfish
+ javax.json
+ 1.1
+
@@ -87,10 +109,29 @@
+
+
+
+ src/main/webapp
+ true
+
+
+ src/main/resources
+ true
+
+
+
+
hit-refresh
+
+
+ src/main/resources
+ true
+
+
org.codehaus.mojo
diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesApp.java b/libraries-3/src/main/java/com/baeldung/takes/TakesApp.java
index f1a7149349..cba715846f 100644
--- a/libraries-3/src/main/java/com/baeldung/takes/TakesApp.java
+++ b/libraries-3/src/main/java/com/baeldung/takes/TakesApp.java
@@ -1,26 +1,103 @@
package com.baeldung.takes;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.takes.Request;
+import org.takes.Response;
+import org.takes.facets.fallback.Fallback;
+import org.takes.facets.fallback.FbChain;
+import org.takes.facets.fallback.FbStatus;
+import org.takes.facets.fallback.RqFallback;
+import org.takes.facets.fallback.TkFallback;
+import org.takes.facets.fork.FkRegex;
+import org.takes.facets.fork.Fork;
+import org.takes.facets.fork.TkFork;
import org.takes.http.Exit;
import org.takes.http.FtBasic;
-import org.takes.http.FtCli;
-import org.takes.tk.TkClasspath;
-import org.takes.tk.TkWrap;
-import org.takes.Take;
-import org.takes.facets.fork.FkRegex;
-import org.takes.facets.fork.TkFork;
+import org.takes.misc.Opt;
+import org.takes.rq.RqHref;
+import org.takes.rs.RsText;
+import org.takes.rs.RsWithBody;
+import org.takes.rs.RsWithStatus;
+import org.takes.rs.RsWithType;
+import org.takes.tk.TkSlf4j;
+
+import com.zaxxer.hikari.HikariConfig;
+import com.zaxxer.hikari.HikariDataSource;
public final class TakesApp {
+ // public static void main(final String... args) throws Exception {
+ // new FtBasic(new TkFork(
+ // new FkRegex("/", "Hello, World!"),
+ // new FkRegex("/index", new TakesIndex()),
+ // new FkRegex("/contact", new TakesContact())
+ // ),6060).start(Exit.NEVER);
+ // }
+
+ public static void main(final String... args) throws IOException, SQLException {
+ new FtBasic(
+ new TkFallback(
+ new TkSlf4j(
+ new TkFork(
+ new FkRegex("/", "Hello, World!"),
+ new FkRegex("/index", new TakesIndex()),
+ new FkRegex("/contact", new TakesContact()),
+ new FkRegex("\\A/createUser", new TakesCreateUser(TakesApp.dbConnection())),
+ new FkRegex("\\A/readUser", new TakesReadUser(TakesApp.dbConnection()))
+ )
+ ),
+ new FbChain(
+ new FbStatus(404, new RsText("sorry, page is absent")),
+ new FbStatus(405, new RsText("this method is not allowed here")),
+ new Fallback() {
+ @Override
+ public Opt route(final RqFallback req) {
+ return new Opt.Single(
+ new RsText(req.throwable().getMessage())
+ );
+ }
+ })
+ ),
+ 6060
+ ).start(Exit.NEVER);
+ }
-// public static void main(final String... args) throws Exception {
-// new FtBasic(new TkFork(new FkRegex("/", "hello, world... this is coming from Takes!")), 6060).start(Exit.NEVER);
-// }
+ // public static void main(final String... args) throws IOException {
+ //
+ // new FtBasic(
+ // new TkFork(
+ // new Fork() {
+ // @Override
+ // public Opt route(Request req) throws Exception {
+ // if (new RqHref.Base(req).href().path().equals("/entry")) {
+ // return new Opt.Single(new RsWithStatus(new RsWithType(new RsWithBody("hello this is for request"), "text/html"), 200));
+ // } else {
+ // return new Opt.Single(null);
+ // }
+ // }
+ // }
+ // ),6060).start(Exit.NEVER);
+ //
+ //
+ // }
- public static void main(final String... args) throws Exception {
- new FtBasic(new TakesIndex(), 6060).start(Exit.NEVER);
- }
+ private static Connection dbConnection() throws SQLException {
+ HikariConfig config = new HikariConfig();
+ config.setJdbcUrl("jdbc:h2:mem:devDB;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'");
+ config.setUsername("sa");
+ config.setPassword("");
+ @SuppressWarnings("resource")
+ HikariDataSource ds = new HikariDataSource(config);
+
+ return ds.getConnection();
+ }
}
\ No newline at end of file
diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesContact.java b/libraries-3/src/main/java/com/baeldung/takes/TakesContact.java
new file mode 100644
index 0000000000..90789c54a9
--- /dev/null
+++ b/libraries-3/src/main/java/com/baeldung/takes/TakesContact.java
@@ -0,0 +1,26 @@
+package com.baeldung.takes;
+
+import java.io.IOException;
+import java.sql.SQLException;
+
+import org.takes.Request;
+import org.takes.Response;
+import org.takes.Take;
+import org.takes.rs.RsWithBody;
+import org.takes.rs.RsWithStatus;
+import org.takes.rs.RsWithType;
+
+public final class TakesContact implements Take {
+
+ @Override
+ public Response act(Request req) throws IOException, SQLException {
+ return new RsWithStatus(new RsWithType(new RsWithBody(""
+ + ""
+ + "Takes Application - Contact"
+ + ""
+ + "Contact us at @baeldung.com"
+ + ""), "text/html"), 200);
+ }
+
+
+}
diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesCreateUser.java b/libraries-3/src/main/java/com/baeldung/takes/TakesCreateUser.java
new file mode 100644
index 0000000000..07b9d803db
--- /dev/null
+++ b/libraries-3/src/main/java/com/baeldung/takes/TakesCreateUser.java
@@ -0,0 +1,49 @@
+package com.baeldung.takes;
+
+import java.io.IOException;
+import java.net.URI;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+import org.takes.Request;
+import org.takes.Response;
+import org.takes.Take;
+import org.takes.misc.Href;
+import org.takes.rq.RqForm;
+import org.takes.rq.RqHref;
+import org.takes.rq.form.RqFormSmart;
+import org.takes.rs.RsHtml;
+import org.takes.rs.RsVelocity;
+
+public final class TakesCreateUser implements Take {
+
+ public static Connection con;
+
+ TakesCreateUser(Connection connection) {
+ con = connection;
+ }
+
+ @Override
+ public Response act(final Request req) throws IOException, SQLException {
+
+ RqForm form = new RqFormSmart(req);
+ Iterable idParam = form.param("id");
+ Iterable userParam = form.param("user");
+
+ int id = Integer.parseInt(idParam.iterator().next());
+ String user = userParam.iterator().next();
+
+ final String INSERT_SQL_QUERY = "insert into take_user values ("+id+", "+"'"+user+"')";
+
+ try (PreparedStatement pst = con.prepareStatement(INSERT_SQL_QUERY)) {
+ System.out.println(pst.executeUpdate());
+
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+
+ return new RsHtml(new RsVelocity(this.getClass().getResource("/templates/index.vm") ,new RsVelocity.Pair("userName", "Anshul")));
+ }
+
+}
diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesIndex.java b/libraries-3/src/main/java/com/baeldung/takes/TakesIndex.java
index 646b886abb..67487bf200 100644
--- a/libraries-3/src/main/java/com/baeldung/takes/TakesIndex.java
+++ b/libraries-3/src/main/java/com/baeldung/takes/TakesIndex.java
@@ -1,25 +1,20 @@
package com.baeldung.takes;
import java.io.IOException;
+import java.sql.SQLException;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
+import org.takes.rs.RsHtml;
import org.takes.rs.RsVelocity;
-import org.takes.rs.RsWithBody;
-import org.takes.rs.RsWithStatus;
-import org.takes.rs.RsWithType;
public final class TakesIndex implements Take {
-
-// @Override
-// public Response act(Request req) throws IOException {
-// return new RsWithStatus(new RsWithType(new RsWithBody("Hello, world!"), "text/html"), 200);
-// }
@Override
- public Response act(Request req) {
- return new RsVelocity("Hello, ${name}", new RsVelocity.Pair("name", "Jeffrey"));
+ public Response act(final Request req) throws IOException, SQLException {
+
+ return new RsHtml(new RsVelocity(this.getClass().getResource("/templates/index.vm") ,new RsVelocity.Pair("userName", "Anshul")));
}
-
+
}
diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesReadUser.java b/libraries-3/src/main/java/com/baeldung/takes/TakesReadUser.java
new file mode 100644
index 0000000000..3d0b85fef3
--- /dev/null
+++ b/libraries-3/src/main/java/com/baeldung/takes/TakesReadUser.java
@@ -0,0 +1,51 @@
+package com.baeldung.takes;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import javax.json.Json;
+import javax.json.JsonStructure;
+
+import org.takes.Request;
+import org.takes.Response;
+import org.takes.Take;
+import org.takes.misc.Href;
+import org.takes.misc.VerboseIterable;
+import org.takes.rq.RqHref;
+import org.takes.rs.RsJson;
+
+
+public final class TakesReadUser implements Take {
+
+ public static Connection con;
+
+ TakesReadUser(Connection connection) {
+ con = connection;
+ }
+
+ @Override
+ public Response act(final Request req) throws IOException, SQLException {
+ Href href = new RqHref.Base(req).href();
+ Iterable ids = href.param("id");
+ int id = Integer.parseInt((String) ids.iterator().next());
+
+ final String SELECT_SQL_QUERY = "select id, user from take_user where id = " + id;
+ JsonStructure json = null;
+ try (PreparedStatement pst = con.prepareStatement(SELECT_SQL_QUERY); ResultSet rs = pst.executeQuery();) {
+ while (rs.next()) {
+ json = Json.createObjectBuilder()
+ .add("id", rs.getInt("id"))
+ .add("user", rs.getString("user"))
+ .build();
+ }
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+
+ return new RsJson(json);
+ }
+
+}
diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesServletApp.java b/libraries-3/src/main/java/com/baeldung/takes/TakesServletApp.java
deleted file mode 100644
index 93951130f7..0000000000
--- a/libraries-3/src/main/java/com/baeldung/takes/TakesServletApp.java
+++ /dev/null
@@ -1,23 +0,0 @@
-package com.baeldung.takes;
-
-import javax.servlet.ServletContext;
-
-import org.takes.Request;
-import org.takes.Response;
-import org.takes.Take;
-import org.takes.rs.RsText;
-
-public final class TakesServletApp implements Take {
-
- private final ServletContext ctx;
-
- public TakesServletApp(final ServletContext context) {
- this.ctx = context;
- }
-
- @Override
- public Response act(final Request req) {
- return new RsText("Hello servlet!");
- }
-}
-
diff --git a/libraries-3/src/main/resources/db.sql b/libraries-3/src/main/resources/db.sql
new file mode 100644
index 0000000000..3b73446537
--- /dev/null
+++ b/libraries-3/src/main/resources/db.sql
@@ -0,0 +1,7 @@
+drop table if exists take_user;
+
+create table take_user(
+ id numeric,
+ user varchar(255),
+ constraint pk_take_user primary key (id)
+);
\ No newline at end of file
diff --git a/libraries-3/src/main/webapp/templates/index.vm b/libraries-3/src/main/webapp/templates/index.vm
new file mode 100644
index 0000000000..1eaf8fa53b
--- /dev/null
+++ b/libraries-3/src/main/webapp/templates/index.vm
@@ -0,0 +1,9 @@
+
+
+Index
+
+
+Takes Web Application
+Welcome, ${userName}
+
+