JSP + Tomcat + MySQL & sevlet & JavaBean configuration process

Source: Internet
Author: User

From: http://www.zzchina.net/article/26/25899.htm

Many problems may occur during the configuration of JSP development environment. Thanks to the experiences of many predecessors on the Internet, the author cl41 JSP connection MySQL database strategy and saulzy MySQL study notes.ArticleFor me, it is just a carbon in the snow. To help beginners like me, I have summed up many experiences of the authors and thank them for their selfless dedication, I also hope that more people will carry forward this spirit!
If the following software download link cannot be displayed, please download it from the official website!
 
Software Download
MySQL
Download: mysql-4.1.13-win32.zip (Windows downloads)
Http://dev.mysql.com/downloads/mysql/4.1.html

JDBC driver
Download version: mysql-connector-java-3.1.10.zip
Http://dev.mysql.com/downloads/connector/j/3.1.html

Download j2sdk
Download version: jdk-1_5_0_04-windows-i586-p.exe
Http://java.sun.com/j2se/1.5.0/download.jsp

Download Tomcat
Download version: jakarta-tomcat-5.5.9.exe
Http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi

Install and configure j2sdk:

Install j2sdkProgramYou can also customize the path, but you need to modify the following configuration.

Configure j2sdk:

Configure environment variables:

Choose my computer> Properties> advanced> environment variables> system variables to add the following environment variables:
Java_home = c: \ Program Files \ Java \ jdk1.5.0 _ 04
Classpath = % java_home % \ Lib \ DT. jar; % java_home % \ Lib \ tools. jar;
Path = % java_home % \ bin; % SystemRoot % \ system32; % SystemRoot % \ system32 \ WBEM

Write a simple Java program to test whether j2sdk has been installed successfully:
Public class hello
{
Public static void main (string ARGs [])
{
System. Out. println ("hello ");
}
}

Save the program as a file named hello. java.

Open the Command Prompt window, go to the directory where hello. Java is located, and type the following command
Javac hello. Java
Java hello
If "hello" is printed, the installation is successful. If "hello" is not printed, check whether the preceding configuration is correct.
Note that "Hide extensions of known file types" is not checked in the system folder option (my computer-tools-view)

Install and configure Tomcat
Run the tomcat installation program and install it according to the default settings. You can also customize the path, but you need to modify the following configuration.

My computer> Properties> advanced> environment variables> system variables Add the following environment variables
Catalina_home = c: \ Program Files \ Apache Software Foundation \ Tomcat 5.5
Catalina_base = c: \ Program Files \ Apache Software Foundation \ Tomcat 5.5

Modify the classpath in the environment variable and add the servlet-api.jar under common \ Lib under the Tomat installation directory to the classpath,
The modified classpath is as follows:
Classpath = % java_home % \ Lib \ DT. jar; % java_home % \ Lib \ tools. jar; C: \ Program Files \ Apache Software Foundation \ Tomcat 5.5 \ common \ Lib \ servlet-api.jar;

Start Tomcat and access http: // localhost: 8080 in IE. If you see the welcome page of Tomcat, the installation is successful.

Install MySQL
Decompress mysql-4.1.13-win32.zipto run setup.exe
The Installation Wizard welcome page appears first. Click "Next"
Continue, select the installation type, select "Custom" m to install, and click "Next" to next. The custom installation interface is displayed.
Select the installation path: C: \ mysql server 4.1 (customizable)
Click "OK" to return to the custom installation interface. The path has been changed to the set path.
Click "Next" to start installation.
Click "Install" to start installation.
The interface for creating a mysql.com account is displayed.
If you are using MySQL for the first time, select "create anew free mysql.com accout"
Click "Next" and enter your email address and the password you set for logging on to mysql.com.
Click "Next" to enter the second step.
Enter the name and other relevant information, and click "Next" to enter step 3.
After entering the phone number, company name, and other information, click "Next ",
Click "Next" to display the UI for previewing the information you just filled in.
Note: Here is a Configuration Wizard option (configure the MySQL server now ),
We recommend that you check to configure your MySQL instance now. Many say that MySQL cannot be started after installation, because MySQL is not configured.
Click "finish" to complete the installation and start configuring MySQL.
Click "Next" to go to the configuration type selection page. Select "detailed configuration" (detailed configuration)
Click "Next" to go to the service type selection page. Select "Developer Machine" to consume less system resources.
Click "Next" to go to the database usage selection page. Select "multifunctional database"
Click "Next" to go To the InnoDB data storage location selection page.
You do not need to change the settings, just put them in the installation directory of installation path, and click "Next" to select the number of simultaneous connections of MySQL.
Select manual setting and set it to 100 (as needed)
Click "Next" to configure the port of MySQL In the TCP/IP communication environment and select the default port 3306.
Click "Next" and select character settings in MySQL.
Note that the selection will affect whether you can use Chinese in MySQL. Select the gb2312 character set to support simplified Chinese
Click "Next" to set Windows service options
Note that the selection here is critical.
You must check "Install as Windows Service" to run MYSQL as a Windows service.
"Service name" uses the default "MySQL"
The following "launch the MySQL server automatically" must be checked, so that MySQL will automatically start the service during Windows Startup, or you will have to manually start MySQL.
Many people say that MySQL cannot be started, cannot be connected, and a 10061 error occurs after it is installed. This is the reason.
Click "Next" to set the root account's logon password.
"Modify Security Settings" is used to set the password of the root account. Enter the password you set.
"Create an anonymous account" is used to create an anonymous account. This will cause unauthorized users to access your database illegally. It is recommended that you do not select this option because of security risks.
Click "Next". The MySQL Configuration Wizard configures MYSQL based on all the settings above, so that MySQL runs as needed.
Click "execute" to start configuration. When "service started successfully" is displayed, your configuration is complete and MySQL service is started successfully.
Click "finish" to complete the configuration of MySQL. The rest is to connect to the MySQL server using the mysql client and then use it.

Install the JDBC driver:
Uncompress mysql-connector-java-3.1.10.zip
Mysql-connector-java-3.1.10-bin-g.jar and mysql-connector-java-3.1.10-bin.jar will be used

Configuration
Create the mysqlforjdbc subdirectory under the c: \ Program Files \ Java directory, enter the directory to mysql-connector-java-3.1.10-bin.jar to this directory
Go to the C: \ Program Files \ Java \ jdk1.5.0 _ 04 \ lib directory to copy the mysql-connector-java-3.1.10-bin-g.jar to this directory
Then configure classpath, append % java_home % \ Lib \ mysql-connector-java-3.1.10-bin-g.jar; C: \ Program Files \ Java \ mysqlforjdbc \ mysql-connector-java-3.1.10-bin.jar; to this environment variable
The environment variables after append are as follows:
Classpath = % java_home % \ Lib \ DT. jar; % java_home % \ Lib \ tools. jar; C: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ common \ Lib \ servlet-api.jar; % java_home % \ Lib \ mysql-connector-java-3.1.10-bin-g.jar; C: \ Program Files \ Java \ mysqlforjdbc \ mysql-connector-java-3.1.10-bin.jar;
The purpose of this configuration is to allow the Java application to find the driver connecting to MySQL.

View and start the MySQL Service
After MySQL is installed in Windows XP, it automatically starts the service and has its client shortcut connection in the Start Menu.
You can view it in the Service Manager of windows. "Start"-"run", enter "services. msc", and press Enter.
The Service Manager of Windows is displayed, and the service item "MySQL" is displayed, and "started" is displayed on the right"
In the Start Menu-all programs-mysql-MySQL Server 4.1-MySQL command line client, use the client shortcut to connect
Enter the password set for installation.

Database usage

After MySQL is installed, choose Start> All Programs> mysql> MySQL Server 4.1> MySQL command line client to use the client shortcut
Enter the password set during installation

Use Basic MySQL commands (a semicolon must be added at the end of each command input in the MySQL command line)
Display Database: Show databases;
Database used: Use Database Name;

Database creation
Create a database first in MySQL and a table about in the database
Command: Create Database first;

Set database permissions (user and password)
Command: grant all privileges on first. * To test @ localhost identified by "123456 ";
After executing this command, you only need to log on to the database first with the username: Test and password: 123456. This avoids the use of Root
Enter the command: use first;
Use the first database;

Create a table in the first database
Command: Create Table about (ID int (8) primary key, name varchar (10 ));

In the table, assume that the data is:
Command: insert into about values ('xyw1026', 'laojiang ');

Exit
Command: Exit

JSP connection to MySQL
Create a sub-directory MyApp in the C: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ webapps directory
Go to the C: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ webapps \ MyApp directory
Write a file in notepad and save it as first. jsp
Code As follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<HTML>
<Body>
<% Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
String url = "JDBC: mysql: // localhost/softforum? User = soft & Password = soft1234 & useunicode = true & characterencoding = 8859_1"
// First is your database name
Connection conn = drivermanager. getconnection (URL );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from first ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
The content of your first field is: <% = Rs. getstring (1) %>
Your second field content is: <% = Rs. getstring (2) %>
<% }%>
<% Out. Print ("database operation successful, congratulations"); %>
<% Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>

Enter:
Http: // 127.0.0.1: 8080/MyApp/first. jsp
If:
Id | num
0 | Laojiang

Database Operation successful, congratulations

Indicates that the configuration is successful.

Go to the C: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ webapps \ MyApp directory
Create a directory WEB-INF under the MyApp directory, note that the directory name is case sensitive;
Create a file in Notepad under the WEB-INF named Web. XML with the following content:

<? XML version = "1.0" encoding = "ISO-8859-1"?>

<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>

<Web-app>
<Display-Name> my web application </display-Name>
<Description>
A Application for test.
</Description>
</Web-app>

Create a test JSP page with notepad under MyApp. The file name is index. jsp. The file content is as follows:

<HTML> <body> <center>
Now time is: <% = new java. util. Date () %>
</Center> </body>

Restart Tomcat
Open the browser and enter http: // localhost: 8080/MyApp/index. jsp.
If the current time is displayed, the installation is successful.

Create your own servlet:
Use NotePad to create a servlet program named helloworld. java. The file content is as follows:

Import java. Io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class helloworld extends httpservlet
{
Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception
{

Response. setcontenttype ("text/html ");
Printwriter out = response. getwriter ();
Out. println ("<HTML> Out. println ("this is my first servlet ");
Out. println ("</title> Out. println ("Out. println ("</body>

}
}

Compile
Cut helloworld. Java to the C: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ common \ Classes directory
Enter the following command line:
Cd c: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ common \ Classes
Javac helloworld. Java
Use javac helloworld. Java to compile this file. If the file cannot be imported javax. servlet .*
Copy the servlet-api.jar file under the c: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ common \ lib directory and add it
C: \ Program Files \ Java \ jre1.5.0 _ 04 \ Lib \ ext directory.

Then a compiled Servlet File: helloworld. class will be generated under c: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ common \ Classes.
Open the Web. XML in the c: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ webapps \ Root \ WEB-INF directory with notepad as follows:
.
.
.
<! -- Jspc servlet mappings start -->

<Servlet>
<Servlet-Name> org. Apache. jsp. index_jsp </servlet-Name>
<Servlet-class> org. Apache. jsp. index_jsp </servlet-class>
</Servlet>

<Servlet-mapping>
<Servlet-Name> org. Apache. jsp. index_jsp </servlet-Name>
<URL-pattern>/index. jsp </url-pattern>
</Servlet-mapping>


<Servlet>
<Servlet-Name> helloworld </servlet-Name>
<Servlet-class> helloworld </servlet-class>
</Servlet>

<Servlet-mapping>
<Servlet-Name> helloworld </servlet-Name>
<URL-pattern>/servlet/helloworld </url-pattern>
</Servlet-mapping>

<! -- Jspc servlet mappings end -->

</Web-app>

In this section, Servlet declares the servlet you want to call, while servlet-mapping maps the declared servlet to the/servlet/helloworld address.

Start tomcat, start the browser, and enter http: // localhost: 8080 // Servlet/helloworld. If you see the output helloworld! It indicates that the servlet has been compiled successfully.
Note: You must restart tomcat after modifying web. xml and adding a new class.

You can also test it in a self-created directory, for example:
Copy helloworld. class to the C: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ webapps \ MyApp \ WEB-INF \ Classes directory
If the classes directory does not exist, create
Open the Web. xml file in the c: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ webapps \ MyApp \ WEB-INF directory with notepad as follows:
.
.
.
<! -- Jspc servlet mappings start -->

<Servlet>
<Servlet-Name> helloworld </servlet-Name>
<Servlet-class> helloworld </servlet-class>
</Servlet>

<Servlet-mapping>
<Servlet-Name> helloworld </servlet-Name>
<URL-pattern>/servlet/helloworld </url-pattern>
</Servlet-mapping>

<! -- Jspc servlet mappings end -->

</Web-app>

Start tomcat, start the browser, and enter http: // localhost: 8080/MyApp/servlet/helloworld. If you see the output helloworld! It indicates that the servlet has been compiled successfully.
Note: You must restart tomcat after modifying web. xml and adding a new class.

Create your own JavaBean:

Use NotePad to create a Java program named testbean. java. The file content is as follows:
Package test;
Public class testbean {
Private string name = NULL;
Public testbean (string strname_p ){
This. Name = strname_p;
}
Public void setname (string strname_p ){
This. Name = strname_p;
}
Public String getname (){
Return this. Name;
}
}

Compile
Place testbean. Java under c: \ test and compile it with the following command:
C: \ test> javac testbean. Java
Then a compiled bean file testbean. class will be generated under c: \ test.
Cut the testbean. Class file to the C: \ Program Files \ Apache Software Foundation \ tomcat5.5 \ webapps \ MyApp \ WEB-INF \ Classes \ test directory,
If there are no subdirectories, create

Create a new testbean. jsp file with the following content:
<% @ Page import = "test. testbean" %>
<HTML> <body> <center>
<%
Testbean = new testbean ("this is a test Java Bean .");
%>
Java Bean name is: <% = testbean. getname () %>
</Center> </body>

Restart tomcat, start the browser, and enter http: // localhost: 8080/MyApp/testbean. jsp. If you see the output Java Bean name is: this is a test Java Bean
It indicates that the an has been compiled successfully.

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.