Configure Web application environment to implement JSP guest book

Source: Internet
Author: User
Tags implement mysql in mysql query variables query stmt zip mysql database
Js|web


Java Server page (JSP), and ASP, PHP is the same as the network programming language, but the JSP page inserted in the script code is a Java statement fragment. To use JSP to write an application, first of all, must have a Web server that can execute JSP script, can build on the original Apache, IIS or PWS server, but there are many technical problems. It is recommended that just contact JSP bugs, or from scratch, directly install a dedicated Web server to support JSP, lest complications, here to introduce you are Tomcat 3.1.

Familiar with the network programming people know that in the network programming without the support of the database, a very simple thing to do is quite hard. So what database is better to use in the Java environment? At present, the popular network databases are mainly Oracle, Sybase, SQL Server, MySQL, etc., but the most suitable for personal web sites or small network use of the most important MySQL, its completely free, easy to install, easy to manage, easy to obtain, fully support the characteristics of SQL language, For the current vast number of network programming enthusiasts like. This article describes how to install the Java SDK, Tomcat, MySQL, JDBC for MySQL in a Windows environment.

   Software Preparation
First download to the following programs:

1, Java Compiler support environment: J2sdk1_3_-win.exe (30MB)

2. JDBC for MySQL driver: mm.mysql.jdbc-1.2b.zip (386KB)

3, JSP-enabled Web server Tomcat3.1:jakarta-to,cat.zip (2.23MB)

4, MySQL data Inventory server: Mysql-3.23.21-beta-win-src.zip (2.23MB)

The above program in addition to MySQL data Inventory server, in the http://java.sun.com/have the latest version of the program, in the major download centers are easy to download.

   Installing a JSP development environment with JDBC for MySQL
   Install the Java Development Kit
When the user accesses the JSP page of the Tomcat server, Tomcat first invokes the Java Development Kit, compiles and executes the Java code in the JSP page, and returns the results to the customer in HTML format. Therefore, before you install Tomcat, you must first install the Java Development Kit. Install the J2sdk1_3_0-win.exe directly double-click the download, you can install the Java development environment, the only thing that needs to be done is to select the installation directory, where the assumption is installed in the C:\JDK1.3 directory.

In addition, if you choose Windows in Chinese, you must modify the system registry, because during the installation process, the system registers the branch of the Java Runtime Environment in Chinese, which indicates the directory of the files required by the Java runtime, and the Java SDK does not recognize the Chinese data in the system registry.

The steps are: Open the registry with Regedit and find the JavaSoft entry, located in: Hkey_local_machine→software→javasoft, locate the "Java Runtime Environment" and export this branch to file 1.reg. Then open the 1.reg with a text editor, replace all of the "Java Runtime Environment" with "Java Runtime environment" and save, and double-click the file to import the registry.

Then set the environment variable. In Win 9x, to edit the Autoexec.bat file, set the environment variable with the SET statement. In Win NT or Win 2000 you can choose "My Computer", right-click the menu, select "Properties", pop-up "System Features" dialog box, select Advanced, and then click the button "Environment variables", you can edit the system's environment variables.

Add the following statement:

REM Setting path

Path=%path%;c:\jdk1.3;c:\jdk1.3\bin

REM Setting Java environment variables

Set Classpath=c:\jdk1.3\lib\tools.jar; C:\jdk1.3\lib\dt.jar;

REM Set Java home directory

Set java_home=c:\jdk1.3

   Install Tomcat 3.1
The installation of Tomcat 3.1 is simpler, the Jakarta-tomcat.zip decompression directly released to the C:\Tomcat directory, and then set the environment variable, add the following statement:

REM Setting path

Path=%path%;c:\tomcat

REM Set TOMCAT environment variable

Set Classpath=c:\tomcat\classes;c:\tomcat\lib;%classpath

REM Set Tomcat's home directory

Set Tomcat_home=c:\tomcat

After you restart your computer, you have a Web server that supports JSP. A window in which two command-line methods appear after running the C:\Tomcat\Bin directory Startup.bat. When you type http://localhost:8080/in the browser, you should see the image of Tomcat 3.1. By the way, don't turn off the two windows, which means Java and Tomcat are running in the background. To shut down the server executable C:\Tomcat\Bin directory shutdown.bat.

   install MySQL database management system
Unzip the downloaded mysql-3.23.21-beta-win-src.zip to a temporary directory, run setup in the Temp directory, and choose the installation directory and installation method according to the Installation wizard, the system will complete the installation. This is assumed to be installed in the C:\MySQL directory. Run the Mysqld-shareware.exe in the C:\MySQL\Bin\ directory to start MySQL. After you start MySQL, the Windows desktop does not change, and you can check that the database service was successfully installed by executing MySQL manager. If the installation is successful, you can directly open the inside of the library test and MySQL.

   installing JDBC for MySQL
In order for Java to operate MySQL's database, you need to install the MySQL JDBC driver, unzip the mm.mysql.jdbc-1.2b.zip to C:\, automatically generate a mm.mysql.jdbc-1.2b directory, and set the environment variables:

REM Setting MYSQL.JDBC environment variable

Set Classpath=c:\mm.mysql.jdbc-1.2b;%classpath

After reboot, all the installation is OK!

   Example: Building a guest book using the Environment
   Create a table for storing data
To design a guest book, you must first create a table in the MySQL database that holds the message data, assuming that the table is Questbook and has the following structure:

RecordID Store record number

Name of the person holding the message

Email address of message store

Message of Body Store message

Specific steps to operate:

1. Execute program mysqld-shareware.exe, start MySQL.

2. Execute program mysqlmanager, open Library test.

3. Select SQL Query under the Tools menu and the MySQL query window appears.

4. Under the Query tab, the input commands are as follows: Create TABLE Questbook (RecordID int,name char (), Email char (), body text).

5. Click the Run icon. You can get a table Questbook for storing guest book data.

   Write a guest book program
Java is the use of JDBC for MySQL in the toolkit provided by the API function, to invoke the MySQL database, you can open the C:\mm.mysql.jdbc-1.2b\doc\apidoc\ browser index.html file to obtain all API function descriptions. Let me just introduce you to a few of the functions you need to write a guest book.

Class.forName ("Org.gjt.mm.mysql.Driver"); For loading the Mm.mysql driver

con = drivermanager.getconnection ("jdbc:mysql://localhost:3306/test?user=root;password="); Used to connect to local database MySQL

stmt = Con.createstatement (); Building Database Objects

rs = Stmt.executequery ("SELECT * from Guestbook"); Execute SQL statement and return result set

After you have compiled the program, put it in the Tomat release directory C:\Tomcat\webapps\ROOT.




Related Article

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.