0
            package clock;

            import java.io.IOException;
            import java.text.SimpleDateFormat;
            import java.util.Date;
            import java.util.SimpleTimeZone;
            import javax.servlet.RequestDispatcher;
            import javax.servlet.ServletException;
            import javax.servlet.http.*;

            import com.google.appengine.api.users.User;
            import com.google.appengine.api.users.UserService;
            import com.google.appengine.api.users.UserServiceFactory;

            @SuppressWarnings("serial")
            public class ClockServlet extends HttpServlet {
                public void doGet(HttpServletRequest req,
                                  HttpServletResponse resp)
                    throws IOException, ServletException {
                    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
                    fmt.setTimeZone(new SimpleTimeZone(0, ""));

                    UserService userService = UserServiceFactory.getUserService();
                    User user = userService.getCurrentUser();
                    String loginUrl = userService.createLoginURL("/");
                    String logoutUrl = userService.createLogoutURL("/");

                    req.setAttribute("user", user);
                    req.setAttribute("loginUrl", loginUrl);
                    req.setAttribute("logoutUrl", logoutUrl);
                    req.setAttribute("currentTime", fmt.format(new Date()));

                    resp.setContentType("text/html");

                    RequestDispatcher jsp = req.getRequestDispatcher("/WEB-INF/home.jsp");
                    jsp.forward(req, resp);
                }
            }



            <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
            <html>
              <head>
                <title>The Time Is...</title>
              </head>
              <body>
                <c:choose>
                  <c:when test="${user != null}">
                    <p>
                      Welcome, ${user.email}!
                      You can <a href="${logoutUrl}">sign out</a>.
                    </p>
                  </c:when>
                  <c:otherwise>
                    <p>
                      Welcome!
                      <a href="${loginUrl}">Sign in or register</a> to customize.
                    </p>
                  </c:otherwise>
                </c:choose>
                <p>The time is: ${currentTime}</p>
              </body>
            </html>

由于我在运行示例时登录了我的 gmail,因此我希望看到 Welcome,但即使我登录了 gmail,它也会登录并注册。我的代码可能有问题吗?

4

1 回答 1

0

应用程序不会自动访问您的 gmail 帐户,如果您登录到您的 gmail,您也不会自动登录到您的应用程序。那将是非常危险的——如果用户访问了他们的 URL,所有 GAE 应用程序都可以收集用户电子邮件。

用户仍然需要登录您的应用程序,例如通过联合登录。

于 2013-08-07T10:40:00.040 回答