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,它也会登录并注册。我的代码可能有问题吗?
答案 0 :(得分:0)
应用程序无法自动访问您的Gmail帐户,如果您登录到Gmail,也无法自动登录您的应用程序。这将非常危险 - 如果用户访问了他们的网址,所有GAE应用都可以收集用户的电子邮件。
用户仍需要登录您的应用,例如通过联合登录。