2013-08-06 403 views
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時,我希望看到歡迎,但它會登錄並註冊,即使我登錄到Gmail。我的代碼可能存在一些問題嗎?Google用戶帳戶

回答

0

應用程序沒有自動訪問您的Gmail帳戶,也不會自動登錄到您的應用程序,如果你登錄到你的Gmail。這非常危險 - 如果用戶訪問了他們的網址,所有GAE應用程序都可以收集用戶的電子郵件。

用戶仍然需要登錄到您的應用,例如,通過聯合登錄。