MIT OpenCourseWare


» 进阶搜寻
 课程首页
 教学大纲
 教学时程
 课堂讲稿
 实作课程
 测验
 专题
 相关资源
 讨论群组

专题


本页翻译进度

灯号说明

审定:无
翻译:庄鸿国(简介并寄信)
编辑:陈盈(简介并寄信)


本部分的.class文档执行时,需要Java® Virtual Machine 机器软件(自动安装在大部份的网络浏览器),很多开发工具也能够编辑且执行课程网站上的.java文档。

期末专题范例

网络论坛 (ZIP) (本ZIP档包括:2个.txt文档,9个.jsp文档,57个.java文档和65个.class文档。)

期末专题指导

对于期末专题,你需要熟悉一些安装在计算机里的工具。在这方面,下面的文档是我们在上节课开始第一个网络应用的步骤摘要。

1. MySQL®

MySQL® 是一个数据库服务器,我们要用它来储存和找回信息。用来运行MySQL®的文档在C:\mysql的文档夹中。你要知道的两个重要的路径是C:\mysql\binC:\mysql\docs,在前者里可以找到可执行的文档来启动服务器并与之相互作用,在后者里有关于我们安装的MySQL®版本的各种文档。

我将给你一些基本的知道,让你能够开始使用MySQL®。关于这些的详细内容可以在你的目录文档中找到。

1.1 开始使用MySQL®服务器

为了在应用中使用MySQL®,你需要运行指令启动装在计算机中的MySQL®服务器。以下是执行步骤:

  • 启动->运行,键入cmd
  • 切换到C:\mysql\bin文档夹
  • 输入mysqld-nt启动 MySQL® 服务器。如果成功,输入这个指令将不会有任何输出。

一旦服务器启动成功,你就可以登入服务器,并用命令列模式执行查询。用来登入服务器的指令是mysql-u root。如果成功,该指令会带出一个欢迎讯息,并显示MySQL®命令列,就可以输入查询指令。

接下来是对一些基本的SQL描述作一个简要的概览,这在上节课已经讲过。为了对该内容有更广泛的复习,你可以通读C:\mysql\docs文档夹中manual.html的第三章。

1.2 查看和建立数据库

你可以通过输入以下指令来查看已经在MySQL®中创建的数据库列表:
show databases;

建立一个数据库也很简单。这样做的指令是:
create database 数据库名称;

你还可以用以下指令删除数据库:
drop database 数据库名称;

当你使用该指令时要小心,因为一旦你删除数据库,就永远不能恢复。

1.3 用数据库工作

在创建了数据库后,你可以用关键词create table来增加表格。

其他你要知道的重要SQL关键词是:insert, select, updatealter。这些都在MySQL®手册中有详细讲述,手册已经安装在文档夹C:\mysql\docs中。

2. Tomcat™

Tomcat™是一个商业网页服务器,它是用来显示JSP™网页的输出。这个网页服务器在很多计算机中都有单独的安装路径。最好的路径是C:\tomcat

你要用到的重要文档夹是webappsconf(包含server.xml文档)。我们在你的桌面建立了一个tomcat文档夹,所有你要用来写网页应用软件的文档都有快捷方式。除了webapps的快捷方式和server.html之外,该文档夹还有启动关闭Tomcat™服务器的两个可执行文档的快捷方式。

2.1 在Tomcat™中建立新的网络应用软件

2.1.1 放文档的地方

为了用Tomcat™来运行你的网页应用软件,你必须把所有的文档都放进Tomcat™安装的webapps文档夹中,我们建立了一个新的名为lecture的文档夹来放置上节课做的课堂应用软件。

在建立了应用软件的文档夹后(即lecture文档夹),你需要按照我在课堂上所大致讲述的方法来把所有其它文档放好。一般来说,你要把表述层的所有文档直接放到lecture文档夹中(包括所有html、JSP™和图像。)然后你要建立一个叫WEB-INF的新文档夹,并且在WEB-INF文档夹中建立一个叫classes的新文档夹,里面用来放置所有运行软件所需的文档。

2.1.2 编辑server.xml文档(每个新的网页模块都要进行一次该步骤)

在建立了网页应用软件的所有文档后,或者当你准备好观看工作成果的输出时,你要在启动服务器之前告知Tomcat™这个新网页模组的存在。通过在server.xml文档中添加应用软件的上下文路径可以完成这一步骤。

通常上下文路径会被添加到server.xml文档的最底部。如果你要找一个适合的地方来添加新的上下文路径,那么要在文档中找到“context path”字串(在JCreator 中按ctrl-f)。这会把你带到文档中有上下文路径的部分。对于lecture应用软件,我们要添加以下这些内容:

在输入这些内容后,保存并关闭server.xml文档。你可以准备好用Tomcat™来看JSP™网页的输出了。

2.1.3 启动 Tomcat™

你可以准备好观看工作成果的输出了。要启动Tomcat™,请在桌面上打开tomcat文档夹并点击启动服务器的捷径图示来启动。这会打开一个命令列指令窗口,其中有Tomcat™启动时的状态资讯。不要关闭该窗口,关闭该窗口等于不按正确程序关闭Tomcat™。正确地关闭Tomcat™的方法是,点击桌面tomcat文档夹中关闭服务器的捷径的图示来关闭。

在Tomcat™运行时,打开IE浏览器并点击收藏夹中的localhost快捷方式。如果成功,这会显示Apache-tomcat网页服务器的默认主页。

要看到网页模块的呈现结果,就要在现有浏览器的URL末尾添加网页模块的名称。例如,如果正在运行的主页是http://localhost,那么就去http://localhost/lecture查看lecture应用软件的输出。这会显示与lecture网页模块相联的文档列表。

请注意,你可能要一次又一次地反复重启Tomcat™来看你对网页模块文档所作的改变。正确的方法是用桌面tomcat文档夹中的快捷方式。不要直接关闭Tomcat™窗口。在重启服务器来察看变化后一定要刷新IE浏览器。

3. 把MySQL®的界面与JSP™网页相连接

让JSP™适应MySQL®网页也许是一件很艰难的事,但第一步成功之后就简单得多了。因为MySQL®(和Samuel已经提供了一个API来帮助它和Java®进行界面连接,你要做的只是把已经在WEB-INF/classes文档夹中建立好的类别添加上去就好了。

接着我要你从文档夹D:\aiti\jsp中复制以下的文档夹和文档到你的网页应用软件文档夹WEB-INF/classes里面:

  • MySQL®提供的org 和 com文档夹
  • DbConnect.java文档,我用它来调用org和com文档夹中的类别还有javax.sql类别。你一定要在运行网页应用软件前编译该文档。

我在D:\aiti\jsp文档夹中放了 UsingDatabases.java文档,它能让你知道如何连接到MySQL®数据库并在其中进行查询。

你一定要让Tomcat™执行任何使用数据库的JSP™脚本前运行MySQL®服务器。


Java® Virtual Machine software (automatically installed in most major web browsers) is required to run the .class files in this section. Any number of development tools can be used to compile and run the .java files in this section.

Sample Final Project

Web Forum (ZIP) (The ZIP file contains: 2 .txt files, 9 .jsp files, 57 .java files and 65 .class files.)

Final Project Guidelines

For the final project, you will need to be familiar with several tools that have been installed in your machine. Towards this end, the following document is a summary of the steps that we went through to create our first web application during the previous lecture.

1. MySQL®

MySQL® is the database server that we will be using to store and retrieve information. The files used to run the MySQL® server are in the folder C:\mysql. The two important locations that you should be aware of are C:\mysql\bin, which has executable files to start and interact with the server, and C:\mysql\docs, which has extensive documentation on the MySQL® version that we have installed.

I will now give you some basic heads up to get you started with using MySQL®. Details on this can be found in the documentation in your directory.

1.1 Starting the MySQL® Server

In order to use MySQL® in your applications, you need to start the MySQL® server installed in your machines from the command prompt. Here are the steps you make:

  • Go to START->command prompt.
  • Navigate to the folder C:\mysql\bin from the command prompt.
  • Start the MySQL® server by typing in mysqld-nt. If successful, typing in this command should return no output.

Once the server has started successfully, you can log in to the server and execute queries from the command prompt. The command used to log in to the server is mysql-u root. If successful, this command will display a welcome message and present the MySQL® prompt, to which you will be entering your queries.

What follows is a brief overview of some of the basic SQL statements that were covered in the previous lecture. For a more extensive review of that material, you should look through Chapter 3 in the manual.html page in your C:\mysql\docs folder.

1.2 Viewing and Creating Databases

You can see the list of databases already created in MySQL® by typing in the command:
show databases;

Creating a database is also simple. The command for doing this is:
create database databaseName;

You can also delete a database by using the command:
drop database databaseName;

Be careful when you use this command because once you delete a database, there is no going back.

1.3 Working on a Database

Once you have created a database, you can add tables to it by using the create table keyword.

Other important SQL keywords that you should know about are: insert, select, update and alter. These are described in great detail in the MySQL® manual that is already installed in the C:\mysql\docs folder.

2. Tomcat™

Tomcat™ is a commercial web server that helps display the output of your JSP™ pages. This web server has been installed in separate locations in many of the machines. A good place to look would be C:\tomcat.

The important folders that you will be working with are the webapps folder and the conf folder (which contains the server.xml file). We have created a tomcat folder in your desktop with shortcuts to all the files that you will be using to write your web application. Besides shortcuts to webapps and server.xml, this folder also contains shortcuts to the two executables that startup and shutdown the Tomcat™ server.

2.1 Creating a New Web Application in Tomcat™

2.1.1 Where to Put your Files

In order to use Tomcat™ to run your web applications, you must put all your files in the webapps folder of the Tomcat™ installation. For example, for the lecture application that we created in the previous lecture, we created a new folder named lecture and put that folder in webapps.

After creating the application's folder (lecture in this case), you should arrange all other files in the way that I outlined during lecture. In general, you should put all the files in the presentation layer in the lecture folder directly (that includes all html, JSP™ and images). You should then create a new folder named WEB-INF, and in the WEB-INF folder, create a new folder called classes that will hold all the Java® files necessary to run your application.

2.1.2 Editing the server.xml File (Do it Once for Every New Web Module)

After creating all the files for the web application, or whenever you are ready to see the output of your work, you should inform Tomcat™ about the existence of a new web module before starting the server. This is done by adding the context path to the application in the server.xml file.

Usually, the context path is added near the very bottom of the server.xml file. If you want to find the right place to add a new context path, find the string "context path" in the file (ctrl-f in JCreator). This will direct you to the section of the file that contains context paths. For the lecture application, these are the lines that we added:

After adding these lines, save and close the server.xml file. You are now ready to use Tomcat™ to view the output of your JSP™ pages.

2.1.3 Starting Tomcat™

You are now ready to see the output of your work. To start Tomcat™, just go to the tomcat folder on your desktop and click on the shortcut to startup icon. This will open a command prompt window with information on the status of Tomcat™ as it starts. DO NOT CLOSE THIS WINDOW. Closing this window amounts to shutting down Tomcat™ without following the right procedure. The right way to shut down Tomcat™ is by clicking on the shortcut to shutdown icon on the tomcat folder in your desktop.

Once Tomcat™ is up and running, open Internet Explorer and click on the localhost shortcut in the favorites tab. If successful, this should display the default homepage of the Apache-tomcat web server.

To see the output of a web module, add the name of the web module at the end of the current URL on the browser. For example, if the homepage is running at http://localhost, go to http://localhost/lecture to see the output of the lecture application. This should display a listing of the files that are associated with the lecture web module.

Please note that you may need to restart the Tomcat™ server from time to time to see changes that you make to files in your web module. The right way to do this is by using the shortcuts in the tomcat folder on your desktop. Do not close the Tomcat™ window directly. Be sure to refresh Internet Explorer after restarting the server to see any changes.

3. Interfacing MySQL® with your JSP™ Pages

Getting JSP™ to work with your MySQL® pages may seem like a daunting task, but it is much simpler that it appears at first. This is because MySQL® (and Samuel), have already provided an API to help interface it with Java®, and all you have to do is add the classes that have already been created to your own WEB-INF/classes folder.

I will therefore ask you to copy the following folders and files from the D:\aiti\jsp folder into your own web application's WEB-INF/classes folder:

  • The folders org and com, which are provided for you by MySQL®.
  • The file DbConnect.java, which I wrote to make calls to classes in the org and com folder and also the javax.sql classes. Make sure you compile this file before running your web application.

I have included a file UsingDatabases.java in the D:\aiti\jsp folder, which has a method that shows you how to connect to a MySQL® database and run some queries on it.

Make sure you have the MySQL® server running before you ask Tomcat™ to execute any JSP™ script that uses the database.


 
MIT Home
Massachusetts Institute of Technology Terms of Use Privacy