Creating a Project –节译

The Project Directory

For this tutorial, we will use a single directory named Guestbook/ for all project files. A subdirectory named src/ contains the Java source code, and a subdirectory named war/ contains the complete application arranged in the WAR format. Our build process compiles the Java source files and puts the compiled classes in the appropriate location in war/.

The complete project directory looks like this:

项目目录结构

在这个指导中,我们将使用一个简单的名字“Guestbook”作为项目的名字,项目中一个名为src的子目录,包含了java的源代码,一个叫war的子目录包含了符合war 格式标准的文件。我们的编译器编译java源文件并将编译好的class文件放在war相应的目录下 这个完整项目的目录看起来像这样:

Guestbook/  

   src/   

     ...Java source code...    

     META-INF/     

     ..other configuration...  

war/    

      ...JSPs, images, data files...    

    WEB-INF/      

     ...app configuration...      

   lib/        

  ...JARs for libraries...      

   classes/        

 .  ..compiled classes...

If you are using Eclipse, create a new project by clicking the New Web Application Project button in the toolbar: The New Web Application Project button.Give the project a "Project name" of Guestbook and a "Package" of guestbook. Uncheck "Use Google Web Toolkit," and ensure "Use Google App Engine" is checked. See Using the Google Plugin for Eclipse for more information. The wizard creates the directory structure, and the files described below.

如果使用的是Eclipse,用 The New Web Application Project button.即可创建一个web 应用,起名:项目名为Guestbook,同时包名为”guestbook”.

If you are not using Eclipse, create the directory structure described above. As you read each of the files described in this section, create the files using the given locations and names.

如果不是使用Eclipse,只需要按如上目录结构创建应用。

You can also copy the new project template included with the SDK, in the appengine-java-sdk/demos/new_project_template/ directory.

你也可以直接从”appengine-java-sdk/demos/new_project_template/”目录下复制这个项目的模版。

The Servlet Class

App Engine Java applications use the Java Servlet API to interact with the web server. An HTTP servlet is an application class that can process and respond to web requests. This class extends either the javax.servlet.GenericServlet class or the javax.servlet.http.HttpServlet class.

App Engine java应用使用java server api与服务器端交互,一个HTTP Servlet是一个能够处理和响应网络请求的应用,这个servlet类继承了javax.servlet.GenericServlet类或者javax.servlet.http.HttpServlet

Our guest book project begins with one servlet class, a simple servlet that displays a message.

这个guest book项目从一个可以显示一条消息的seervlet类开始。

If you are not using the Eclipse plugin, create the directories for the path src/guestbook/, then create the servlet class file described below.

如果你没使用Eclipse插件,在应用中创建src/guestbook目录,然后创建这个servlet类文件,描述如下:

In the directory src/guestbook/, a file named GuestbookServlet.java has the following contents:

在目录src/guestbook/下有一个叫GuestbookServlet.java的文件包含如下内容:

package guestbook;

import java.io.IOException;
import javax.servlet.http.*;

public class GuestbookServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        resp.setContentType("text/plain");
        resp.getWriter().println("Hello, world");
    }
}
The web.xml File

When the web server receives a request, it determines which servlet class to call using a configuration file known as the "web application deployment descriptor." This file is named web.xml, and resides in the war/WEB-INF/ directory in the WAR. WEB-INF/ and web.xml are part of the servlet specification.

web服务器接收到一个请求,服务器通过查找一个叫“web应用程序部署描述符的文件来决定调用哪一个servlet类处理相应请求。这个文件名叫“web.xml”。该文件在war/WEB-INF/目录下。WEB-INF目录和web.xml文件是servlet规范的一部分。

In the directory war/WEB-INF/, a file named web.xml has the following contents:

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>guestbook</servlet-name>         <servlet-class>guestbook.GuestbookServlet</servlet-class>     </servlet>     <servlet-mapping>         <servlet-name>guestbook</servlet-name>         <url-pattern>/guestbook</url-pattern>     </servlet-mapping>     <welcome-file-list>        <welcome-file>index.html</welcome-file>     </welcome-file-list> </web-app>

This web.xml file declares a servlet named guestbook, and maps it to the URL path /guestbook. It also says that whenever the user fetches a URL path that is not already mapped to a servlet and represents a directory path inside the application's WAR, the server should check for a file named index.html in that directory and serve it if found.

这个web.xml文件声明了一个名为guestbookservlet,它映射的url/guestbook,它同时也指出了如果一个请求的URL,war目录下没有与之匹配的servlet配置和代表的目录路径,服务器将会在该目录下查找一个名为index.html文件,如果找到,将此文件返回给请求。

The appengine-web.xml File

App Engine needs one additional configuration file to figure out how to deploy and run the application. This file is named appengine-web.xml, and resides in WEB-INF/ alongside web.xml. It includes the registered ID of your application (Eclipse creates this with an empty ID for you to fill in later), the version number of your application, and lists of files that ought to be treated as static files (such as images and CSS) and resource files (such as JSPs and other application data).

App Engine 需要一个附加的配置文件去指示如何部署和运行程序。这个文件名叫“appengine-web.xml”,并且与web.xml在同一目录,它包含了你应用注册的IDEclipse自动创建的appengine-web.xml中不包含此ID,而是当你在部署时才叫你填写),应用程序的版本号和一些被视为静态文件的文件(如图片和CSS)和资源文件(如jsps和其它应用程序数据)。

In the directory war/WEB-INF/, a file named appengine-web.xml has the following contents:

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>

appengine-web.xml is specific to App Engine, and is not part of the servlet standard. You can find XML schema files describing the format of this file in the SDK, in the appengine-java-sdk/docs/ directory. See Configuring an App for more information about this file.

appengine-web.xmlApp Engine是特殊的,它不是servlet标准的一部分,你能够在sdkappengine-java-sdk/docs/目录下找到该xml的格式描述文件,对于此文件更详细的信息在Configuring an App上。

Running the Project

The App Engine SDK includes a web server application you can use to test your application. The server simulates the App Engine environment and services, including sandbox restrictions, the datastore, and the services.

If you are using Eclipse, you can start the development server within the Eclipse debugger. In the Run menu, select Debug As > Web Application. (If you do not see this menu option, select the Window menu > Reset Perspective..., click OK, then try the Run menu again.) See Using the Google Plugin for Eclipse for details on creating the debug configuration.

如果使用的是Eclipse,直接点Run菜单。Debug As > Web Application.

If you are not using Eclipse, see Using Apache Ant for a build script that can build the project and start the development server. To start the server with this build script, enter the following command: ant runserver To stop the server, hit Control-C.

如果你不是使用Eclipse,Using Apache Ant,通过一个构建脚本能够部署这个项目,并启动服务。启动服务使用如下命令:ant runserver,要停止服务,点击ctrl+c;

Testing the Application

Start the server, then visit the following URL in your browser:

The server calls the servlet, and displays the message in the browser.

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值