Google App Engine(3)Java Start and User Service

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值