Configure Web application environment to implement JSP guestbook
Author:Eve Cole
Update Time:2009-07-02 17:11:54
Java Server Page (JSP for short) is a network programming language like ASP and PHP, but the script code inserted in the JSP page is a Java statement fragment. To use JSP to write applications, first of all, you must have a Web server that can execute JSP scripts. It can be built on the original Apache, IIS or PWS server, but there are many technical problems. It is recommended that those who are new to JSP start from scratch and directly install a web server that specifically supports JSP to avoid unnecessary complications. What I will introduce to you here is Tomcat 3.1.
Anyone who is familiar with network programming knows that without the support of a database in network programming, it will be quite hard to do a very simple thing. So what database is better to use in a Java environment? At present, the popular network databases mainly include Oracle, Sybase, SQL Server, MySQL, etc. However, MySQL is the most suitable for personal websites or small networks. It is completely free, easy to install, easy to manage, easy to obtain, and fully supports SQL language, etc. Features, currently favored by the vast majority of network programming enthusiasts. This article introduces how to install Java SDK, Tomcat, MySQL, and JDBC for MySQL in a Windows environment.
Software preparation <BR> First, download the following programs:
1. Java compilation support environment: j2sdk1_3_-win.exe (30MB)
2. JDBC for MySQL driver: mm.mysql.jdbc-1.2b.zip (386KB)
3. Web server Tomcat3.1 that supports JSP:jakarta-to,cat.zip(2.23MB)
4. MySQL data inventory server: mysql-3.23.21-beta-win-src.zip (2.23MB)
In addition to the MySQL data inventory server, the above programs have the latest versions at http://java.sun.com/, and they are also easy to download from major download centers.
Install the JSP development environment with JDBC for MySQL <BR> Install the Java Development Kit <BR> When the user accesses the JSP page of the Tomcat server, Tomcat first calls the Java Development Kit, compiles and executes the Java code in the JSP page, and converts the results Return to the client in HTML format. Therefore, before installing Tomcat, the Java Development Kit must be installed first. During installation, directly double-click the downloaded j2sdk1_3_0-win.exe to install the Java development environment. The only operation required is to select the installation directory. It is assumed that the installation is in the C:JDK1.3 directory.
In addition, if you choose Chinese Windows, you must modify the system registry, because during the installation process, the system registers the "Java Runtime Environment" branch in Chinese. This branch is used to indicate the directory of files required for Java runtime, and Java The SDK cannot recognize Chinese data in the system registry.
The specific steps are: use RegEdit to open the registry and find the Javasoft item. The location is: hkey_local_machine→software→javasoft. Find "Java Runtime Environment" and export this branch to the file 1.reg. Then open 1.reg with a text editor, replace all "Java Runtime Environment" with "Java Runtime Environment" and save it. Double-click the file to import it into the registry.
Next is setting environment variables. In Win 9x, edit the Autoexec.bat file and use the Set statement to set environment variables. In Win NT or Win 2000, you can select "My Computer", right-click the menu, select "Properties", the "System Properties" dialog box will pop up, select "Advanced", and then click the button "Environment Variables" to edit the system environment variables.
Add the following statement to it:
rem set path
PATH=%PATH%;c:jdk1.3;c:jdk1.3bin
rem sets java environment variables
set CLASSPATH=C:jdk1.3libTools.jar;C:jdk1.3libdt.jar;
rem sets the java home directory
set JAVA_HOME=c:jdk1.3
Install Tomcat 3.1
Installing Tomcat 3.1 is relatively simple. Just decompress jakarta-tomcat.zip and release it to the C:Tomcat directory. Then set the environment variables and add the following statement:
rem set path
PATH=%PATH%;c:tomcat
rem sets tomcat environment variables
set CLASSPATH=c:tomcatclasses;c:tomcatlib;%CLASSPATH
rem sets the home directory of TOMcat
set TOMCAT_HOME=c:tomcat
After restarting your computer, you will have a Web server that supports JSP. After running Startup.bat in the C:TomcatBin directory, two command line windows appear. At this time, type http://localhost:8080/ in the browser, and you should be able to see the Tomcat 3.1 screen. By the way, never close these two windows, it means that Java and Tomcat are running in the background. To shut down the server, execute Shutdown.bat in the C:TomcatBin directory.
Install MySQL database management system <BR> Extract the downloaded mysql-3.23.21-beta-win-src.zip to a temporary directory, run Setup in the temporary directory, and select the installation directory and installation method according to the installation wizard. The installation will be completed. It is assumed here that it is installed in the C:MySQL directory. Run mysqld-shareware.exe in the C:MySQLBin directory to start MySQL. After starting MySQL, there is no change on the Windows desktop. You can check whether the database service is installed successfully by executing MySQL Manager. If the installation is successful, you can directly open the libraries Test and MySQL inside.
Install JDBC for MySQL
In order to enable Java to operate the MySQL database, you need to install the MySQL JDBC driver, unzip mm.mysql.jdbc-1.2b.zip to C:, automatically generate a mm.mysql.jdbc-1.2b directory, and Set environment variables:
rem sets mysql.jdbc environment variables
set CLASSPATH=c:mm.mysql.jdbc-1.2b;%CLASSPATH
After restarting, all installations are OK!
Example: Use the environment to create a guestbook <BR> Create a table to store data <BR> To design a guestbook, you must first create a table in the MySQL database to store message data. Assume that the table is Questbook, and its structure is as follows:
Recordid stores the record number
name stores the name of the person who left the message
Email stores the email address of the person who left the message
body stores the message of the commenter
Specific steps:
1. Execute the program mysqld-shareware.exe to start MySQL.
2. Execute the program MySQLManager and open the library Test.
3. Select SQL Query under the Tools menu, and the MySQL Query window appears.
4. Under the Query tab, enter the command as follows: create table questbook(Recordid int,Name char(20),Email char(30), Body text).
5. Click the run icon. You can get the table Questbook used to store guestbook data.
Writing a guestbook program in Java uses the API function in the toolkit provided by JDBC for MySQL to call the MySQL database. You can use a browser to open C:mm.mysql.jdbc-1.2bdocapidocindex.html file to get descriptions of all API functions. Below I will only introduce you to a few functions required to write a guestbook.
Class.forName("org.gjt.mm.mysql.Driver"); used to load mm.mysql driver
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root;password="); used to connect to the local database MySQL
stmt = con.createStatement(); Generate database object
rs = stmt.executeQuery("SELECT * FROM guestbook");Execute the SQL statement and return the result set
After editing the program, just put it in Tomat's release directory C:TomcatwebappsROOT.