手机版

用 Tomcat 和 Eclipse开发 Web 应用程序英文原文及中文翻译

发布时间:2024-11-18   来源:未知    
字号:

毕业设计(外文翻译)

Developing Web applications with

Tomcat and Eclipse

Required components

The Eclipse V3.2 Callisto integrated development environment (IDE) includes tools for doing Web development and integrating with servers. So, aside from the software development kit (SDK), you need only Eclipse and Apache Tomcat installed. The versions featured in this article are listed below.

Table 1. Components and versions used

Component Version used in this article

Eclipse Platform 3.2.2

Java EE 5 SDK 1.5.0_09

Apache Tomcat 5.5.20

The Eclipse IDE is an open source IDE used as the development environment for your Java™Server Pages (JSPs) and Java files. With the Web and Java 2 Platform, Enterprise Edition (J2EE) Development tools installed, Eclipse is great tool for creating HTML, JSPs, and servlets.

You need the Java SDK to run servlets on your machine. Finally, Apache Tomcat is an open source Web and servlet container, used in the official reference implementations for Java Servlet and Java Server Pages. Installation

Installation of Eclipse and Tomcat is similar —both are extracted from the archive file format into a folder on your hard disk. If you ever want to uninstall either, it's as simple as deleting the directory in which you extracted the files.

Install Eclipse

1

毕业设计(外文翻译)

2 To install the Eclipse IDE, unpack the compressed (.zip or .tar.gz) file you downloaded from Eclipse, then move the unpacked folder into a convenient location. I try to install it in a location that follows the conventions of whatever operating system I'm using. If I'm installing Eclipse on a Macintosh, I put the Eclipse folder in the Applications folder. If I'm using Microsoft™ Windows™, I put the unpacked folder into the C:\Program Files directory.

Once you've downloaded and installed Eclipse, start it up. Before you can create a Web project, you need to install the J2EE and Web Development plug-ins. Fortunately, it's easy to install the plug-in using the Callisto Discovery Site.

Install the Web tools

With the Eclipse IDE running, select Help > Software Updates > Find and Install. This option lets you download and install the Web tools without going to a Web site.

Select Search for New Features to Install, as shown below, then click

Next.

Figure 1. Install/Update window

Select Callisto Discovery Site, as shown below, then click Next.

毕业设计(外文翻译)

3

Figure 2. Select the Callisto Discovery Site

Under Callisto Discovery Site, check the Web and J2EE Development box. The wizard immediately warns you that you're missing dependencies. Click Select Required, and these warnings should disappear. Some or all of the components in the Graphical Editors and Frameworks and Models and Model Development categories are selected (as shown in Figure 3) because they're

required for the installation of the Web and J2EE plug-ins.

Figure 3. Select features for installation

毕业设计(外文翻译)

Click Next to continue, and license agreements appear for each feature you've selected. If you agree with the licenses, accept them and follow the wizard through the rest of the process. When the IDE is finished installing the plug-ins, it asks you to restart Eclipse in order for the changes to take effect. Be sure you restart the IDE before continuing.

After you restart the IDE, the new features are available to begin developing dynamic Web projects.

Install Tomcat

To install Apache Tomcat, extract the files from the downloaded archive and place them into a directory. I put them in my C:\apps directory to make them easy to locate later. That's it for now; use Eclipse as shown later to start Tomcat.

Create a new Web project

From the Eclipse IDE, select File > New > Project to view the project wizards. Type Web in the Wizards box, and the New Project window filters the wizards to show the ones that match. This is an easy way to locate the wizard you want instead of going through each item.

Choose Dynamic Web Project from the list. You use the Standard Web Project Wizard to make static HTML Web projects. You can technically use Tomcat to run static Web sites, although a Web server such as Apache Web Server may be a better choice for static content. Web servers built specifically for serving up static pages tend to have less overhead and are tuned for that purpose. Target a runtime

Under Target Runtime, you see <None>, as shown in Figure 4, because you haven't created a runtime yet for Apache Tomcat. Click New to open the New Target Runtime Wizard. Select Apache Tomcat V5.5 from the Apache folder, as shown in Figure 5, then click Next.

4

毕业设计(外文翻译)

5

Figure 4. Create a new dynamic Web project

Figure 5. Create a new server runtime

Now you have the opportunity to name your runtime, although the default name Apache Tomcat V5.5 is fine. Click Browse to locate the base folder for your Apache Tomcat configuration (mine is C:\apps\apache-tomcat-5.5.20, as shown in Figure 6). I recommend leaving the IBM® Java Runtime Environment (JRE) version the same as the workbench default JRE, unless you have a specific

毕业设计(外文翻译)

6 reason to change it. Once you've entered a valid directory, click Finish to

create the runtime configuration.

Figure 6. Define the server location

You're asked if you want to switch to the J2EE Perspective. I always answer yes because that perspective includes views like the Servers view, which help to start and stop Tomcat later. You can always open the Servers view by selecting Window > Show View.

Add the server

In the Servers view is a view that contains nothing. This view is for application and Web servers; it lets you control them and monitor their status. To control Tomcat directly from the comfort of the IDE, add a reference to Tomcat by selecting New > Server from the context-sensitive menu. The New Server wizard, as shown in Figure 7, will default to the Apache Tomcat V5.5 runtime you just created.

毕业设计(外文翻译)

7

Figure 7. Define a new server

Click Next. You're asked whether you want to add projects to the server.

Select the project you created in the previous steps, then click Add.

Figure 8. Add the projects to the server

Click Finish to close the wizard. Now the Servers view contains the Tomcat V5.5 Server with the project under it.

毕业设计(外文翻译)

Figure 9. The Tomcat server in the Servers view

Create a sample page

The easiest way to test your new dynamic Web project and Tomcat server integration is to create a simple JSP and deploy it to Tomcat. You can create the new JSP file by selecting File > New > Other, or you can use the context-sensitive menu by right-clicking the project name in the Project Explorer view and selecting New > JSP.

Make sure the parent folder is WebContent and name the file index.jsp. Click Finish to create the page using the default template. If you click Next, you can choose different templates to use (such as HTML or XHTML) for the new page. The defaults are fine for this test.

The page code shown below displays a simple date so you can see it working.

Listing 1. Contents of index.jsp

8

毕业设计(外文翻译)

Deploy the page

Now that the test page is complete, use Eclipse to redeploy it to Apache Tomcat. In the Servers view, notice that State is set to Republish. That is because changes have been made since the project was last deployed to the server. Right-click the server and select Publish from the menu. The state changes to Synchronized once the project has been deployed to the server.

With the project successfully deployed and the server started, open a browser and navigate to http://localhost:8080/[ProjectName]/ (where [ProjectName] is the name of the dynamic Web project you created). The new Web page you created appears, and the date should be current. If you refresh the page, the date changes.

9

毕业设计(外文翻译)

10

Figure 10. Output of index.jsp in a browser

Hello World servlet

You can add Java servlets to your project and deploy them. To create a quick servlet you can use for testing purposes, use the Servlet Wizard by right-clicking your dynamic Web project in the Project Explorer view and selecting New > Other. Under the Web folder, select Servlet, then click Next.

Add the package name com.example.servlets next to Java Package and add HelloServlet next to Class name. Click Finish. This is an example, so the defaults are fine. The Eclipse IDE generates a servlet class for you. To watch the test servlet in action, add a line in the doGet() method to print a message to the ResponseWriter. The servlet class should look like the one shown below.

Listing 2. Contents of HelloServlet.java

毕业设计(外文翻译)

package com.example.servlets;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Servlet implementation class for Servlet: HelloServlet

*

*/

public class HelloServlet extends javax.servlet.http.HttpServlet

implements javax.servlet.Servlet {

/* (non-Java-doc)

* @see javax.servlet.http.HttpServlet#HttpServlet()

*/

public HelloServlet() {

super();

}

/* (non-Java-doc)

* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

11

毕业设计(外文翻译)

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.getWriter().write("Hello, world!");

}

/* (non-Java-doc)

* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// TODO Auto-generated method stub

}

}

After you add the servlet, the Eclipse IDE notes that your project has changed. Look again at the Servers view; Restart now appears in the Status column next to the Tomcat server. To restart the server, right-click the server in the Servers view, then select Restart > Start. Open a browser and navigate to http://localhost:8080/[ProjectName]/HelloServlet (where [ProjectName] is the name of your dynamic Web project). The text "Hello, world!" appears in the browser window. You can add the date, similar to the code in the index.jsp file, to the doGet() method, and redeploy the servlet using the Eclipse IDE.

12

毕业设计(外文翻译)

用 Tomcat 和 Eclipse

开发 Web 应用程序

所需的组件

Eclipse V3.2 Callisto 集成开发环境 (IDE) 包括了用于 Web 开发及与服务器集成的工具。所以,除了软件开发工具箱 (SDK) 之外,只需安装 Eclipse 和 Apache Tomcat。本文所使用的组件和版本如下所示:

表 1. 使用的组件和版本

组件本文使用的版本

Eclipse Platform 3.2.2

Java EE 5 SDK 1.5.0_09

Apache Tomcat 5.5.20

Eclipse IDE 是一种开源 IDE,用作Java™Server Pages (JSP) 和 Java 文件的开发环境。如若同时安装 Web 和 Java 2 Platform,Enterprise Edition (J2EE) 开发工具,那么 Eclipse 将非常适合于创建 HTML、JSP 和 servlet。

运行 servlet 需要 Java SDK。最后,在 Java Servlet 和 Java Server Page 的正式参考实现中还会用到 Apache Tomcat,它是一款开源 Web 和 servlet 容器。安装

Eclipse 和 Tomcat 的安装非常类似—都需要将归档文件解压缩到本地硬盘上的文件夹。如果想要卸载任何一个,只需将解压缩后的文件所在的文件夹删除即可。安装 Eclipse

要安装 Eclipse IDE,将从 Eclipse 网站下载的压缩文件 (.zip 或 .tar.gz) 解压缩,然后将压缩后的文件夹放入合适的位置。我会根据所使用的操作系统的约定来选择安装的位置。如果在 Macintosh 上安装 Eclipse,我会将 Eclipse 文件夹放在 Applications 文件夹内。如果使用的是Microsoft™ Windows™,我会将解压缩后的文件夹放入 C:\Program 文件目录。

13

毕业设计(外文翻译)

14 下载和安装了 Eclipse 后,就可以启动它了。在开始创建 Web 项目之前,您需要安装 J2EE 和 Web Development 插件。所幸的是,借助 Callisto Discovery 站点很容易安装这些插件。

安装 Web 工具

Eclipse IDE 运行正常后,选择 Help > Software Updates > Find and Install 。这个选项会让您可以下载和安装 Web 工具,且无需转到 Web 站点。

选择 Search for New Features to Install ,如下所示,然后单击 Next 。

图 1. 安装/更新窗口

选择 Callisto Discovery Site ,如下所示,然后单击 Next 。

毕业设计(外文翻译)

15

图 2. 选择 Callisto Discovery Site

在 Callisto Discovery Site 下,选中 Web and J2EE Development 复选框。向导会随即警告您,尚缺少依赖项。单击 Select Required ,警告消失。在 Graphical Editors and Frameworks and Models and Model Development 类别中的部分或全部组件会被选中(如图 3 所示),这些组件是安装 Web 和 J2EE 插件所必需的。

图 3. 选择要安装的特性

毕业设计(外文翻译)

单击 Next 以继续,针对所选的每个特性的证书协议也会继而出现。如果您同意这些证书,就选择接受,继续向导的后续过程。当 IDE 完成插件的安装后,就会要求您重启 Eclipse 以便更改能够生效。在继续之前,请务必重启 IDE。

重启了 IDE 后,开始开发动态 Web 项目所需的新特性就已准备完毕。

安装 Tomcat

要安装 Apache Tomcat,将所下载的归档文件解压缩并放入到目录中,我将其放在了我的 C:\apps 目录下以便于日后查找。到目前所做的这些就足够了,使用随后显示的 Eclipse 来启动 Tomcat。

创建一个新的 Web 项目

从 Eclipse IDE,选择 File > New > Project 查看项目向导。在 Wizards 框,输入 Web,而 New Project 窗口会过滤向导以只显示匹配的那些项。利用这种方法可以很容易找到向导,而无需遍历每一项。

从列表中选择 Dynamic Web Project。我们要使用 Standard Web Project Wizard 来建立静态 HTML Web 项目。虽然您也可以在技术上使用 Tomcat 运行静态 Web 站点,但 Web 服务器,比如 Apache Web Server 更适合于静态内容。专门为服务于静态页面而设计的 Web 服务器拥有的开销更少,且专为此目的作了调优。

以运行库为目标

在 Target Runtime 下,可以看到 <None>,如图 4 所示,由于您尚未为 Apache Tomcat 创建运行库,所以需要单击 New 打开 New Target Runtime Wizard。从Apache 文件夹中选择 Apache Tomcat V5.5,如图 5 所示,然后单击 Next。

16

用 Tomcat 和 Eclipse开发 Web 应用程序英文原文及中文翻译.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
    ×
    二维码
    × 游客快捷下载通道(下载后可以自由复制和排版)
    VIP包月下载
    特价:29 元/月 原价:99元
    低至 0.3 元/份 每月下载150
    全站内容免费自由复制
    VIP包月下载
    特价:29 元/月 原价:99元
    低至 0.3 元/份 每月下载150
    全站内容免费自由复制
    注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
    × 常见问题(客服时间:周一到周五 9:30-18:00)