Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"mastering tomcat development" servlet code problem

Status
Not open for further replies.

phtobias

Technical User
Aug 24, 2003
5
US
I try to follow a servlet example [Chapter_5/SQLSampleServlet.java] in the book "mastering tomcat development" by Peter Harrison/Ian McFarland, but run into a problem.

Your help is greatly appreciated. Many Thanks.

yi-ren

My setup:
-----------------------------------------------------------
Apache Tomcat/4.1.18 and httpd/2.0.47 using connector mod_jk-2.0.43.so

[java version "1.4.2"]
javac -classpath common/lib/servlet.jar webapps/examples/WEB-INF/classes/SQLSampleServlet.java

-----------------------------------------------------------

Here is the error messages:
----------------------------------------------------
javax.servlet.UnavailableException
at SQLSampleServlet.init(SQLSampleServlet.java:22)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:934)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:666)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:432)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:386)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:534)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:530)
at java.lang.Thread.run(Thread.java:534)
----------------------------------------------------


Here is the part of the code that causes the problem:
----------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class SQLSampleServlet extends HttpServlet{
private Connection con;
public void init(ServletConfig config) throws ServletException
{
String driver = config.getInitParameter("database-driver-class");
String jdbcurl = config.getInitParameter("database-url");
String login = config.getInitParameter("database-login");
String password = config.getInitParameter("database-password");
try
{
//error comes from here
Class.forName(driver); // Load the driver
con = DriverManager.getConnection(jdbcurl, login, password);
}
catch (Exception e)
{
throw new UnavailableException(e.getMessage());
}
}

public void doGet(HttpServletRequest request, HttpServletResponse response) {
//This part works OK.
}
}
 
I think the problem is at web.xml. I have to give the <init-param> first.

sorry for the long write.

Post again if I make it work.

yi-ren
 
It is kind of half work, half failure.

I added the <init-param> (see attached) into the web.xml and now it knows the variables driver/jdbcurl/login/passowrd. The problem is that it still said &quot;javax.servlet.UnavailableException: org.gjt.mm.mysql.Driver&quot;.

Can anyone give me a hint how to install org.gjt.mm.mysql.Driver onto the tomcat?

yi-ren

----------------------------------------------------------
Added to web.xml
----------------------------------------------------------
<servlet>
<servlet-name>SQL</servlet-name>
<servlet-class>SQLSampleServlet</servlet-class>
<init-param>
<param-name>persistence.type</param-name>
<param-value>pooled</param-value>
</init-param>
<init-param>
<param-name>database-driver-class</param-name>
<param-value>org.gjt.mm.mysql.Driver</param-value>
<!--param-value>org.postgresql.Driver</param-value-->
</init-param>
<init-param>
<param-name>database-url</param-name>
<param-value>jdbc:mysql://localhost/tomcatbook</param-value>
<!--param-value>jdbc:postgresql://localhost/tomcatbook</param-value-->
</init-param>
<init-param>
<param-name>database-login</param-name>
<param-value>tomcatbook</param-value>
</init-param>
<init-param>
<param-name>database-password</param-name>
<param-value>badpassword</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>SQL</servlet-name>
<url-pattern>/servelt/sql</url-pattern>
</servlet-mapping>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top