You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Bagus <ba...@cox.net> on 2005/05/12 00:38:59 UTC
database - jdbc setup help
Hey there newbie here again,
I've now set up Tomcat 5.5.4 and have installed
mysql-standard-4.1.9-unknown-freebsd4.7-i386 on my Freebsd 5.3 box intel
box.
I've loaded some databases and tables into the database.
How do I get them to work together with jsp?
Can anyone point me to a short-sweet tutorial that can get me from here to
there?
I'm not sure if I need to install jdbc and the mysql driver or if those are
included in the tomcat release.
A tutorial that takes me through all that would be great.
Thanks
Bagus
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: database - jdbc setup help
Posted by Lutz Zetzsche <Lu...@sea-rescue.de>.
Hi Bagus,
Am Freitag, 13. Mai 2005 04:04 schrieb Bagus:
> Wow, great.
>
> That worked like a charm. After following your simple instructions,
> your script listed my database tables. Amazing!
Many thanks for the compliment. :-) I have worked this out for me from
the various how-tos. The problem is that most of them presume that you
know some prerequisites. And a newbie can fail there. So I have
documented it with a working example for me once and forever.
> I don't quite get the JSTL stuff tho... Your script says:
> > <%--
> > The taglib directive below imports the JSTL library. If you
> > uncomment it, you must also add the JSTL library to the project.
> > The Add Library...
>
> action
>
> > on Libraries node in Projects view can be used to add the JSTL 1.1
>
> library.
>
> > --%>
> > <%--
> > <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
> > --%>
>
> So where are the "Projects View", "Libraries Node", "Add Library
> action", etc. I was thinking maybe in the tomcat admin or manager web
> interfaces, but couldn't find it.
>
> Thanks,
Well, I created this test page with NetBeans IDE and just changed to the
default page what I needed to. The JSTL things are added by NetBeans.
However, if you would like to get JSTL to work you only have to download
it somewhere and put the classes into the suitable classes resp. lib
directory. As I haven't used JSTL yet, I have no further details but I
am sure you can easily work it out with Google.
Best wishes
Lutz
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
RE: database - jdbc setup help
Posted by Bagus <ba...@cox.net>.
Wow, great.
That worked like a charm. After following your simple instructions, your
script listed my database tables. Amazing!
I don't quite get the JSTL stuff tho... Your script says:
> <%--
> The taglib directive below imports the JSTL library. If you uncomment it,
> you must also add the JSTL library to the project. The Add Library...
action
> on Libraries node in Projects view can be used to add the JSTL 1.1
library.
> --%>
> <%--
> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
> --%>
So where are the "Projects View", "Libraries Node", "Add Library action",
etc. I was thinking maybe in the tomcat admin or manager web interfaces, but
couldn't find it.
Thanks,
b.
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
RE: database - jdbc setup help
Posted by Geoff Wiggs <gw...@orcastech.com>.
Lutz,
Thank you for the excellent primer on Tomcat DB setup. I'm going to keep
this around forever. Quick question, how can you tell that it is really
using the db pool? Is there a monitoring tool somewhere that would show the
number of connections in use?
Geoff Wiggs
Orcas Technologies, Inc
-----Original Message-----
From: Lutz Zetzsche [mailto:Lutz.Zetzsche@sea-rescue.de]
Sent: Thursday, May 12, 2005 12:14 AM
To: Tomcat Users List
Subject: Re: database - jdbc setup help
Hi Bagus,
Am Donnerstag, 12. Mai 2005 00:38 schrieb Bagus:
> I've now set up Tomcat 5.5.4 and have installed
> mysql-standard-4.1.9-unknown-freebsd4.7-i386 on my Freebsd 5.3 box
> intel box.
> I've loaded some databases and tables into the database.
>
> How do I get them to work together with jsp?
>
> Can anyone point me to a short-sweet tutorial that can get me from
> here to there?
>
> I'm not sure if I need to install jdbc and the mysql driver or if
> those are included in the tomcat release.
>
> A tutorial that takes me through all that would be great.
Firstly, you have to install the MySQL JDBC driver. That means you have
to download it from here:
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: database - jdbc setup help
Posted by Lutz Zetzsche <Lu...@sea-rescue.de>.
Hi Bagus,
Am Donnerstag, 12. Mai 2005 00:38 schrieb Bagus:
> I've now set up Tomcat 5.5.4 and have installed
> mysql-standard-4.1.9-unknown-freebsd4.7-i386 on my Freebsd 5.3 box
> intel box.
> I've loaded some databases and tables into the database.
>
> How do I get them to work together with jsp?
>
> Can anyone point me to a short-sweet tutorial that can get me from
> here to there?
>
> I'm not sure if I need to install jdbc and the mysql driver or if
> those are included in the tomcat release.
>
> A tutorial that takes me through all that would be great.
Firstly, you have to install the MySQL JDBC driver. That means you have
to download it from here:
http://dev.mysql.com/doc/mysql/en/java-connector.html
Then untar it somewhere outside the $CATALINA_HOME and only put the
mysql-connector-java-3.1.8-bin.jar file into
$CATALINA_HOME/common/lib/. Put it only there and nowhere else!
Secondly, nest the following Resource definition into the Context
element of your web application's context file:
<Resource
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
logAbandoned="true"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/{ResourceName}"
password="{UnencryptedUserPassword}"
removeAbandoned="true"
removeAbandonedTimeout="300"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/{DatabaseName}?autoReconnect=true"
username="{DatabaseUserName}"
/>
You have to set the following parameters accordingly:
- name/{ResourceName}:
How you want to call your resource is up to you, i.e.
"jdbc/{DatabaseName}" where {DatabaseName} stands for the name of the
database which this resource will connect to.
- url/{DatabaseName}:
The hostname should normally be "localhost" and the port "3306";
you only have to change this, if your database doesn't run on
"localhost" and port 3306;
most importantly you have to set the database name right; here you
tell the resource which database from the mysql server to use;
it is vitally important to set the database name absolute correctly,
as it is also case-sensitive (dataBase != database !!!); if it is no
absolutely correct, your database connection will fail!
- username/{DatabaseUserName}:
A database user which has permission to access the database,
specified in the url. If necessary, grant rights in the mysql.user
table and in other tables of the mysql database.
- password/{UnencryptedUserPassword}:
The unencrypted password of the mysql user, specified with username.
Thirdly, you have set the following resource reference in the web.xml of
your web application:
<resource-ref>
<description>MySQL Database Connection</description>
<res-ref-name>jdbc/{ResourceName}</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
The description, you choose, is free. The res-ref-name has to be the
name of the resource defined in your Context file (see above).
After you have made all these configurations, you have to restart Tomcat
to activiate the new resource. Please note, that the above
configuration uses the Tomcat DBCP:
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/jndi-datasource-examples-howto.html
And finally, you will find a short test script at the bottom of this
mail which I have used to test my MySQL database connection. It will
show you the tables in the database if the connection was established
successfully and the sql query was executed successfully as well. Don't
forget to set the {ResourceName} correctly in line 41 of the script.
If you should run Tomcat using the Security Manager, you have to grant
the necessary runtime and network-socket permissions for the connection
in the §CATALINA_HOME/conf/catalina.policy file. I don't think that you
run Tomcat with the Security Manager, but if you should, just drop a
line. Yesterday I have figured out, which permission I have to set to
connect to a MySQL database while the Security Manager is running.
Good luck!
Lutz
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<%--
The taglib directive below imports the JSTL library. If you uncomment
it,
you must also add the JSTL library to the project. The Add Library...
action
on Libraries node in Projects view can be used to add the JSTL 1.1
library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>JSP Page</h1>
<%
InitialContext cxt = null;
DataSource ds = null;
Connection conn = null;
Statement sql = null;
ResultSet res = null;
try {
cxt = new InitialContext();
ds = (DataSource)
cxt.lookup("java:/comp/env/jdbc/{ResourceName}");
out.print(ds);
conn = ds.getConnection();
sql = conn.createStatement();
sql.execute("show tables;");
res = sql.getResultSet();
out.print("<ul>");
while (res.next()) {
out.print("<li style=\"color: #00ff00;
\">"+res.getString(1)+"</li>");
}
out.print("</ul>");
res.close();
res = null;
sql.close();
sql = null;
conn.close();
conn = null;
} catch (NamingException namE) {
out.print("<p>NamingException:<br><b style=\"color: #ff0000;
\">"+namE+"</b></p>");
} catch (SQLException sqlE) {
out.print("<p>SQLException:<br><b style=\"color: #ff0000;
\">"+sqlE+"</b></p>");
} catch (NullPointerException nulE) {
out.print("<p>NullPointerException:<br><b style=\"color: #ff0000;
\">"+nulE+"</b></p>");
} catch (Exception E) {
out.print("<p>Exception:<br><b style=\"color: #ff0000;
\">"+E+"</b></p>");
} finally {
if (res != null) {
try { res.close(); } catch (SQLException e) { ; }
res = null;
}
if (sql != null) {
try { sql.close(); } catch (SQLException e) { ; }
sql = null;
}
if (conn != null) {
try { conn.close(); } catch (SQLException e) { ; }
conn = null;
}
}
%>
<%--
This example uses JSTL, uncomment the taglib directive above.
To test, display the page like this:
index.jsp?sayHello=true&name=Murphy
--%>
<%--
<c:if test="${param.sayHello}">
<!-- Let's welcome the user ${param.name} -->
Hello ${param.name}!
</c:if>
--%>
</body>
</html>
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: tomcat and virtual hosts
Posted by Pete Stevens <pe...@ex-parrot.com>.
On Wed, 11 May 2005, Bagus wrote:
>
> Here's a more general question about Tomcat.
>
> I'm used to using apache to set up virtual hosts, but when Tomcat installed,
> http://localhost/ now goes to my tomcat installation. So how does one set up
> multiple virtual hosts with Tomcat? Is it in the server.xml file or
> something? Or do I somehow now modify my apache configuration to be able to
> use tomcat and the jsp files?
Here's my guide to making Tomcat behave like Apache, rather than the J2EE .war
file fashion.
http://www.ex-parrot.com/~pete/tomcat-vhost.html
Yours,
Pete Stevens
--
Pete Stevens
pete@ex-parrot.com
http://www.ex-parrot.com/~pete/
It was either burnt to a cinder or it escaped through a small hole in the
corner of the shed, but I imagine it perished and went to bunny heaven.
-- Fire Station Commander on a burning bunny rabbit that cause GBP60000 damage
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: tomcat and virtual hosts
Posted by Lutz Zetzsche <Lu...@sea-rescue.de>.
Hi Bogus,
Am Donnerstag, 12. Mai 2005 00:52 schrieb Bagus:
> Here's a more general question about Tomcat.
>
> I'm used to using apache to set up virtual hosts, but when Tomcat
> installed, http://localhost/ now goes to my tomcat installation. So
> how does one set up multiple virtual hosts with Tomcat? Is it in the
> server.xml file or something? Or do I somehow now modify my apache
> configuration to be able to use tomcat and the jsp files?
have a look here:
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/engine.html
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/host.html
http://jakarta.apache.org/tomcat/tomcat-5.5-doc/config/context.html
You need to know a bit about all three to successfully set up a virtual
host.
(1) ENGINE
As regards the Engine element, the only important thing is the
defaultHost parameter:
"The default host name, which identifies the Host that will process
requests directed to host names on this server, but which are not
configured in this configuration file. This name MUST match the name
attributes of one of the Host elements nested immediately inside."
"You can nest one or more Host elements inside this Engine element, each
representing a different virtual host associated with this server. At
least one Host is required, and one of the nested Hosts MUST have a
name that matches the name specified for the defaultHost attribute,
listed above."
So, if you should later change the hostname of the default host, that
means the name attribute of the Host element representing the default
host, you must not forget to set the defaultHost attribute of the
Engine element accordingly.
(2) HOST
Different to the Apache HTTP server, a virtual host is not equivalent
with a web application in Tomcat terms. It can host several web
applications, and so the Host element only contains the appBase
attribute and not a docBase attribute:
"appBase: The Application Base directory for this virtual host. This is
the pathname of a directory that may contain web applications to be
deployed on this virtual host."
(3) CONTEXT
Concerning the Context element, you have to observe three things:
a) "You may define as many Context elements as you wish. Each such
Context MUST have a unique context path, which is defined by the path
attribute. In addition, you MUST define a Context with a context path
equal to a zero-length string. This Context becomes the default web
application for this virtual host, and is used to process all requests
that do not match any other Context's context path."
b) Normally, your context files will be placed here:
$CATALINA_HOME/conf/[enginename]/[hostname]/
So, if you should later change the hostname, that means the name
attribute of the Host element, you must not forget to rename the
directory of the associated context files accordingly.
c) For the web application which you want to run as ROOT application -
http://localhost:8080/ instead of http://localhost:8080/webapp/ - need
to have a context file called ROOT.xml:
$CATALINA_HOME/conf/[enginename]/[hostname]/ROOT.xml
Best wishes
Lutz
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
RE: tomcat and virtual hosts
Posted by Fritz Schneider <Fr...@Peacham.HomeIP.net>.
Bagus,
Each virtual host is a host element in server.xml. The service (e.g.
"Catalina") has connectors to the ports for incoming requests. The engine in
that service sorts out the requests for a particular virtual host based on
the name attribute of the hosts. Any which don't match go to the defaultHost
(e.g. localhost). Each host can have its own appBase as well as logs, root
context, etc.
Fritz
-----Original Message-----
From: Bagus [mailto:bagus@cox.net]
Sent: Wednesday, May 11, 2005 3:53 PM
To: Tomcat Users List
Subject: tomcat and virtual hosts
Here's a more general question about Tomcat.
I'm used to using apache to set up virtual hosts, but when Tomcat installed,
http://localhost/ now goes to my tomcat installation. So how does one set up
multiple virtual hosts with Tomcat? Is it in the server.xml file or
something? Or do I somehow now modify my apache configuration to be able to
use tomcat and the jsp files?
Thanks,
Bagus
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
tomcat and virtual hosts
Posted by Bagus <ba...@cox.net>.
Here's a more general question about Tomcat.
I'm used to using apache to set up virtual hosts, but when Tomcat installed,
http://localhost/ now goes to my tomcat installation. So how does one set up
multiple virtual hosts with Tomcat? Is it in the server.xml file or
something? Or do I somehow now modify my apache configuration to be able to
use tomcat and the jsp files?
Thanks,
Bagus
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org