From 5e63713fdaba68647c6a64837a730d055e725f13 Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Date: Thu, 26 Jan 2012 23:20:14 -0200 Subject: [PATCH 1/2] Hello World JSP Support --- README.md | 16 ++++++++++++---- pom.xml | 24 ++++++++++++++++++------ src/main/java/HelloWorld.java | 19 ++++++++++++------- src/main/webapp/WEB-INF/web.xml | 16 ++++++++++++++++ src/main/webapp/hello.jsp | 1 + 5 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 src/main/webapp/WEB-INF/web.xml create mode 100644 src/main/webapp/hello.jsp diff --git a/README.md b/README.md index fb0a7b6..8518661 100644 --- a/README.md +++ b/README.md @@ -165,13 +165,21 @@ On Windows: You should now see something similar to: :::term - 2011-08-18 15:52:24.066:INFO::jetty-7.4.5.v20110725 - 2011-08-18 15:52:24.142:INFO::started o.e.j.s.ServletContextHandler{/,null} - 2011-08-18 15:52:24.168:INFO::Started SelectChannelConnector@0.0.0.0:5000 START + 2012-01-26 23:16:56.678:INFO:oejs.Server:jetty-7.5.4.v20111024 + 2012-01-26 23:16:56.886:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/,file:/.../src/main/webapp/},src/main/webapp + 2012-01-26 23:16:56.939:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:5000 STARTING Open the app in your browser: [http://localhost:5000](http://localhost:5000) - + + If you see this error "org.apache.jasper.JasperException: java.err.nojdk" on windows, try this: + You must have JAVA_HOME pointing to JDK PATH. + + :::term + $ set PATH=%JAVA_HOME%\bin;%PATH% + $ set PORT=5000 + $ target\bin\webapp.bat + ## Declare Process Types With Procfile diff --git a/pom.xml b/pom.xml index e1ca90a..4224bc0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,18 +6,30 @@ com.example 1.0-SNAPSHOT helloworld + + - org.eclipse.jetty - jetty-servlet - 7.4.5.v20110725 + org.eclipse.jetty.aggregate + jetty-webapp + 7.5.4.v20111024 + + - javax.servlet - servlet-api - 2.5 + org.eclipse.jdt.core.compiler + ecj + 3.7.1 + + + + + org.glassfish.web + jsp-impl + 2.1.3-b10 + diff --git a/src/main/java/HelloWorld.java b/src/main/java/HelloWorld.java index e04cc7c..a717df6 100644 --- a/src/main/java/HelloWorld.java +++ b/src/main/java/HelloWorld.java @@ -1,23 +1,28 @@ import java.io.IOException; + import javax.servlet.ServletException; -import javax.servlet.http.*; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.servlet.*; +import org.eclipse.jetty.webapp.WebAppContext; public class HelloWorld extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - resp.getWriter().print("Hello from Java!\n"); + String name = req.getParameter("name"); + req.setAttribute("name", name == null ? "Heroku" : name); + + // Call JSP + req.getRequestDispatcher("hello.jsp").forward(req, resp); } public static void main(String[] args) throws Exception{ Server server = new Server(Integer.valueOf(System.getenv("PORT"))); - ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); - context.setContextPath("/"); - server.setHandler(context); - context.addServlet(new ServletHolder(new HelloWorld()),"/*"); + server.setHandler(new WebAppContext("src/main/webapp", "/")); server.start(); server.join(); } diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..c0e1728 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,16 @@ + + + + HelloWorld Application + + + HelloWorldServlet + HelloWorld + + + + HelloWorldServlet + / + + + \ No newline at end of file diff --git a/src/main/webapp/hello.jsp b/src/main/webapp/hello.jsp new file mode 100644 index 0000000..99b242f --- /dev/null +++ b/src/main/webapp/hello.jsp @@ -0,0 +1 @@ +Hello, <%=request.getAttribute("name")%> \ No newline at end of file From 8b804b0bdadd562048f90e4161499c3bf4e1174e Mon Sep 17 00:00:00 2001 From: Gabriel Moreira Date: Thu, 26 Jan 2012 23:24:39 -0200 Subject: [PATCH 2/2] Hello World JSP Support --- README.md | 10 ++++------ src/main/java/HelloWorld.java | 5 +++-- src/main/webapp/hello.jsp | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8518661..774cc01 100644 --- a/README.md +++ b/README.md @@ -172,13 +172,11 @@ You should now see something similar to: Open the app in your browser: [http://localhost:5000](http://localhost:5000) - If you see this error "org.apache.jasper.JasperException: java.err.nojdk" on windows, try this: - You must have JAVA_HOME pointing to JDK PATH. + If you see this error "org.apache.jasper.JasperException: java.err.nojdk" on windows (You must have JAVA_HOME pointing to JDK PATH), try this: - :::term - $ set PATH=%JAVA_HOME%\bin;%PATH% - $ set PORT=5000 - $ target\bin\webapp.bat + $ set PATH=%JAVA_HOME%\bin;%PATH% + $ set PORT=5000 + $ target\bin\webapp.bat ## Declare Process Types With Procfile diff --git a/src/main/java/HelloWorld.java b/src/main/java/HelloWorld.java index a717df6..f0f5034 100644 --- a/src/main/java/HelloWorld.java +++ b/src/main/java/HelloWorld.java @@ -14,9 +14,9 @@ public class HelloWorld extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); - req.setAttribute("name", name == null ? "Heroku" : name); + req.setAttribute("name", name == null ? "JSP" : name); - // Call JSP + // Forward to JSP req.getRequestDispatcher("hello.jsp").forward(req, resp); } @@ -26,4 +26,5 @@ public static void main(String[] args) throws Exception{ server.start(); server.join(); } + } diff --git a/src/main/webapp/hello.jsp b/src/main/webapp/hello.jsp index 99b242f..23f4971 100644 --- a/src/main/webapp/hello.jsp +++ b/src/main/webapp/hello.jsp @@ -1 +1 @@ -Hello, <%=request.getAttribute("name")%> \ No newline at end of file +Hello <%=request.getAttribute("name")%> \ No newline at end of file