|
|
Velocity + Tomcat 5 driving me crazy
|
|
|
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
I've used Velocity templates many times in the past with great success. However, I am trying for the first time to use it with Tomcat (jakarta-tomcat-5.0.19) or WebLogic (8.1) with Velocity 1.4 and am having an odd problem. My servlet is the simple servlet_example1 that comes with Velocity. I installed it as suggested, created the web.xml file, and redeployed. I get this error both in WebLogic and in Tomcat:
Code:
WebLogic: java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet.
Tomcat: 2004-04-21 13:56:09 StandardWrapperValve[VelocityExample]: Allocate
exception for servlet VelocityExamplejavax.servlet.ServletException: Error allocating a
servlet instance
----- Root Cause -----
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServlet
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
As you can see, the message is essentially the same, so it must be something I am doing wrong (or something wrong in the example).
Here is my web.xml:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Velocity Sample</display-name>
<description>
Sample Velocity Template Code
</description>
<servlet>
<servlet-name>VelocityExample</servlet-name>
<servlet-class>VelocityExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VelocityExample</servlet-name>
<url-pattern>/velocity</url-pattern>
</servlet-mapping>
</web-app>
The class file is in WEB-INF/classes/ and is named VelocityExample.class. The java source for that file is:
Code:
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.servlet.VelocityServlet;
public class VelocityExample extends VelocityServlet
{
protected Properties loadConfiguration(ServletConfig config)
throws IOException, FileNotFoundException
{
Properties p = new Properties();
String path = config.getServletContext().getRealPath("/");
if (path == null)
{
System.out
.println(" SampleServlet.loadConfiguration() : unable to "
+ "get the current webapp root. Using '/'. Please fix.");
path = "/";
}
p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
p.setProperty("runtime.log", path + "velocity.log");
return p;
}
public Template handleRequest(HttpServletRequest request,
HttpServletResponse response, Context context)
{
String currentDate = new Date().toString();
context.put("currentDate", currentDate);
Template outty = null;
try
{
outty = getTemplate("velocity_example.vm");
}
catch (ParseErrorException pee)
{
System.out.println("SampleServlet : parse error for template "
+ pee);
}
catch (ResourceNotFoundException rnfe)
{
System.out.println("SampleServlet : template not found " + rnfe);
}
catch (Exception e)
{
System.out.println("Error " + e);
}
return outty;
}
}
Anyone see where the problem is?
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
I'm not sure, I've never used Velocity with Tomcat. Do you have servlet.jar in $CATALINA_HOME/common/lib?
BTW, driving people crazy is what Tomcat does best
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Originally posted by itai195:
I'm not sure, I've never used Velocity with Tomcat. Do you have servlet.jar in $CATALINA_HOME/common/lib?
There is a $CATALINA_HOME/common/lib/servlet-api.jar which contains the requisite class:
Code:
[dshaw@flybook /Library/tomcat/common/lib] jar -tvf servlet-api.jar | grep HttpServlet
7738 Fri Feb 13 21:36:52 EST 2004 javax/servlet/http/HttpServlet.class
The thing is, it happens in WebLogic as well, which uses its own set of jar files for the servlet API. I am wondering if there is a problem with Velocity 1.4 (which is pretty new).
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
I should also mention that a regular servlet that inherits from HttpServlet is working fine in the same installation. So it's not as simple as that Tomcat can't find the class.
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status:
Offline
|
|
Weird, it definitely looks like a classpath issue from the errors though. Where is the velocity-dep.jar file located?
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Originally posted by itai195:
Weird, it definitely looks like a classpath issue from the errors though. Where is the velocity-dep.jar file located?
Inside my app/WEB-INF/lib folder. That's where it is supposed to go according to the standard. I tried it in $CATALINA_HOME/common/lib too but it didn't make a difference.
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|