Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running Servlets on Linux

 
kunal Jaggi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
danny liu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic