Google App Engine(3)Java Start and User Service
User service lets your application integrate with Google user accounts.
Using Users
Change the servlet something like following:
publicvoid doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user != null) {
resp.setContentType("text/plain");
resp.getWriter().println(
"Hello, " + user.getNickname() + ". Your email will be "
+ user.getEmail());
} else {
resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
}
}
Eclipse compiles the new code automatically, then attempts to insert the new code into the already-running server. Changes to classes, JSPs static files and app engine-web.xml are reflected immediately in the running server without needing to restart.
Only if I change web.xml or other configuration files, I need to stop and start the server.
When this runs on local machine, the redirect goes to the page where we can enter any email address to simulate an account sign-in.
Using JSPs
We can do output the HTML in servlets, but that would be difficult to maintain as the HTML get complicated.
Hello, JSP
The content of the JSP page will be mostly like I design my old JSP projects in guestbook.jsp under war directory:
<%@pagecontentType="text/html;charset=UTF-8"language="java"%>
<%@pageimport="java.util.List"%>
<%@pageimport="com.google.appengine.api.users.User"%>
<%@pageimport="com.google.appengine.api.users.UserService"%>
<%@pageimport="com.google.appengine.api.users.UserServiceFactory"%>
<%@taglibprefix="fn"uri="http://java.sun.com/jsp/jstl/functions"%>
<html>
<body>
<%
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user != null) {
pageContext.setAttribute("user", user);
%>
<p>
Hello, ${fn:escapeXml(user.nickname)}! (You can <a
href="<%=userService.createLogoutURL(request.getRequestURI())%>">sign
out ${fn:escapeXml(user.email)}</a>.)
</p>
<%
} else {
%>
<p>
Hello! <a
href="<%=userService.createLoginURL(request.getRequestURI())%>">Sign
in</a> to include your name with greetings you post.
</p>
<%
}
%>
</body>
</html>
I can visit this page for verification:
http://localhost:8888/guestbook.jsp
The Guestbook Form
Add the very simple form in guestbook.jsp
<formaction="/sign"method="post">
<div><textareaname="content"rows="3"cols="60"></textarea></div>
<div><inputtype="submit"value="Post Greeting"/></div>
</form>
A new servlet to handle the post message SignGuestbookServlet.java
Simple, very simple servlet to output some greeting message in console.
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
String content = req.getParameter("content");
if (content == null) {
content = "(No greeting)";
}
if (user != null) {
log.info("Greeting posted by user " + user.getNickname() + ": "
+ content);
} else {
log.info("Greeting posted anonymously: " + content);
}
resp.sendRedirect("/guestbook.jsp");
}
Do the mapping according to the sample of GuestbookServlet
The logging configuration file is logging.properties and it is already configured in file app engine-web.xml:
<system-properties>
<propertyname="java.util.logging.config.file"value="WEB-INF/logging.properties"/>
</system-properties>
The default log level is WARNING, which suppresses INFO message.
guestbook.level = INFO, but for me, it should be
com.sillycat.easymymessage.level = INFO
Yes, I verified from this URL http://carl.digby.com:8888/guestbook.jsp. It works.
References:
https://developers.google.com/appengine/docs/java/gettingstarted/usingusers
https://developers.google.com/appengine/docs/java/gettingstarted/usingjsps
User service lets your application integrate with Google user accounts.
Using Users
Change the servlet something like following:
publicvoid doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user != null) {
resp.setContentType("text/plain");
resp.getWriter().println(
"Hello, " + user.getNickname() + ". Your email will be "
+ user.getEmail());
} else {
resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
}
}
Eclipse compiles the new code automatically, then attempts to insert the new code into the already-running server. Changes to classes, JSPs static files and app engine-web.xml are reflected immediately in the running server without needing to restart.
Only if I change web.xml or other configuration files, I need to stop and start the server.
When this runs on local machine, the redirect goes to the page where we can enter any email address to simulate an account sign-in.
Using JSPs
We can do output the HTML in servlets, but that would be difficult to maintain as the HTML get complicated.
Hello, JSP
The content of the JSP page will be mostly like I design my old JSP projects in guestbook.jsp under war directory:
<%@pagecontentType="text/html;charset=UTF-8"language="java"%>
<%@pageimport="java.util.List"%>
<%@pageimport="com.google.appengine.api.users.User"%>
<%@pageimport="com.google.appengine.api.users.UserService"%>
<%@pageimport="com.google.appengine.api.users.UserServiceFactory"%>
<%@taglibprefix="fn"uri="http://java.sun.com/jsp/jstl/functions"%>
<html>
<body>
<%
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user != null) {
pageContext.setAttribute("user", user);
%>
<p>
Hello, ${fn:escapeXml(user.nickname)}! (You can <a
href="<%=userService.createLogoutURL(request.getRequestURI())%>">sign
out ${fn:escapeXml(user.email)}</a>.)
</p>
<%
} else {
%>
<p>
Hello! <a
href="<%=userService.createLoginURL(request.getRequestURI())%>">Sign
in</a> to include your name with greetings you post.
</p>
<%
}
%>
</body>
</html>
I can visit this page for verification:
http://localhost:8888/guestbook.jsp
The Guestbook Form
Add the very simple form in guestbook.jsp
<formaction="/sign"method="post">
<div><textareaname="content"rows="3"cols="60"></textarea></div>
<div><inputtype="submit"value="Post Greeting"/></div>
</form>
A new servlet to handle the post message SignGuestbookServlet.java
Simple, very simple servlet to output some greeting message in console.
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
String content = req.getParameter("content");
if (content == null) {
content = "(No greeting)";
}
if (user != null) {
log.info("Greeting posted by user " + user.getNickname() + ": "
+ content);
} else {
log.info("Greeting posted anonymously: " + content);
}
resp.sendRedirect("/guestbook.jsp");
}
Do the mapping according to the sample of GuestbookServlet
The logging configuration file is logging.properties and it is already configured in file app engine-web.xml:
<system-properties>
<propertyname="java.util.logging.config.file"value="WEB-INF/logging.properties"/>
</system-properties>
The default log level is WARNING, which suppresses INFO message.
guestbook.level = INFO, but for me, it should be
com.sillycat.easymymessage.level = INFO
Yes, I verified from this URL http://carl.digby.com:8888/guestbook.jsp. It works.
References:
https://developers.google.com/appengine/docs/java/gettingstarted/usingusers
https://developers.google.com/appengine/docs/java/gettingstarted/usingjsps
本文详细介绍了如何使用Google App Engine通过Java实现用户认证与账户集成,包括用户信息获取、登录与登出操作,以及利用JSP进行用户界面展示。通过示例代码展示了如何使用Google UserService进行用户交互,并提供了简单的Web应用实例,如Guestbook系统,实现了用户注册、登录、发布问候等基本功能。

被折叠的 条评论
为什么被折叠?



