`
sillycat
  • 浏览: 2560104 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

    基于Google.App.Engine(GAE)的Java和GWT应用开发.pdf

    原名: Google App Engine Java and GWT Application Development 作者: Daniel Guermeur, Amy Unruh 资源格式: PDF 版本: 文字版 出版社: Packt Publishing书号: 1849690448发行时间: 2010年 地区: 美国 语言: 英文 ...

    Go.Programming.Blueprints.2nd

    Get a feel for app deployment using Docker and Google App Engine Table of Contents Chapter 1: Chat Application with Web Sockets Chapter 2: Adding User Accounts Chapter 3: Three Ways to Implement ...

    Deliver.Audacious.Web.Apps.with.Ember2

    In this book, you'll learn these new features: how to use module-driven development with Ember CLI, take advantage of the new DOM-based rendering engine, and use a service-based architecture to make ...

    Beginning Android Games

     You'll start with game design fundamentals and programming basics, and then progress toward creating your own basic game engine and playable game apps that work on Android and earlier version ...

    .Learning.iPhone.Game.Development.with.Cocos2D.3.0

    Use the physics engine to apply forces and detect collisions Add sounds and music and change playback properties Integrate your game with Game Center and make In-App purchases Approach This book is an...

    Python匡架yate

    Python框架Yate,全称为Yet Another Template Engine,是一个基于模板的Python框架,旨在简化Web应用的开发过程。它利用模板语言将静态内容与动态逻辑分离,使得开发者可以专注于业务逻辑,而设计师则可以专注于页面...

    Linux CentOS6.5源码安装多个 mariadb10.0.15

    scripts/mysql_install_db --basedir=/app/mysql1/mysql --datadir=/app/mysql1/mysql/data --user=mysql ``` **4. 设置配置文件** 修改默认的配置文件,以便正确配置第一个实例: ```bash cp support-files...

    Linux环境搭建流程文档

    8. 启动MySQL server:执行命令service mysqld start,启动MySQL server。 三、安装Nginx (略) 四、安装Redis (略) 五、安装MinIO (略) 本文档提供了一个详细的Linux环境搭建流程,涵盖了JDK、MySQL、...

    python-app-with-electron-gui:为python应用程序制作GUI的更好方法

    python-app-with-electron-gui 为Python应用制作GUI的更好方法 使用HTML,JavaScript和CSS制作高度定制的跨平台桌面应用程序,这些应用程序使用本机Python后端。 注意:这仅出于教育目的,可能不是有效的或没有...

    社区绿色版mysql的安装与配置1

    ##### 3. 创建`my.ini`配置文件 - 在MySQL安装目录下创建一个名为`my.ini`的文本文件,用于配置MySQL服务器的基本参数。 - 内容示例: ```ini [client] port = 3306 socket = /data/3306/mysql.sock default-...

    OpenShift-Example:在OpenShift的单个节点上部署的三层应用程序

    示例:OpenShift中单个节点上的三层应用程序 此示例显示了如何在OpenShift上部署三层应用... oc new-app --name=mysql5 --docker-image=mysql:5.7 \ -e MYSQL_USER=user1 -e MYSQL_PASSWORD=mypa55 -e MYSQL_DATABASE=

    Django+celery+rabbitmq配置文档

    - 启动服务命令: `service rabbitmq-server start`。 #### 二、配置详解 ##### 2.1 Django项目配置 在Django项目中需要配置数据库信息以便于Celery能够存储任务状态等信息。具体配置如下: - 在`settings.py`中...

    ADB常用命令和路径

    - **格式**: `adb shell pm list packages [-f][-d][-e][-s][-3][-i][-u][–user USER_ID][FILTER]` - **参数说明**: - `-f`: 显示APK文件路径。 - `-d`: 只显示已启用的组件。 - `-e`: 只显示已禁用的组件。 ...

    SQL部分函数使用说明

    1表示Desktop Engine,2表示标准版,3表示企业版。 - instancename:返回服务器实例的名称,默认实例返回空字符串。 - productversion:返回SQL Server的版本号。 - productlevel:返回SQL Server的具体版本,如...

    MySql5.7.11编译安装及修改root密码的方法小结

    service mysqld start ``` 接下来是修改 root 用户密码的步骤。在安装过程中,MySQL 会自动生成一个随机的 root 密码,你可以通过以下命令查看日志文件找到这个临时密码: ``` less /var/log/mysqld.log ``` ...

    Express:快速,灵活且极简的Node.js Web框架

    app.set('view engine', 'ejs'); // 设置EJS为默认模板引擎 app.get('/profile', (req, res) =&gt; { res.render('profile', { user: 'John Doe' }); // 渲染profile.ejs并传入数据 }); ``` **6. 开发环境与生产环境*...

    express_tutorial

    app.set('view engine', 'ejs'); app.set('views', './views'); app.get('/', (req, res) =&gt; { res.render('index', { title: 'Home Page' }); }); ``` 创建`views`目录和`index.ejs`文件,编写HTML内容。 七、...

    mysql8.0.11 winx64手动安装配置教程

    3. 编写配置文件 在MySQL的解压目录下创建一个名为my.ini的配置文件,文件内容应包括设置数据库端口、安装目录、数据目录、字符集以及默认存储引擎等。例如: ``` [mysql] default-character-set=utf8 [mysqld...

Global site tag (gtag.js) - Google Analytics