Tomcat + mysql + eclipse JSP environment Configuration

Source: Internet
Author: User

I have been eager to learn JSP, But I was rejected by the configuration environment before getting started! It took a lot of time (555 .. I am so stupid !!!), I found a lot of information on the Internet and books, and found that many of them were incomplete and not detailed enough. Finally, after patiently repeat the configuration and installation, we have done a good job of the tool!
The joy of this success did not dare to stand alone. This article is dedicated to all the first buddies who have been confused in the vast amount of configuration materials and are still embarrassed to configure the configuration environment. This article is an encouragement! As a summary of this bird's initial experience!
In this article, WIN2000/XP + J2SDK1.4.2/1.5 + TOMCAT5.0.28/5.5.9 + MYSQL4.1 + ECLIPSE3.02/3.1 is passed. Different versions have little impact on the configuration. Because the TOMCAT5.5 ADMIN package needs to be downloaded by itself, we recommend that you use version 0.28.
Configuration is annoying, so you don't have to complete the configuration once. This is very exhausting. Come on step by step! Finally, because I am a newbie, I am welcome to the mistakes in the text !!! I hope this article will help you !!!
Step 1: (software preparation)
J2SDK (download URL: http://java.sun.com/j2se)
TOMCAT (download URL: http://www.apache.org/dist/jakarta)
MYSQL (http://www.mysql.com/downloads)
MYSQL-CONNERCTOR-JAVA (http://www.mysql.com/downloads)
ECLIPSE-SDK (download URL: http://www.eclipse.org/download)
Tomcat (tomcatpluginv31.zip) Sysdeo Eclipse Tomcat Launcher plugin
(Download URL: http://www.sysdeo.com/eclipse)
Here I use J2SDK1.4.2. 08 + and TOMCAT5.0.28 + ECLIPSE3.1 + tomcatPluginV31 (BETA)
-------
In addition, eclipse3.0.2contains Chinese plug-ins (nlpack-eclipse-sdk-3.0.x-win32.zip)
Download URL:
Http://sunsite.informatik.rwth-aachen.de/eclipse/downloads/drops/L-3.0.1_Translations-200409161125/
It can be used in conjunction with ECLIPSE3.0.2 (Chinese plug-ins are placed under Plugins );
The Lomboz plug-in also supports JSP syntax highlighting. Tanghan is an ECLIPSE database management plug-in,
Jboss is a J2EE server software.
The URLs are:
Http://www.objectlearn.com/
Http://www.hibernate.org/
Http://www.Jboss.org/
-----------
Step 2: (software installation and configuration)
A. J2SDK Installation
1.
Install J2SDK as instructed. (You can select a path and a named folder. I name it JDK,
The installation path is C: \ JDK)
* Note: Install with an independent JRE is not checked, because it is included in the JDK tool. This option is selected by default!
2.
Set the environment variables and go to my computer --> properties --> advanced --> environment variables,
Create the following variables and assign values:
The variable name JAVA_HOME is assigned "<JDK installation path directory>", for example, "C: \ JDK ;";
Variable name CLASSPATH value ".; % JAVA_HOME % \ lib \ tools. jar; % JAVA_HOME % \ lib \ dt. jar ;";
* Note: % JAVA_HOME % can be replaced by <JDK installation path directory>. In fact, % JAVA_HOME % is the above variable.
Variable PATH value: "% JAVA_HOME % \ bin ;";
3.
Now you can perform the test. Enter "java-version" in the command prompt.
The JDK version number indicates that your JDK software is correctly installed, as shown in the first line after I press Enter.
Is 'java version "1.5.0 _ 04 "'.
4.
Use NotePad to create a txt file in the "x: \" Directory (your drive letter, my name is "d: \"),
The input content is as follows:
Public class Test // Test is the class name
{
Public static void main (String [] args)
{
System. out. print ("This is a test! ");
}
}
Save it as the Test. java file in the "x: \" Directory (for example, my file is "d: \ Test. java ").
* Note: The file name must be the same as the class name, that is, the file name must be case sensitive and the input content must be the same.
At the command prompt, go to the directory of your "Test. java" file, that is, the "x: \" drive letter;
Enter "javac Test. java" and press Enter. If a prompt is displayed, it may be set as an environment variable.
Error, or an error occurs in the "Test. java" file. If the drive letter "x: \" and the input prompt appear again,
Indicates that the "Test. java" file is correctly compiled. You can see a "Test. class" in the "x: \" directory"
File. Now you can enter "java Test" at the input prompt and the result is:
"This is a test !".
------------------------------------------------------------------------------
B. TOMCAT installation
1.
The installation of Tomcat is very simple. Just follow the prompts one step! Select your preferred installation path (my "D: \ tomcat ")
Tomcat automatically selects the independent jre attached to JDK installation. Here I select "C: \ jdk \ jre" in JDK ".
All other items are default and are not modified.
Start Tomcat now, open IE, and enter "http: // localhost: 8080/" in the address bar.
The welcome page of Tomcat is displayed.
* Note: You can also use "http: // 127.0.0.1: 8080/". The two are the same.
2.
Set the environment variable again and go to my computer --> properties --> advanced --> environment variable,
The variable TOMCAT_HOME is assigned the value "<Tomcat installation path directory> ";
Select the variable CLASSPATH and append "% TOMCAT_HOME % \ common \ lib;" to the end ;",
After modification, the value of CLASSPATH should be ".; % JAVA_HOME % \ lib \ tools. jar;
% JAVA_HOME % \ lib \ dt. jar; % TOMCAT_HOME % \ common \ lib ;".
Similarly, % atat_home % can be replaced by <Tomcat installation path directory>.
3.
Find the webapps folder in the Tomcat installation path directory, and find the ROOT folder in it.
Create a txt file. The input content is as follows:
<Html>
<Head>
<Title> hello_html </title>
</Head>
<Body>
<Center> </Body>
</Html>
The "hello.html" file is stored in the "% TOMCAT_HOME % \ webapps \ ROOT \" directory.
A new hello.html file is created.
For example, after creating a file, the file location is "D: \ tomcat \ webapps \ ROOT \ hello.html ".
Start (or restart) Tomcat again, open IE, and enter
"Http: // localhost: 8080/hello.html" should be able to see
Displays the page with the "Hello Html" character.
Return to the webapps directory and create a folder named jsp_test. In the jsp_test folder
Create a new WEB-INF folder and create a txt file in it. The entry is 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> JspTest </display-name>
<Description>
A test.
</Description>
</Web-app>
Save as the "web. xml" file, that is, in "% TOMCAT_HOME % \ webapps \ jsp_test \ WEB-INF"
A "web. xml" file is created in the directory. For example, after creating a file,
The file location is D: \ tomcat \ webapps \ jsp_test \ WEB-INF \ web. xml ".
Return to the webapps directory to create a txt file. The input content is as follows:
<Html>
<Head>
<Title> hello_jsp </title>
</Head>
<Body>
<Center>
Now time is: <% = new java. util. Date () %>
<P>
<% Out. print ("Hello Jsp"); %>
</P>
</Center>
</Body>
</Html>
Save as "index. jsp", that is, in "% TOMCAT_HOME % \ webapps \ jsp_test \"
A new "index. jsp" file is created in the directory.
For example, after creating a file, the file is located at "D: \ tomcat \ webapps \ jsp_test \ index. jsp ".
Start (or restart) Tomcat, open IE, and enter "http: // localhost: 8080/jsp_test/" in the address bar /"
The "Now time is: 'Current time'" and "Hello Jsp" pages are displayed in the center.
Later, you can put your JSP file in this folder for debugging!
Return to the webapps directory to create a new folder named servlet_test, which contains a WEB-INF folder;
The WEB-INF contains the class folder and lib folder. For example, after I create a folder, the path is "D: \ tomcat \ webapps \ servlet_test \ WEB-INF \
Class (and lib )". Similarly, the web. xml file is created under D: \ tomcat \ webapps \ servlet_test \ WEB-INF \ 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>
<Servlet>
<Servlet-name> servlet_test </servlet-name>
<Servlet-class> HelloTomcat </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> servlet_test </servlet-name>
<Url-pattern>/* </url-pattern>
</Servlet-mapping>
</Web-app>
Create the "HelloTomcat. java" file in the "x: \" directory. The content is as follows:
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class HelloTomcat extends HttpServlet
{
Protected void doGet (HttpServletRequest arg0,
HttpServletResponse arg1)
Throws ServletException, IOException
{
PrintWriter out = arg1.getWriter ();
Out. print ("<center> ");
Out. print ("Hello Tomcat! ");
Out. print ("<br/> ");
Out. print ("This is my first servlet! ");
Out. print ("</center> ");
}
}
Compile with the following statement under the "x: \" drive letter:
"Javac-classpath.; <% TOMCAT_HOME %> \ common \ lib \ servlet-api.jar HelloTomcat. java"
* Note that "<% TOMCAT_HOME %>" must be in the actual directory of your Tomcat (for example, my directory is "D: \ tomcat ").
Another convenient way is to append "servlet-api.jar" to the back of CLASSPATH.
It is also worth mentioning that the servlet to use the package file to "% atat_home % \ webapps \ servlet_test \ WEB-INF \
Lib ", all the classes required by the servlet in the servlet_test folder will be
This can be easily referenced! (The same is true for JSP-generated servelt applications .)
Put your compiled servlet class files
D: \ tomcat \ webapps \ servlet_test \ WEB-INF \ class.
Start (or restart) Tomcat, open IE, and enter
& Quot; http: // localhost: 8080/servlet_test/& quot /"
We should be able to see "Hello Tomcat!" displayed in the center !"
And "This is my first servlet !" Page.
--------------------------------------------------------------------------------
C. installation and connection of MYSQL
1.
First extract the MYSQL-CONNERCTOR-JAVA to your favorite drive letter directory,
Then go to the MYSQL-CONNERCTOR-JAVA to unzip the file
Find a mysql-connector-java-3.x.x-bin.jar in the folder ",
Copy it to "% atat_home % \ common \ lib \" and
Under ".. \ WEB-INF \ lib \" in your JSP program folder (for example, "D: \ tomcat \ webapps \ jsp_test \ WEB-INF \ lib ").
2.
Windows is easy to install and can be installed as instructed. I also installed it under d,
There is nothing to say, there are run and configuration in the Start Menu.
Run the decompressed version by entering "D: \ MySQL \ bin \" in the Start Menu;
After the installation and configuration are complete, run MYSQL. If the password is set, enter the password first,
Otherwise, it can be used directly.
Run the following command to view the database:
"Mysql> show databases ;"
It is usually displayed in the dotted table box. The first database and the second database are mysql and test.
* Note: "mysql>" is an existing prompt. You do not need to enter a semicolon (;).
It indicates the end of the command line.
After you know which database you have, you can select the database test:
"Mysql> use test ;"
The table is added later:
"Mysql> create table t (id int (11) not null auto_increment,
Name varchar (50) not null, primay key (id ));"
Display table:
"Mysql> show tables ;"
Add a record for table t:
"Mysql> insert into t (id, name )"
"Mysql> value (123456, hello );"
View records:
"Mysql> select * from t ;"
You can see the table record. It is generally normal to follow the above steps.
3.
Start TOMCAT. Enter "http: // 127.0.0.1: 8080/admin/" in the IE column /",
Enter your account and password.
If the account has not been changed during TOMCAT installation, it should be "admin ",
The password is empty.
Select Resources> Data sources to go to the Data source configuration page,
Select Create New Data Source in the drop-down box in the upper right corner,
Go to the configuration details page. The content is as follows:
JNDI Name: jdbc/mysql (available as your favorite Name)
Data Source URL: jdbc: mysql: // localhost: 3306/test
(Here is the name of the test Database above)
JDBC Driver Class: com. mysql. jdbc. Driver
User Name: root (your MYSQL User Name, default: root)
Password: ******** (your MYSQL Password)
Max. Active Connections: 4
Max. Idle Connections: 2
Max. Wait for Connection: 5000
Validation Query:
The JNDI Name and other information, except JDBC DriverClass,
Others can be entered according to your needs.
* Note that the content of the Data Source URL may be:
"Jdbc: mysql: // <MYSQL Server IP address or name>/<Your Database Name> ".
Finally, click Save to Save and Commit Change to submit the changes. In this way, you can proceed to the next step.
Find the web. xml file under "% atat_home % \ conf \" and edit it to add content:
<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<Web-app...>
.
.
.
.
<Resource-ref>
<Description> DB Connection </description>
<Res-ref-name> jdbc/mysql </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>
</Web-app>
The vertex number is the original content that is omitted, and the added content is <resource-ref> to </resource-ref>.
At first glance, we can see that "jdbc/mysql" in <res-ref-name> jdbc/mysql </res-ref-name> must be consistent
The JNDI Name must be consistent when TOMCAT configures the data source.
Everything is ready. You can write the code for testing as follows:
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. SQL. *" %>
<Html>
<Body>
<%
Try
{
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
String url = "jdbc: mysql: // localhost/test? User = root & password = 123456 & useUnicode = true & characterEncoding = 8859_1 ";
Connection conn = DriverManager. getConnection (url );
Statement stmt = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
String SQL = "select * from t ";
ResultSet rs1_stmt.exe cuteQuery (SQL );
While (rs. next ())
{
Out. println ("one is:" + rs. getString (1 ));
Out. println ("tow is:" + rs. getString (2 ));
}
Out. println ("<p> Test is passing! </P> ");
Rs. close ();
Stmt. close ();
Conn. close ();
}
Catch (Exception e)
{
Out. print ("Exception:" + e. getMessage ());
}
%>
</Body>
</Html>
Save it as MySQLTest. jsp and put it in the "../test/" directory. Restart TOMCAT to test it!
The result should be:
One is: 123456 tow is: hello
Test is passing!
-------------------------------------------------------------------------------
Integration of D.ECLIPSE-SDK and TOMCAT
1.
Decompress the ECLIPSE-SDK of windows to "x: \" and change it to eclipse;
Tomcat (tomcatpluginv31.zip) Sysdeo Eclipse Tomcat Launcher plugin
Decompress the package to "x: \" and copy it to "eclipse \ plugins.
2.
If ECLIPSE is started for the first time, you are required to set the "workspace" path;
Check whether there are three TOMCAT icons. If not, check as shown above.
Set the JRE Virtual Machine, select Window> Preferences> Java from the ECLIPSE menu, and select Installed JREs.
Add a JRE by clicking the Add button, select the JDK, and click OK to check the newly added items in the table.
Set Tomcat, and select Window> Preferences> Tomcat (
This will not happen if Tomcat plug-in is not installed), set, Tomcat Version: Your tomcat Version,
Tomcat Home: <tomcat installation directory> Expand Tomcat items with the Advances option,
Tomcat base is the installation directory of tomcat;
Restart ECLIPSE. Can I see the Tomcat Project in FILE> NEW? Now, create a new one!
Change the Project name to your favorite one (I will change it to tomcat_jsp ),
Can update server. xml file must be checked.
Well, for convenience, place the web. xml (in the jsp_test \ WEB-INF folder) created for the jsp application in the jsp_test folder above ),
Copy to "eclipse \ workspace \ tomcat_jsp \ WEB-INF", this is the Project you just created.
Copy index. jsp to "eclipse \ workspace \ tomcat_jsp \" and restart ECLIPSE. Expand tomcat_jsp in the navigation window on the left. Double-click index. jsp.
Open it in ECLIPSE and see the code! Start TOMCAT in the menu bar,
In IE, input "http: // localhost: 8080/tomcat_jsp/index. jsp". Is the same result as above?
Try creating a JSP file! Right-click tomcat_jsp in the navigation window and select NEW-> FILE. The name must have the suffix ". jsp,
Content customization. Restart TOMCAT to test the function.
* Note: You must restart TOMCAT to modify the. xml file and create a new JSP file, but modify the existing JSP file without restarting.
The Servlet in ECLIPSE is similar to that in JSP. Refer to the above!
The configuration has been completed. It is enough for us to learn and use. Now you can start the JSP journey!
Finally, I wish you a happy and smooth learning experience !!
From http://www.javaresearch.org/forum/thread.jsp? Thread = 36847 thanks to the author for writing a good article

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.