aspose file tools*
The moose likes Servlets and the fly likes Running Servlets on Linux Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Running Servlets on Linux" Watch "Running Servlets on Linux" New topic
Author

Running Servlets on Linux

kunal Jaggi
Greenhorn

Joined: Sep 16, 2003
Posts: 17
I’m using the following Open Source softwares to build a J2EE Web Application:

Linux-- OS (Mandrake Linux 9.1)
Apache-- Web Server (Tomcat 5)
Tomcat-- Servlet Container
MySQL-- Database

I have the following environment variables correctly in the /etc/profile file:

PATH� points to my J2SDK installation
CLASSPATH � set to compile servlets and path to MYSQL
CATALINA_HOME � points to Tomcat installation
JAVA_HOME� points to JDK installation

I can run servlets and JSP.

The problem that I’m facing is that although I can access the MYSQL DB from a standalone application but not from a servlet.

Here is the servlet code snippet:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class TestServlet extends HttpServlet{
Connection con = null;
Statement stmt=null;
ResultSet rs=null;

public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("Inside Test Servlet..");
pw.println("The date is "+new java.util.Date());

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
pw.println("the driver class is loaded");

con = DriverManager.getConnection("jdbc:mysql:///sun","root","");
pw.println("connection is established");

if(con!=null)
System.out.println("Successfully connected to MySQL server using TCP/IP...");
stmt=con.createStatement();
rs=stmt.executeQuery("Select * from employee");

while(rs.next()){
pw.print(rs.getString(1)+"\t");
pw.println(rs.getString(2));
}


} catch(Exception e) {
pw.println("Error Message -->"+e.getMessage());
pw.println("Error -->"+e);

//Error Message -->com.mysql.jdbc.Driver
}

pw.close();

}
}
Output on the web browser:

Inside Test Servlet.. The date is Fri Nov 05 00:58:46 EST 2004
Error Message -->com.mysql.jdbc.Driver
Error -->java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I’m using mysql-connector-java-3.0.15-ga. But, ironically I can query the database from a standalone Java Application.

In case you have any suggestion pl. post a reply.

Regards,
Kunal Jaggi
SCJP
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Put the mysql jar in the <context>/WEB-INF/lib directory in Tomcat.
Application servers typically ignore the classpath since this allows them to create separate running environments for each of the applications thay are running. Any jars placed in the WEB-INF/lib directory automatically get loaded and made available to your application context.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
the answer is contained in your question.
You need to add the JDBC driver to the lib directory of your web application (I guess it's a jar file?).

Web applications have access ONLY to what's in their own library (WEB-INF/classes and WEB-INF/lib) and to default servlet functionality (servlet.jar or j2ee.jar) and nothing else.
They're fully self-contained.


42
danny liu
Ranch Hand

Joined: Jan 22, 2004
Posts: 185
If you want that mysql driver accessed by all applications,
You may copy it to [tomcathome]/common/lib.

Hope it helps.

Dan
kunal Jaggi
Greenhorn

Joined: Sep 16, 2003
Posts: 17
Hi!!
It's working fine now...thanks.....

I want to change my public display name, how do I go about doing that??

Regards,
Kunal Jaggi
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093


Change the "path" property to change the path via which your webapp can be approached.

To change the name displayed in the web application management application, change the web.xml for the webapp:

[ November 08, 2004: Message edited by: Jeroen Wenting ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Unless you menat your JavaRanch display name, which can be changed here. To get there in future, to the right of the Moose Head is a 'My Profile' link, and you 'View/Edit Profile' there.

Dave.
 
Consider Paul's rocket mass heater.
 
subject: Running Servlets on Linux