Having nightmare configuring server.xml

Hi,

I have a server.xml file that I need to configure to get my jsp pages fetching data from mysql database. My Server is apache/tomcat 5.5.16 and MySQL version is 5.0. The connector .jar for MySQL is in place. Now I have to configure <Context> tag in server.xml for MySQL and also for each class name has to be defined via the server.xml.

I have 23 .class files on the server and the following in my <Host> tag in server.xml

<Host name="www.nichefhgs.com" appBase="/www/virtual/mja/www.nichefhgs.com"
        unpackWARs="true" autoDeploy="true">

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

        <Valve className="org.apache.catalina.valves.AccessLogValve"
                 directory="logs"  prefix="web1_access_log." suffix=".txt"
                 pattern="common" resolveHosts="false"/>
      </Host>

Please help, it has been a nightmare for me.

N.
n2alexanAsked:
Who is Participating?
 
objectsCommented:
no, to do that you would not need anything specific in your server.xml.
you'd just need to add the driver jar to your webapp
0
 
objectsCommented:
you could just build a war, and avoid touching server.xml all together


or in server.xml your context would look something like:

<Context path="/DBTest" docBase="DBTest"
        debug="5" reloadable="true" crossContext="true">

    <!-- maxActive: Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
         -->

    <!-- maxIdle: Maximum number of idle dB connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->

    <!-- maxWait: Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->

    <!-- username and password: MySQL dB username and password for dB connections  -->

    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
         -->
   
    <!-- url: The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.
         -->

  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>

</Context>

taken from http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
0
 
n2alexanAuthor Commented:
even if I have a class that connects to the database and this class is used for all my sql data reading and writing I would still have to add what you have suggested.

public class Connector {
   
    /** Creates a new instance of Connector */
    public Connector() {
    }
   
    private String dbUrl = "jdbc:mysql://localhost:3306/mydatabasename";
    private String jdbcDriver = "com.mysql.jdbc.Driver";
     
    public Connection getConnection() {
       
        Connection con = null;
       
        try {
           
            Class.forName(jdbcDriver);
            con = DriverManager.getConnection(dbUrl, "someuser", "somepassword");
           
        } catch (SQLException se) {
           
            System.err.print("getConnection SQLException: " + se.toString());
           
        } catch (ClassNotFoundException cnfe) {
           
            System.err.print("getConnection ClassNotFoundException: " + cnfe.toString());
           
        }
       
        return con;
       
    }
}
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
n2alexanAuthor Commented:
Also, my host told me that there's no auto deployment and
each class name has to be defined via the server.xml. I have 23 .class files. What should I do about this?

Thank you for you help.
0
 
objectsCommented:
> each class name has to be defined via the server.xml. I have 23 .class files. What should I do about this?

not sure what they meant.
u sure they didn't mean each *context* would need to be defined?
0
 
n2alexanAuthor Commented:
how do I add the driver jar to my webapp?
0
 
n2alexanAuthor Commented:
For my questions to the host "Did I place my .class files in the correct directory? (WEB-INF/classes)"
the host replied

the host replied with this:
"there's no auto deployment each class name has to be defined via the server.xml under the virtualhost for www.nichefhgs.com"

I thought that it means that each class files must be somehow declared in server.xml.

And I am not sure how to do it?

Many thanks for help
0
 
objectsCommented:
either put them in <tomcat>/common/lib to make them available to all webapps

or put them in <webapp>/WEB-INF/lib
0
 
objectsCommented:
> "there's no auto deployment each class name has to be defined via the server.xml under the virtualhost for www.nichefhgs.com"

i think they meant each *context* has to be defined in server.xml
0
 
n2alexanAuthor Commented:
ok I have created /WEB-INF/lib directory and copied my framework/<class files> there, where framework is my package name (actually I do not have webapp directory on my server and instead I have to put everything in /www/virtual/mja directory.)
0
 
n2alexanAuthor Commented:
But it is not working
0
 
objectsCommented:
just jars should go in WEB-INF/lib
classes goo in WEB-INF/classes


When I say <webapp> I am referring to whatever directory your web application is in, sorry that wasn't clear
0
 
n2alexanAuthor Commented:
I have checked <tomcat>/common/lib directory and it contains mysql-connector-java-3.1.12-bin.jar which is Connector J
0
 
n2alexanAuthor Commented:
And if I understand correctly I do not need to place this file in WEB-INF/lib directory for my Web Application.
0
 
objectsCommented:
>  And if I understand correctly I do not need to place this file in WEB-INF/lib directory for my Web Application.

correct
0
 
n2alexanAuthor Commented:
I have just created simple jsp page (I used the correct login values for db, username and password).

==================

<%@ page import="java.sql.*" %>
<%
String connectionURL = "jdbc:mysql://localhost/somedb?user=someuser;password=somepassword";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>

<html><body>

<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "", "");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM cat");

while (rs.next()) {
out.println(rs.getString("cat_name")+"<br>");
}

rs.close();
%>

</body></html>

==================

which when I call gives me this org.apache.jasper.JasperException: Exception in JSP: /testdb.jsp:13

10:
11: <%
12: Class.forName("com.mysql.jdbc.Driver").newInstance();
13: connection = DriverManager.getConnection(connectionURL, "", "");
14: statement = connection.createStatement();
15: rs = statement.executeQuery("SELECT * FROM category");
16:


Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

javax.servlet.ServletException: Access denied for user ''@'127.0.0.1' (using password: NO)
      org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
      org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
      org.apache.jsp.testdb_jsp._jspService(testdb_jsp.java:74)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Do you have any idea what is going on here?



0
 
n2alexanAuthor Commented:
I use the same credentials to login into phpMyAdmin
0
 
n2alexanAuthor Commented:
The main reason why I used this page to test is that it does not use any java beans.
This will eliminate that problem and just concentrate on connection.

Many many thanks for your help.
0
 
objectsCommented:
looks like you don't have access to mysql.
do you need to provide user/password?
0
 
objectsCommented:
> connection = DriverManager.getConnection(connectionURL, "", "");

supply the user/pass here

connection = DriverManager.getConnection(connectionURL, "someuser", "somepass");
0
 
n2alexanAuthor Commented:
Dear object,

You were right. I do not need anything in server.xml. My host had resolved the issue by changing persmissions for root for 127.

Thank you for your help,
N.
0
 
objectsCommented:
no worries :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.