<chapter 2>2-2-2. Developing Java Applications (developing a Java app)

Source: Internet
Author: User

The Java Web application of App engine uses the Java Servlet standard interface to interact with the application server. An application consists of one or more servlet classes, each of which extends the (extend) Servlet base class. Use a standard configuration file called the deployment Description (Deployment descriptor), which means that web.xml,servlets is mapped to URLs. When the app engine accepts a request for a Java application, it determines which servlet class to use based on the URL and deployment description, instantiates the class, and then invokes the appropriate method in the Servlet object.

Java applications All files, including compiled Java classes, configuration files, static files, are managed with a standard directory structure called Web application archive or "WAR". Everything in the war directory is deployed to the app engine. You typically use an automated build process or war-aware development tool in your development workflow to build the contents of a war against a set of source code files.

If you use the Eclipse IDE with the Google plugin, you can use the Web Application Wizard to create a new project. Click the Google drop-down button and select New Web application Project. (Optionally, choose New from the File menu, then Web Application project) in this open form, enter a project name (such as clock) and a package name (such as clock).

Uncheck the "Use Google Web Toolkit" checkbox and make sure the "use Google App Engine" check box is checked. (If you make GWT check box selected, this new item will be created with the GWT startup file.) It's cool, but it's beyond the scope of this chapter. Figure 2-8 shows the completion dialog box for creating a clock app. Click Finish to create the project.

If you don't use Google Plugin for Eclipse, you'll need to create this directory and file separately. If you are already familiar with Java network development, you can use your existing tools and processes (tools and processes) to generate the final war. For the remainder of this section, we assume that you use the directory structure created by the Eclipse plug-in.

Figure 2-9 shows the structure of the project file, which is described in the Eclipse Package Browse window.

The root path of this project (Clock) contains two primary subdirectories: SRC and war. The src/directory contains all of the project's class files and uses the Java package structure. In the clock packet path, Eclipse creates a source code for a servlet class called Clockservlet in file Clock/clockservlet.java.

The war/directory contains the full final content of the app (the complete final contents). Eclipse automatically compiles the source code from src/and places the compiled class file into the war/web-inf/classes/directory, which is hidden by default in the Eclipse Package Browse window. Eclipse also automatically copies the contents of src/meta-inf/into War/web-inf/classes/meta-inf. All other content, such as CSS or JavaScript files, must be created in a predetermined location in the war/directory.

Let's start our clock application with a simple servlet that shows the current time. Open and edit the Src/clock/clockservlet.java file (if necessary, create it), and give it something like example 2-9.

Example 2-9. A simple Java servlet

Package ClockImportjava.io.IOException;ImportJava.io.PrintWriter;ImportJava.text.SimpleDataFormat;Importjava.util.Date;ImportJava.util.SimpleTimeZone;Importjavax.servlet.http.*; @SuppressWarning ("Serial") PublicclassClockservlet extends httpservlet{public void doget (HttpServletRequest req,httpservletreponse resp) Throw ioexception{SimpleDateFormat FMT= New SimpleDateFormat ("yyyy-mm-dd Hh:mm:ss. Ssssss"); Fmt.settimezone (New SimpleTimeZone (0,""); Resp.setcontenttype ("text/html"); PrintWriter out=Resp.getwriter (); Out.println ("<p>the Time is:"+ Fmt.format (new Date ()) +"</p>"); }}

<chapter 2>2-2-2. Developing Java Applications (developing a Java app)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.