Practice: Set up Tomcat 5 + MySQL5 + JSP under Ubuntu Dapper

Source: Internet
Author: User
Tags blank page

Transferred from: forum.ubuntu.org.cn Author:Vstar

First, check that jdk has been installed in the Installation Guide (wiki.ubuntu.org.cn ), and then add the JAVA environment variable to the system.
Sudo gedit/etc/bash. bashrc
Add at the end
Export JAVA_HOME =/usr/jvm/java-1.5.0-sun
Export JRE = $ JAVA_HOME/jre

I. Tomcat
1. Installation
Sudo aptitude install tomcat5 tomcat5-admin tomcat5-webapps
2. Settings
Tomcat default admin and manager accounts are not enabled, You can edit/usr/share/tomcat5/conf/tomcat-users.xml file to enable

Cd/usr/share/tomcat5/conf
Sudo gedit tomcat-users.xml

Add two roles, admin and manager, and add a user root with a password of 123456 (here is an example. You can set the user name and password by yourself). The edited file is:

Reference:
<? Xml version = '1. 0' encoding = 'utf-8'?>
<Tomcat-users>
<Role rolename = "tomcat"/>
<Role rolename = "role1"/>
<Role rolename = "admin"/>
<Role rolename = "manager"/>
<User username = "root" password = "123456" roles = "admin, manager"/>
<User username = "tomcat" password = "tomcat" roles = "tomcat"/>
<User username = "both" password = "tomcat" roles = "tomcat, role1"/>
<User username = "role1" password = "tomcat" roles = "role1"/>
</Tomcat-users>


Save and exit. Restart tomcat.
Sudo/etc/init. d/tomcat5 restart
3. Test
In this case, open http: // 127.0.0.1: 8180 in the browser. When you see the page, it indicates that Tomcat has been installed successfully. Then, you can click Tomcat Administration and Tomcat Manager on the left side to manage the Administrator and site. Run the instances in Examples on the left of the homepage to testJspRunning status.
In fact, I personally think it is not necessary to add this management user. Due to permissions, the changes made on the management page will disappear after being restarted. Therefore, if you must make changes on the Management page, set the Tomcat directory permission to be writable by all users. That is, sudo chmod 777-R/usr/share/tomcat5. I personally do not recommend this and this practice, because this will undermine security. We recommend that you use text for configuration, which will be discussed later.

Ii. MySQL
1. Installation
Sudo aptitude install mysql-admin mysql-client-5.0 mysql-navigator mysql-query-browser mysql-server-5.0
2. Test and create a test account
Enter the terminal after installation
Sudo mysql-uroot
Create a new test account javatest with the password javadude and specify that you can log on from any host to authorize this user to have all the permissions of the database javatest. Add a database javatest

Reference:
Mysql> grant all privileges on javatest. * TO javauser @ "%" identified by "javadude ";
Mysql> create database javatest;


3. Install the JDBC driver
Download the driver to the http://dev.mysql.com/downloads/connector/j/5.0.html, just extract the mysql-connector-java-5.0.3-bin.jar file, copy to/usr/share/tomcat5/common/lib.
Education Network speed is slow, you can download to the http://mysql.mirror.edu.cn.
4. Test Tomcat + MySQL
Add a virtual directory
Cd/usr/share/tomcat5/conf/Catalina/localhost
Sudo gedit javatest. xml
Add the following information:

Reference:
<Context path = "/javatest" docBase = "javatest" debug = "0" reloadable = "true"/>


Enter the tomcat5 webapps directory, add a javatest directory, and add a file test.Jsp
Cd/usr/share/tomcat5/webapps
Sudo mkdir javatest
Sudo gedit test.Jsp
The content in the edit is

Reference:
<% @ Page contentType = "text/html; charset = UTF-8" %>
<% @ Page language = "java" %>
<% @ Page import = "com. mysql. jdbc. Driver" %>
<% @ Page import = "java. SQL. *" %>
<%
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
Connection conn = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/javatest? User = javauser & password = javadude ");
Statement stmt = conn. createStatement ();
%>


We recommend that you use this method for testing. Of course there are two ways to add a virtual directory. One is to edit the server under conf. xml put the previous javatest. add server to the content in xml. before the last page in xml JspPackage and create an archive file, select the war type, and put it under webapps. Tomcat will automatically install the virtual directory named javatest.

Note: There may be several problems so far. We will talk about them later.

Restart Tomcat
Sudo/etc/init. d/tomcat5 restart
Generally, this is enough. Open the browser, enter http: // 127.0.0.1: 8180, and enter Tomcat Manager. You can see a directory named javatest. After you click it, you will see a file list and click test.Jsp. If you see a blank page, it means it is normal. In this case, you can log on to mysql from the terminal and run the show processlist command to view the user information currently connected.

What? Open test.JspOnly one error message is displayed? Don't worry, as I said, there may be several problems here. If the above configuration is correct under normal circumstances, you can see the connection information on the blank page and MySQL. However, there are currently two problems with MySQL and Tomcat in Ubuntu (I don't know if it is a BUG), so it cannot be connected normally.

* Problem 1: It is possible that MySQL does not use locahost: 3306, that is, 127.0.0.1: 3306, but uses the local IP address or other addresses as the connection address. You can use telnet 127.0.0.1 3306 to test whether the connection is rejected.JspThe access denied is similar.
* Question 2: Use/etc/init. d/tomcat cannot connect to the database when starting the tomcat service. Instead, you must use/usr/share/tomcat/bin/startup. sh to start.

It was very difficult to find out the cause. I thought the problem was caused by the jdbc driver. Then I downloaded and added mysql-connnect for debugging. It took more than a week to solve the problem.
Now we can solve the problem.
First, edit the MySQL configuration file/etc/mysql/my. cnf, replace the default value of bind-address with 127.0.0.1.
Then stop tomcat
Sudo/etc/init. d/tomcat stop
Start tomcat through startup. sh
Sudo/usr/share/tomcat5/bin/startup. sh
OK. In the browser, enter http: // 127.0.0.1: 8180/javatest/test.JspPress enter. The page is blank. Log on to mysql under the terminal and use show processlist; view. You can see that a User whose User is javauser has logged on.
However, I had the chance to automatically run Tomcat 5 under/etc/init. d. I always wanted to find the problem with this file and correct it, but I am not very familiar with shell programming. The current solution can only disable the running of this file at startup and start the server using startup. sh in usr/share/tomcat5/bin.

 

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.