From 1845201ea67d14853d518161580fbe48cf411a25 Mon Sep 17 00:00:00 2001 From: Stephanie Fu Date: Mon, 6 Jul 2020 19:48:16 +0000 Subject: [PATCH 1/2] Declare datastore and userService once --- .../main/java/com/google/sps/servlets/NicknameServlet.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/walkthroughs/week-4-libraries/authentication/examples/user-nicknames/src/main/java/com/google/sps/servlets/NicknameServlet.java b/walkthroughs/week-4-libraries/authentication/examples/user-nicknames/src/main/java/com/google/sps/servlets/NicknameServlet.java index 07fadbc..71915a1 100644 --- a/walkthroughs/week-4-libraries/authentication/examples/user-nicknames/src/main/java/com/google/sps/servlets/NicknameServlet.java +++ b/walkthroughs/week-4-libraries/authentication/examples/user-nicknames/src/main/java/com/google/sps/servlets/NicknameServlet.java @@ -30,6 +30,8 @@ @WebServlet("/nickname") public class NicknameServlet extends HttpServlet { + private static final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); + private static final UserService userService = UserServiceFactory.getUserService(); @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { @@ -37,7 +39,6 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro PrintWriter out = response.getWriter(); out.println("

Set Nickname

"); - UserService userService = UserServiceFactory.getUserService(); if (userService.isUserLoggedIn()) { String nickname = getUserNickname(userService.getCurrentUser().getUserId()); out.println("

Set your nickname here:

"); @@ -54,7 +55,6 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { - UserService userService = UserServiceFactory.getUserService(); if (!userService.isUserLoggedIn()) { response.sendRedirect("/nickname"); return; @@ -63,7 +63,6 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr String nickname = request.getParameter("nickname"); String id = userService.getCurrentUser().getUserId(); - DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Entity entity = new Entity("UserInfo", id); entity.setProperty("id", id); entity.setProperty("nickname", nickname); @@ -77,7 +76,6 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr * Returns the nickname of the user with id, or empty String if the user has not set a nickname. */ private String getUserNickname(String id) { - DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); Query query = new Query("UserInfo") .setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, id)); From 239ac5b3706bcc87ae0f43fe397a4578b047fa13 Mon Sep 17 00:00:00 2001 From: Stephanie Fu Date: Mon, 6 Jul 2020 19:48:44 +0000 Subject: [PATCH 2/2] Make getUserNickname static --- .../src/main/java/com/google/sps/servlets/NicknameServlet.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/walkthroughs/week-4-libraries/authentication/examples/user-nicknames/src/main/java/com/google/sps/servlets/NicknameServlet.java b/walkthroughs/week-4-libraries/authentication/examples/user-nicknames/src/main/java/com/google/sps/servlets/NicknameServlet.java index 71915a1..10a5b30 100644 --- a/walkthroughs/week-4-libraries/authentication/examples/user-nicknames/src/main/java/com/google/sps/servlets/NicknameServlet.java +++ b/walkthroughs/week-4-libraries/authentication/examples/user-nicknames/src/main/java/com/google/sps/servlets/NicknameServlet.java @@ -75,7 +75,7 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr /** * Returns the nickname of the user with id, or empty String if the user has not set a nickname. */ - private String getUserNickname(String id) { + private static String getUserNickname(String id) { Query query = new Query("UserInfo") .setFilter(new Query.FilterPredicate("id", Query.FilterOperator.EQUAL, id));