Jsp+tomcat+mysql and Sevlet and JavaBean configuration whole process

Source: Internet
Author: User
Tags connect sql mysql mysql client variables string stmt version
js|mysql| process

In the process of configuring JSP development environment will encounter a lot of problems, thanks to the experience of many predecessors on the Internet, the author cl41 JSP connection MySQL database introduction and author Saulzy MySQL learning notes and other articles for me is simply snow to send carbon, In order to help beginners like me here summed up a lot of the author's experience, thank them very much for their selfless dedication, but also hope that more people to carry forward this spirit, hehe, again to the author of the reference article to express the most lofty respect!
The following software download link address can not be displayed, please go directly to the official website to download it!

Software downloads
Mysql
Download version: 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, configure J2SDK:

Perform J2SDK setup, install by default, or customize the path, but modify the following configuration

Configure J2SDK:

To configure environment variables:

My Computer-> Properties-> the advanced-> environment variable-> system variable 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%;%systemroot%\system32\wbem

Write a simple Java program to test whether the J2SDK has been installed successfully:
public class Hello
{
public static void Main (String args[])
{
System.out.println ("Hello");
}
}

Save the program as a file with a file name of Hello.java.

Open a Command Prompt window and enter the directory where Hello.java is located, type the following command
Javac Hello.java
Java Hello
At this point if the print out Hello is installed successfully, if not printed out this sentence, carefully check the above configuration is correct.
Note The System Folder option should determine "Hide extensions for known file types" not checked (my Computer-tools-view)

Install, configure Tomcat
Perform the Tomcat installer, install by default, or customize the path, but modify the following configuration

My Computer-> Properties-> Advanced-> environment variable-> system variable 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 to add the Servlet-api.jar under the Tomat installation directory to 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, Access http://localhost:8080in IE, and if you see the Tomcat Welcome page, the installation is successful.

installing MySQL
Decompress Mysql-4.1.13-win32.zip, run Setup.exe
The first thing that appears is the Installation Wizard welcome interface, click "Next" directly
Continue, select the installation type, select "Custom" Custom install, then click "Next" Next, the Custom installation interface appears
Select installation path: C:\MySQL Server 4.1 (customizable)
Click OK to return to the custom installation interface, path changed to set path
Point "Next" to start the installation
Click "Install" to start the installation
The interface to create the MySQL.com account is present when completed
If you are using MySQL for the first time, select "Create anew free mysql.com accout"
Click "Next", enter your email address and your own set of password for login mysql.com
When you are finished, click Next to enter the second step.
Fill in the name and other related information, fill out the point "Next", enter the third step
After completing the phone number, company name, and so on, click "Next",
Then there appears a preview of the information you just filled in the interface, click "Next" appears the installation completion interface
Note that there is a configuration Wizard option (Configure the MySQL Server now),
It is recommended to configure your MySQL immediately. Many say the installation of MySQL can not start after the reason is not configured MySQL.
Click "Finish" to complete the installation and start to configure MySQL
Click "Next" to enter the configuration Type Selection page. Select "Detailed Configuration" (Detailed configuration)
Click "Next" to enter the service Type Selection page. Choose "Developer Machine" (developer machine) so that the resources of the system will not be much
Click "Next" to enter the database Usage Selection page. Select "Multifunctional Database"
Click "Next" to enter the InnoDB data storage Location page
Do not change the settings, directly in the installation path installation directory, and then click "Next", choose the number of concurrent MySQL connection
Select "Manual Setting", set to 100 (set as appropriate, as required)
Click "Next" to configure MySQL's port in the TCP/IP communication environment to select the default 3306 port.
Click "Next" to select the character settings in MySQL
Note that the choices here will affect whether you can use Chinese in MySQL. Select gb2312 character set to support Simplified Chinese
Click "Next" to set Windows service options
Note that the choice here is critical.
"Install as Windows Service" must be checked, this is the MySQL as Windows services run.
"Service Name" is the default "MySQL"
The following "Launch the MySQL Server automatically" must be checked, so that when Windows starts, MySQL will automatically start the service, or you will manually start MySQL.
Many people say that after installing MySQL, unable to start, cannot connect, there are 10061 errors, the reason is here.
Click "Next" to set the login password for root account root
"Modify security Settings" is to set the root account password, enter your password can be set.
"Create a Anonymous account" is the creation of an anonymous accounts, which will cause unauthorized users to access your database illegally, there are security concerns, the proposal does not check.
Point "Next", MySQL Configuration wizard will be based on all the settings above you to configure MySQL, so that the operation of MySQL to meet your needs
Click "Execute" to start configuration, when "service started successfully", the description of your configuration completed, MySQL service started successfully
Point "Finish" completed, the entire MySQL configuration completed, the rest is to use MySQL client connection MySQL server, and then used.


To install the JDBC driver:
Decompression Mysql-connector-java-3.1.10.zip
will be using Mysql-connector-java-3.1.10-bin-g.jar and Mysql-connector-java-3.1.10-bin.jar.

Configuration
The MYSQLFORJDBC subdirectory is established under the C:\Program Files\java directory, which will be mysql-connector-java-3.1.10-bin.jar into the directory
Enter the C:\Program files\java\jdk1.5.0_04\lib directory to copy Mysql-connector-java-3.1.10-bin-g.jar to the 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; Go to the environment variable
After appending the environment variables 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 have the Java application find the driver to connect to MySQL.


View and start the MySQL service
After installing MySQL under Windows XP, it has started the service automatically and has a shortcut connection to its client in the Start menu
Can be viewed through the Windows Service Manager. "Start"-"Run", enter "Services.msc", carriage return.
Windows Service Manager pops up, and then you can see the service item with the service name "MySQL", with the right labeled "Started"
In the Start menu-All Programs-mysql-mysql Server 4.1-mysql Command line client connects with shortcuts to clients
Enter the setup is the password you can


Use of the database

After MySQL is installed, in the Start menu-All Programs-mysql-mysql Server 4.1-mysql Command line client uses the client's shortcut connection
Enter the password set at installation

Basic commands for MySQL (you must have a semicolon at the end of the MySQL command line edit after each input command)
Display database: show databases;
Using database: Use database name;


Build a library
Build a database in MySQL, and a table in the database about
Command: Create database A;

Set permissions for a database (user and password)
Command: Grant all privileges on first.* to Test@localhost identified by "123456";
When you're done with this command, you can only do this with the user name: Test, Password: 123456 When you log on, so that you avoid using the root
Enter command: Use A;
Use of the A-database;

To build a table in the I-Library
Command: CREATE table about (ID int (8) Primary key,name varchar (10));

In the table if the data:
Command: INSERT into about values (' xyw1026 ', ' Laojiang ');

Exit
Command: Exit

JSP connection MySQL
C:\Program files\apache Software Foundation\tomcat5.5\webapps directory to create subdirectories MyApp
Enter the C:\Program files\apache Software Foundation\tomcat5.5\webapps\myapp Directory
Write a file in Notepad to save as first.jsp
The code is as follows:
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<body>
<%class.forname ("Org.gjt.mm.mysql.Driver"). newinstance ();
String url = "jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useunicode=true& Characterencoding=8859_1 "
The name of your database
Connection conn= drivermanager.getconnection (URL);
Statement stmt=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);
String sql= "select * from";
ResultSet rs=stmt.executequery (SQL);
while (Rs.next ()) {%>
Your first field content is: <%=rs.getstring (1)%>
The contents of your second field are: <%=rs.getstring (2)%>
<%}%>
<%out.print ("Successful database operation, congratulations");%>
<%rs.close ();
Stmt.close ();
Conn.close ();
%>
</body>

In the browser, enter:
http://127.0.0.1:8080/myapp/first.jsp
If present:
Id|num
0 |laojiang

Database operation successful, congratulations!

Indicates a successful configuration


Enter C:\Program files\apache Software Foundation\tomcat5.5\webapps\myapp Directory
Create a new directory in the MyApp directory Web-inf, note that the directory name is case-sensitive;
Under Web-inf, create a new file with Notepad named Web.xml, which reads as follows:

<?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>

Under MyApp, create a new test JSP page with Notepad, the file name is index.jsp, and the file reads as follows:

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


Restart Tomcat
Open the browser and enter http://localhost:8080/myapp/index.jsp
When you see the current time, the installation is successful.

To build your own servlet:
Create a new servlet program with Notepad, the file name is Helloworld.java, and the file reads 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 ("Out.println ("This is the My A-Servlet");
Out.println ("</title>Out.println ("Out.println ("</body>

}
}


Compile
Cut the Helloworld.java to the C:\Program files\apache Software foundation\tomcat5.5\common\classes Directory
Command-Line Input:
CD C:\Program Files\apache Software foundation\tomcat5.5\common\classes
Javac Helloworld.java
Use Javac Helloworld.java to compile this file if there is no import javax.servlet.*
The Servlet-api.jar file is copied from the C:\Program files\apache Software foundation\tomcat5.5\common\lib directory and added to the
C:\Program Files\java\jre1.5.0_04\lib\ext directory.

Then a compiled servlet file is generated under C:\Program files\apache Software foundation\tomcat5.5\common\classes: Helloworld.class
With Notepad, the web.xml under the C:\Program files\apache Software foundation\tomcat5.5\webapps\root\web-inf directory is shown below, and the added content is 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>

The servlet section of this passage declares the servlet you want to invoke, while servlet-mapping "maps" the declared servlet to the/servlet/helloworld address

Start Tomcat, start the browser, enter http://localhost:8080//servlet/HelloWorld If you see the output helloworld! The servlet that wrote the instructions was successful.
Note: The Web.xml is modified and the new class is added to restart Tomcat


You can also test in the directory you created:
Copy the Helloworld.class to the C:\Program files\apache Software foundation\tomcat5.5\webapps\myapp\web-inf\classes Directory
Where the classes directory does not create a
Use Notepad to open the Web.xml in the C:\Program files\apache Software foundation\tomcat5.5\webapps\myapp\web-inf directory, as shown below, with the following modifications:
.
.
.
<!--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, enter http://localhost:8080/myapp/servlet/HelloWorld If you see the output helloworld! The servlet that wrote the instructions was successful.
Note: The Web.xml is modified and the new class is added to restart Tomcat


To build your own JavaBean:

Create a new Java program with Notepad, the file name is Testbean.java, and the file reads 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 the Testbean.java under C:\Test and compile with the following command:
C:\test>javac Testbean.java
Then a compiled bean file is generated under C:\Test: Testbean.class
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 a

Create a new testbean.jsp file with the following contents:
<%@ page import= "test. Testbean "%>
<%
Testbean testbean=new Testbean ("This is a test Java bean.");
%>
Java Bean name is: <%=testbean.getname ()%>
</center></body>

Reboot Tomcat, start browser, enter http://localhost:8080/myapp/TestBean.jsp If you see output java Bean name Is:this is a test Java bean
The JavaBean succeeded in writing the instructions.



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.