Google App Engine provides several useful services based on Google infrastructure, accessible by applications using libraries included with the SDK. One such service is the Users service, which lets your application integrate with Google user accounts. With the Users service, your users can use the Google accounts they already have to sign in to your application.
Let's use the Users service to personalize this application's greeting.
大意:在google原有架构的基础上,Google App Engine提供了许多有用的服务。这些服务可以通过使用SDK libraries来获得。其中有一个服务是用户服务,这个服务使你的应用能够与google 用户账户交互。通过这个服务,用户可以使用已有的google账户登录你的应用。
通过使用这个用户服务,使我们的程序实现个性化的问候。
Using Users
Edit src/guestbook/GuestbookServlet.java as indicated to resemble the following:
编辑src/guestbook/GuestbookServlet.java,使其内容与如下相似:
package guestbook;
import java.io.IOException;
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;
public class GuestbookServlet extends HttpServlet {
public void 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());
} else {
resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
}
}
}
If you are using Eclipse and your development server is running in the debugger, when you save your changes to this file, 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 appengine-web.xml are reflected immediately in the running server without needing to restart. If you change web.xml or other configuration files, you must stop and start the server to see the changes.
大意:如果你使用的是Eclipse并且你的应用服务器处于debugger模式,当你保存文件时,Eclipse会自动编译新的代码,并尝试把这些代码放入已经在运行的服务器里,原有的类文件,jsp文件,静态文件和appengin-web.xml立即被映射到正在运行的服务器中,你也不用重新启动服务器。如果你改变了web.xml或者其它配置文件,为了看到这些变化,你必须要停止再启动服务。
If you are using Ant, you must stop the server and rebuild the project to see changes made to source code. Changes to JSPs and static files do not require restarting the server.
大意:如果你使用的是Ant,你必须停止服务并重新编译项目才能看到改变。只改变jsp文件和静态文件不需要重启服务。
Rebuild your project and restart the server, if necessary. Test the application by visiting the servlet URL in your browser:
重新创建你的项目,重启服务,如果有必要,在浏览器里通过如下地址测试:
Instead of displaying the message, the server prompts you for an email address. Enter any email address (such as alfred@example.com, then click "Log In." The app displays a message, this time containing the email address you entered.
这时会显示一个消息,服务提示你输入一个email,输入任何email地址,如alfred@example.com,然后点击Log in,这个应用将显示一条消息,这次这条消息中包含你输入的email地址
The new code for the GuestbookServlet class uses the Users API to check if the user is signed in with a Google Account. If not, the user is redirected to the Google Accounts sign-in screen.
大意:这些代码使用Users API确认是否使用google账号登陆,如果没有,则将用户直接转向google账户登录界面。
userService.createLoginURL(...) returns the URL of the sign-in screen. The sign-in facility knows to redirect the user back to the app by the URL passed to createLoginURL(...), which in this case is the URL of the current page.
大意:userService.createLoginURL(...)这个方法返回的是登入界面的URL,这个登录sign-in又提交这个应用到当前页面。
The development server knows how to simulate the Google Accounts sign-in facility. When run on your local machine, the redirect goes to the page where you can enter any email address to simulate an account sign-in. When run on App Engine, the redirect goes to the actual Google Accounts screen.
大意:开发服务器知道如何模拟谷歌帐户登录,当运行在本机上时,直接重定向到当前页面,这个页面能够模拟输入一个email账号,当运行在App Engine时,就直接重定向到真实的google账户登陆界面。
You are now signed in to your test application. If you reload the page, the message will display again.
大意:你现在可以登录你的应用。如果你重新载入页面,这个消息会再显示一遍。
To allow the user to sign out, provide a link to the sign-out screen, generated by the method createLogoutURL(). Note that a sign-out link will sign the user out of all Google services.
大意:通过使用createLogoutURL(),让你的应用提供一个登出界面。必须注意的是,一个sign-out链接将会使得当前用户退出google所有的服务。