From ca9de6fe6a21a4fb13fe4aacc518a1511753ef7e Mon Sep 17 00:00:00 2001 From: jkramr Date: Thu, 15 Sep 2016 17:17:53 +0300 Subject: [PATCH 1/2] Add project id, deploy to Cloud Platform. --- appengine/helloworld/pom.xml | 4 ++++ .../com/example/appengine/helloworld/HelloServlet.java | 8 +++++--- .../helloworld/src/main/webapp/WEB-INF/appengine-web.xml | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/appengine/helloworld/pom.xml b/appengine/helloworld/pom.xml index af1b1b5899f..67143e226b7 100644 --- a/appengine/helloworld/pom.xml +++ b/appengine/helloworld/pom.xml @@ -33,6 +33,10 @@ Copyright 2015 Google Inc. All Rights Reserved. jar provided + + com.google.appengine + appengine-api-1.0-sdk + diff --git a/appengine/helloworld/src/main/java/com/example/appengine/helloworld/HelloServlet.java b/appengine/helloworld/src/main/java/com/example/appengine/helloworld/HelloServlet.java index d9135a0ce63..9ec820407e1 100644 --- a/appengine/helloworld/src/main/java/com/example/appengine/helloworld/HelloServlet.java +++ b/appengine/helloworld/src/main/java/com/example/appengine/helloworld/HelloServlet.java @@ -16,12 +16,13 @@ package com.example.appengine.helloworld; -import java.io.IOException; -import java.io.PrintWriter; +import com.google.appengine.api.utils.SystemProperty; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; // [START example] @SuppressWarnings("serial") @@ -30,7 +31,8 @@ public class HelloServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { PrintWriter out = resp.getWriter(); - out.println("Hello, world"); + out.println("Hello, "); + out.println(SystemProperty.Environment.version.get()); } } // [END example] diff --git a/appengine/helloworld/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/helloworld/src/main/webapp/WEB-INF/appengine-web.xml index 468b6d0430a..039d2cdffe1 100644 --- a/appengine/helloworld/src/main/webapp/WEB-INF/appengine-web.xml +++ b/appengine/helloworld/src/main/webapp/WEB-INF/appengine-web.xml @@ -13,8 +13,8 @@ --> - YOUR-PROJECT-ID - YOUR-VERSION-ID + hello-project-143513 + 1 true From 5f307553ba632ca102eeda7f5b113fd021f816b0 Mon Sep 17 00:00:00 2001 From: jkramr Date: Fri, 16 Sep 2016 15:16:38 +0300 Subject: [PATCH 2/2] Add web project user list. --- appengine/helloworld/pom.xml | 10 +++ .../example/appengine/helloworld/Guest.java | 45 ++++++++++ .../appengine/helloworld/HelloServlet.java | 45 ++++++---- .../appengine/helloworld/OfyHelper.java | 32 +++++++ .../src/main/webapp/WEB-INF/appengine-web.xml | 6 ++ .../main/webapp/WEB-INF/logging.properties | 13 +++ .../src/main/webapp/WEB-INF/web.xml | 37 ++++++-- .../src/main/webapp/stylesheets/main.css | 0 .../helloworld/src/main/webapp/users.jsp | 87 +++++++++++++++++++ 9 files changed, 251 insertions(+), 24 deletions(-) create mode 100644 appengine/helloworld/src/main/java/com/example/appengine/helloworld/Guest.java create mode 100644 appengine/helloworld/src/main/java/com/example/appengine/helloworld/OfyHelper.java create mode 100644 appengine/helloworld/src/main/webapp/WEB-INF/logging.properties rename appengine/{guestbook-objectify => helloworld}/src/main/webapp/stylesheets/main.css (100%) create mode 100644 appengine/helloworld/src/main/webapp/users.jsp diff --git a/appengine/helloworld/pom.xml b/appengine/helloworld/pom.xml index 67143e226b7..78fa7ad1b90 100644 --- a/appengine/helloworld/pom.xml +++ b/appengine/helloworld/pom.xml @@ -37,6 +37,16 @@ Copyright 2015 Google Inc. All Rights Reserved. com.google.appengine appengine-api-1.0-sdk + + com.googlecode.objectify + objectify + RELEASE + + + jstl + jstl + 1.2 + diff --git a/appengine/helloworld/src/main/java/com/example/appengine/helloworld/Guest.java b/appengine/helloworld/src/main/java/com/example/appengine/helloworld/Guest.java new file mode 100644 index 00000000000..faff18be09f --- /dev/null +++ b/appengine/helloworld/src/main/java/com/example/appengine/helloworld/Guest.java @@ -0,0 +1,45 @@ +/** + * Copyright 2014-2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//[START all] +package com.example.appengine.helloworld; + +import com.googlecode.objectify.annotation.Entity; +import com.googlecode.objectify.annotation.Id; +import com.googlecode.objectify.annotation.Index; + +/** + * The @Entity tells Objectify about our entity. We also register it in + * OfyHelper.java -- very important. + * + * This is never actually created, but gives a hint to Objectify about our Ancestor key. + */ +@Entity +public class Guest { + @Id public String id; + @Index public String name; + @Index public String email; + + public Guest(String id, String name, String email) { + this.id = id; + this.name = name; + this.email = email; + } + + public Guest() { + + } +} \ No newline at end of file diff --git a/appengine/helloworld/src/main/java/com/example/appengine/helloworld/HelloServlet.java b/appengine/helloworld/src/main/java/com/example/appengine/helloworld/HelloServlet.java index 9ec820407e1..4f61fb03506 100644 --- a/appengine/helloworld/src/main/java/com/example/appengine/helloworld/HelloServlet.java +++ b/appengine/helloworld/src/main/java/com/example/appengine/helloworld/HelloServlet.java @@ -1,12 +1,12 @@ /** * Copyright 2015 Google Inc. All Rights Reserved. - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,7 +16,10 @@ package com.example.appengine.helloworld; -import com.google.appengine.api.utils.SystemProperty; +import com.google.appengine.api.users.User; +import com.google.appengine.api.users.UserService; +import com.google.appengine.api.users.UserServiceFactory; +import com.googlecode.objectify.ObjectifyService; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -24,15 +27,25 @@ import java.io.IOException; import java.io.PrintWriter; -// [START example] -@SuppressWarnings("serial") -public class HelloServlet extends HttpServlet { - - @Override - public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { - PrintWriter out = resp.getWriter(); - out.println("Hello, "); - out.println(SystemProperty.Environment.version.get()); - } -} -// [END example] +@SuppressWarnings("serial") public class HelloServlet extends HttpServlet { + + public void doPost(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + PrintWriter out = resp.getWriter(); + + UserService userService = UserServiceFactory.getUserService(); + User user = userService.getCurrentUser(); + + Guest guest; + + String nickname = req.getParameter("nickname"); + + if (user != null) { + guest = new Guest(user.getUserId(), nickname, user.getEmail()); + + ObjectifyService.ofy().save().entity(guest).now(); + } + + resp.sendRedirect("/users.jsp"); + } +} \ No newline at end of file diff --git a/appengine/helloworld/src/main/java/com/example/appengine/helloworld/OfyHelper.java b/appengine/helloworld/src/main/java/com/example/appengine/helloworld/OfyHelper.java new file mode 100644 index 00000000000..7a6c1556203 --- /dev/null +++ b/appengine/helloworld/src/main/java/com/example/appengine/helloworld/OfyHelper.java @@ -0,0 +1,32 @@ +/** + * Copyright 2014-2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +//[START all] +package com.example.appengine.helloworld; + +import com.googlecode.objectify.ObjectifyService; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +/** + * OfyHelper, a ServletContextListener, is setup in web.xml to run before a JSP is run. This is + * required to let JSP's access Ofy. + **/ +public class OfyHelper implements ServletContextListener { + public void contextInitialized(ServletContextEvent event) { + ObjectifyService.register(Guest.class); + } + + public void contextDestroyed(ServletContextEvent event) { + } +} \ No newline at end of file diff --git a/appengine/helloworld/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/helloworld/src/main/webapp/WEB-INF/appengine-web.xml index 039d2cdffe1..c26451784dd 100644 --- a/appengine/helloworld/src/main/webapp/WEB-INF/appengine-web.xml +++ b/appengine/helloworld/src/main/webapp/WEB-INF/appengine-web.xml @@ -15,6 +15,12 @@ hello-project-143513 1 + true + + + + + diff --git a/appengine/helloworld/src/main/webapp/WEB-INF/logging.properties b/appengine/helloworld/src/main/webapp/WEB-INF/logging.properties new file mode 100644 index 00000000000..a17206681f0 --- /dev/null +++ b/appengine/helloworld/src/main/webapp/WEB-INF/logging.properties @@ -0,0 +1,13 @@ +# A default java.util.logging configuration. +# (All App Engine logging is through java.util.logging by default). +# +# To use this configuration, copy it into your application's WEB-INF +# folder and add the following to your appengine-web.xml: +# +# +# +# +# + +# Set the default logging level for all loggers to WARNING +.level = WARNING diff --git a/appengine/helloworld/src/main/webapp/WEB-INF/web.xml b/appengine/helloworld/src/main/webapp/WEB-INF/web.xml index 1a1704104a2..87b2e7a21da 100644 --- a/appengine/helloworld/src/main/webapp/WEB-INF/web.xml +++ b/appengine/helloworld/src/main/webapp/WEB-INF/web.xml @@ -1,14 +1,35 @@ - - + + + + - hello + sign com.example.appengine.helloworld.HelloServlet + - hello - / + sign + /sign + + + users.jsp + + + + + + ObjectifyFilter + com.googlecode.objectify.ObjectifyFilter + + + ObjectifyFilter + /* + + + com.example.appengine.helloworld.OfyHelper + + diff --git a/appengine/guestbook-objectify/src/main/webapp/stylesheets/main.css b/appengine/helloworld/src/main/webapp/stylesheets/main.css similarity index 100% rename from appengine/guestbook-objectify/src/main/webapp/stylesheets/main.css rename to appengine/helloworld/src/main/webapp/stylesheets/main.css diff --git a/appengine/helloworld/src/main/webapp/users.jsp b/appengine/helloworld/src/main/webapp/users.jsp new file mode 100644 index 00000000000..5521f454229 --- /dev/null +++ b/appengine/helloworld/src/main/webapp/users.jsp @@ -0,0 +1,87 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ page import="com.google.appengine.api.users.User" %> +<%@ page import="com.google.appengine.api.users.UserService" %> +<%@ page import="com.google.appengine.api.users.UserServiceFactory" %> + +<%@ page import="com.example.appengine.helloworld.Guest" %> +<%@ page import="com.googlecode.objectify.ObjectifyService" %> + +<%@ page import="java.util.List" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> + + + + + + + + +<% + UserService userService = UserServiceFactory.getUserService(); + User user = userService.getCurrentUser(); + if (user != null) { + pageContext.setAttribute("user", user); +%> + +

Hello, ${fn:escapeXml(user.nickname)}! (You can + sign out.)

+<% + } else { +%> +

Hello! + Sign in + to include your name with greetings you post.

+<% + } +%> + +<% + List guests = ObjectifyService.ofy() + .load() + .type(Guest.class) + .limit(5) + .list(); + + if (guests.isEmpty()) { +%> +

No records.

+<% + } else { +%> +

Users:

+ + + + + + + <% + for (Guest guest : guests) { + pageContext.setAttribute("guest_id", guest.id); + pageContext.setAttribute("guest_nickname", guest.name); + String author; + if (guest.email == null) { + author = ""; + } else { + author = guest.email; + } + pageContext.setAttribute("guest_email", author); +%> + + + + + +<% + } + } +%> +
IDemailName
${fn:escapeXml(guest_id)}${fn:escapeXml(guest_email)}${fn:escapeXml(guest_nickname)}
+ +
+
+
+
+ + + \ No newline at end of file