File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes JSP to Servlet Mapping Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "JSP to Servlet Mapping Problem" Watch "JSP to Servlet Mapping Problem" New topic

JSP to Servlet Mapping Problem

Gary Nitzberg

Joined: Aug 15, 2011
Posts: 4
In the server.xml file I have a virtual host defined as,

<Host name="" appBase="/home/webapps/servlets">
<Context path="/" docBase="/home/user/public_html"></Context>

In the docBase is an index.jsp file, "/home/user/public_html/index.jsp"
In the appBase is an index.jsp file, "/home/webapps/servlets/index.jsp"
Both index.jsp file are identical.

In the index.jsp file I attempt to use a servlet;

<%@ page import="com.domain.myServlet" %>
<jsp:useBean id="myId" scope="page" class="com.domain.myServlet" />

The servlet is located in, "/home/webapps/servlets/WEB-INF/classes/com/domain/myServlet"

If I execute I get the following error,
"The value for the useBean class attribute com.domain.myServlet is invalid"

If I change the context in the server.xml file to the following it works.
<Context path="" docBase="."></Context>
This is because it executes the index.jsp file in the appBase directory of "/home/webapps/servlets/index.jsp"

It appears the code in the index.jsp file in "/home/user/public_html" can not find the servlets in the appBase.
So how do I tell code in the index.jsp in the public_html directory how to find the servlets in appBase.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17282

That's a funny way to use a servlet. Strictly speaking, servlets are executable objects invoked directly from URL requests and not something that you'd include into a JSP with a "useBean" tag. Also, it's strongly discouraged these days to include Context definitions in the Tomcat server.xml file. They should be separate files in the TOMCAT_HOME/conf/Catalina/localhost directory or in the WAR's META-INF/context.xml file.

An IDE is no substitute for an Intelligent Developer.
Gary Nitzberg

Joined: Aug 15, 2011
Posts: 4
I know Tomcat has its own way to map classes.
I have dozens of applications that were developed as far back as 10 years ago using Resin.
The Resin web.xml file allows a classpath configuration,
<classpath id='/home/webapps/servlets/WEB-INF/classes' source='/home/webapps/sources' />
that lets you set the classpath to anywhere you wanted on a host basis.

I have seen a number of posts where porting from Resin to Tomcat becomes difficult due to this exclusion. After all, a JSP page is a servlet that should be able to use a class where-ever it is located.

I guess this is all based on packaging an entire application in a WAR file instead of having pieces of the application all over the place.

I just wish I did not have to reconfigure the all the Resin apps to conform to Tomcat!

I know about the context statement in the TOMCAT_HOME/conf/Catalina/localhost.
I was just trying to make the example simple.

I agree. Here's the link:
subject: JSP to Servlet Mapping Problem
It's not a secret anymore!