zoukankan      html  css  js  c++  java
  • Google App Engine使用

    下载App Engine SDK

    下载App Engine SDK。

    然后通过:

    javac -version

    查看Java JDK安装版本,确定自己安装的是Java 6 JDK。

    如果版本较低,那么去官网下一个。

    Servlet

    Servlet = Server + Applet

    即Servlet是:服务器端小程序。

    文件结构

    项目的文件结构是怎样的呢?

    project/
      src/
        ...Java source code...
        META-INF/
          ...other configuration...
      war/
        ...JSPs, images, data files...
        WEB-INF/
          ...app configuration...
          lib/
            ...JARs for libraries...
          classes/
            ...compiled classes...

    其中project是项目目录。

    使用Apache Ant

    • 首先先确认ant的版本,在终端输入:

    ant -version

    • 打开终端,创建相应的路径:
    mkdir project
    cd project
    mkdir -p src/META-INF
    mkdir -p war/WEB-INF/classes
    mkdir -p war/WEB-INF/lib
    • 然后在项目根目录下创建ant配置文件build.xml:
    <project>
      <property name="sdk.dir" location="../appengine-java-sdk" />
    
      <import file="${sdk.dir}/config/user/ant-macros.xml" />
    
    </project>

    sdk.dir指向app engine sdk的路径。

    • 定义库路径:
      <path id="project.classpath">
        <pathelement path="war/WEB-INF/classes" />
        <fileset dir="war/WEB-INF/lib">
          <include name="**/*.jar" />
        </fileset>
        <fileset dir="${sdk.dir}/lib">
          <include name="shared/**/*.jar" />
        </fileset>
      </path>
    • 复制库规则:
      <target name="copyjars"
          description="Copies the App Engine JARs to the WAR.">
        <copy
            todir="war/WEB-INF/lib"
            flatten="true">
          <fileset dir="${sdk.dir}/lib/user">
            <include name="**/*.jar" />
          </fileset>
        </copy>
      </target>

    然后在终端输入:

    ant copyjars

    则可将库文件复制到war/WEB-INF/lib中。

    • 编译源文件规则:
      <target name="compile" depends="copyjars"
          description="Compiles Java source and copies other source files to the WAR.">
        <mkdir dir="war/WEB-INF/classes" />
        <copy todir="war/WEB-INF/classes">
          <fileset dir="src">
            <exclude name="**/*.java" />
          </fileset>
        </copy>
        <javac
            srcdir="src"
            destdir="war/WEB-INF/classes"
            classpathref="project.classpath"
            debug="on" />
      </target>

    现在我们可以用:

    ant compile

    执行编译。

    • 运行服务器:
      <target name="runserver" depends="datanucleusenhance"
          description="Starts the development server.">
        <dev_appserver war="war" />
      </target>

    则,我们可以用:

    ant runserver

    运行服务器。

    • 上传配置:
      <target name="update" depends="datanucleusenhance"
          description="Uploads the application to App Engine.">
        <appcfg action="update" war="war" />
      </target>
    
      <target name="update_indexes" depends="datanucleusenhance"
          description="Uploads just the datastore index configuration to App Engine.">
        <appcfg action="update_indexes" war="war" />
      </target>
    
      <target name="rollback" depends="datanucleusenhance"
          description="Rolls back an interrupted application update.">
        <appcfg action="rollback" war="war" />
      </target>
    
      <target name="request_logs"
          description="Downloads log data from App Engine for the application.">
        <appcfg action="request_logs" war="war">
          <options>
            <arg value="--num_days=5"/>
          </options>
          <args>
            <arg value="logs.txt"/>
          </args>
        </appcfg>
      </target>
    • 完整配置
    <project>
      <property name="sdk.dir" location="../appengine-java-sdk" />
    
      <import file="${sdk.dir}/config/user/ant-macros.xml" />
    
      <path id="project.classpath">
        <pathelement path="war/WEB-INF/classes" />
        <fileset dir="war/WEB-INF/lib">
          <include name="**/*.jar" />
        </fileset>
        <fileset dir="${sdk.dir}/lib">
          <include name="shared/**/*.jar" />
        </fileset>
      </path>
    
      <target name="copyjars"
          description="Copies the App Engine JARs to the WAR.">
        <copy
            todir="war/WEB-INF/lib"
            flatten="true">
          <fileset dir="${sdk.dir}/lib/user">
            <include name="**/*.jar" />
          </fileset>
        </copy>
      </target>
    
      <target name="compile" depends="copyjars"
          description="Compiles Java source and copies other source files to the WAR.">
        <mkdir dir="war/WEB-INF/classes" />
        <copy todir="war/WEB-INF/classes">
          <fileset dir="src">
            <exclude name="**/*.java" />
          </fileset>
        </copy>
        <javac
            srcdir="src"
            destdir="war/WEB-INF/classes"
            classpathref="project.classpath"
            debug="on" />
      </target>
    
      <target name="datanucleusenhance" depends="compile"
          description="Performs JDO enhancement on compiled data classes.">
        <enhance_war war="war" />
      </target>
    
      <target name="runserver" depends="datanucleusenhance"
          description="Starts the development server.">
        <dev_appserver war="war" />
      </target>
    
      <target name="update" depends="datanucleusenhance"
          description="Uploads the application to App Engine.">
        <appcfg action="update" war="war" />
      </target>
    
      <target name="update_indexes" depends="datanucleusenhance"
          description="Uploads just the datastore index configuration to App Engine.">
        <appcfg action="update_indexes" war="war" />
      </target>
    
      <target name="rollback" depends="datanucleusenhance"
          description="Rolls back an interrupted application update.">
        <appcfg action="rollback" war="war" />
      </target>
    
      <target name="request_logs"
          description="Downloads log data from App Engine for the application.">
        <appcfg action="request_logs" war="war">
          <options>
            <arg value="--num_days=5"/>
          </options>
          <args>
            <arg value="logs.txt"/>
          </args>
        </appcfg>
      </target>
    
    </project>

    For Example

    package example;
    
    import java.io.IOException;
    import javax.servlet.http.*;
    
    //写一个Servlet类,继承自javax.servlet.http.HttpServlet
    public class myServlet extends HttpServlet{
        //处理HTTP GET请求的方法
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            resp.setContentType("text/html");
            resp.getWriter().println("<p>Hello, world</p>");
        }    
    }

    我们在src文件夹下,创建一个myServlet.java文件。因为我们把他放在example package中,所以还要放在example目录中。真实的路径是:

    project/src/example/myServlet.java

    配置部署描述符文件

    创建文件war/WEB-INF/web.xml,修改内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
        <servlet>
            <servlet-name>example.</servlet-name>
            <servlet-class>example.myServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>example</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    
    </web-app>

    配置App Engine配置文件

    创建war/WEB-INF/appengine-web.xml,修改内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
        <application></application>
        <version>1</version>
    </appengine-web-app>

    OK,我们可以用:

    ant runserver

    通过http://localhost:8080打开我们的服务页面了。

  • 相关阅读:
    主机与虚拟机之间的网络连接
    QT下过多点的曲线绘制
    C++返回对象和返回引用
    STS MVC与MyBatis的结合
    STS中依赖项的设置
    STS中MyBatis的基本实现
    STS中不同包但相同类名引起的问题:A component required a bean of type 'javax.activation.DataSource' that could not be found
    STS中AOP的实现
    STS如何将一个文件夹设置缺省的创建路径(build path)
    ARB扩展与标准OpenGL的关系
  • 原文地址:https://www.cnblogs.com/justany/p/2817217.html
Copyright © 2011-2022 走看看