Re: Need Help Programmatically Loading Servlets in StandardContext subclass

2005-08-23 Thread Philip Weaver


I give up. MVC baby!

On Aug 21, 2005, at 7:24 PM, Philip Weaver wrote:



How can I programmatically load servlets and servlet mappings in 
Tomcat? I'm using Tomcat 5.0.28.


I'm trying to extend StandardContext to automatically map/assign a 
batch of servlets from a specified jar at startup. I'm having trouble. 
As I try to add these servlets programmatically as the StandardContext 
is starting up, I can't get them to register/load. The servlets loaded 
from the web.xml work fine, but the ones I'm trying to add 
programmatically return null ObjectNames when I query 
context.getServlets(). They are also ignored or not found when I get 
the URL.


Here's the code I'm trying... The most relevant code is in 
addServlet() below - which I'm calling after super.start().


package com.luminera.www;

import java.io.*;
import java.util.*;
import javax.management.*;
import javax.naming.*;
import javax.servlet.*;
import org.apache.catalina.*;
import org.apache.catalina.deploy.*;
import org.apache.catalina.loader.*;
import org.apache.catalina.mbeans.*;
import org.apache.catalina.startup.*;
import org.apache.catalina.util.*;
import org.apache.commons.logging.*;
import org.apache.commons.modeler.*;
import org.apache.naming.*;
import org.apache.naming.resources.*;
import org.apache.tomcat.util.compat.*;

public class StandardContext extends 
org.apache.catalina.core.StandardContext {


public StandardContext() {

super();
}

public synchronized void start() throws LifecycleException {

super.start();

this.loadServlets();

String[] asServlets = this.getServlets();
for (int i = 0; i  asServlets.length; i++) {
System.out.println(known servlet:  + asServlets[i]);
}
}

private void loadServlets() {

String sParameter = this.findParameter(servlets-jar);
String sBasePath = this.getBasePath();
System.out.println(sParameter:  + sParameter);
System.out.println(sBasePath:  + sBasePath);
if ((sParameter != null)  (sBasePath != null)) {
String sJarPath = sBasePath + sParameter;
System.out.println(servlet jar path:  + sJarPath);
ClassLoader classLoader = 
this.getLoader().getClassLoader();
			//HashMap hashMap = ServletUtility.buildServletMap(sJarPath, 
classLoader); // external lib

HashMap hashMap = new HashMap();
			for (Iterator iterator = hashMap.keySet().iterator(); 
iterator.hasNext();) {

String sPattern = (String)iterator.next();
String sClass = (String)hashMap.get(sPattern);
this.addServlet(sPattern, sClass);
}
}
}

private void addServlet(String sPattern, String sClass) {

		System.out.println(Trying to load servlet for url pattern:  + 
sPattern);


Wrapper wrapper = null;
try {
String sName = sClass;
wrapper = this.createWrapper();
wrapper.setName(sName);
wrapper.setServletClass(sClass);
this.addChild(wrapper);
this.addServletMapping(sPattern, sName);
if (wrapper instanceof Lifecycle) {
((Lifecycle)wrapper).start();
}
System.out.println(succeeded);
System.out.println(sPattern:  + sPattern);
System.out.println(sName:  + sName);
} catch (Throwable throwable) {
System.out.println(failed);
Log log = LogFactory.getLog(StandardContext.class);
log.error(Failed to load servlet:  + sClass, 
throwable);
this.removeServletMapping(sPattern);
this.removeChild(wrapper);
}
}

private String getBasePath() {

String docBase = null;
Container container = this;
while (container != null) {
if (container instanceof Host) {
break;
}
container = container.getParent();
}
File file = new File(getDocBase());
if (!file.isAbsolute()) {
if (container == null) {
docBase = (new File(engineBase(), 
getDocBase())).getPath();
} else

Need Help Programmatically Loading Servlets in StandardContext subclass

2005-08-21 Thread Philip Weaver


How can I programmatically load servlets and servlet mappings in 
Tomcat? I'm using Tomcat 5.0.28.


I'm trying to extend StandardContext to automatically map/assign a 
batch of servlets from a specified jar at startup. I'm having trouble. 
As I try to add these servlets programmatically as the StandardContext 
is starting up, I can't get them to register/load. The servlets loaded 
from the web.xml work fine, but the ones I'm trying to add 
programmatically return null ObjectNames when I query 
context.getServlets(). They are also ignored or not found when I get 
the URL.


Here's the code I'm trying... The most relevant code is in addServlet() 
below - which I'm calling after super.start().


package com.luminera.www;

import java.io.*;
import java.util.*;
import javax.management.*;
import javax.naming.*;
import javax.servlet.*;
import org.apache.catalina.*;
import org.apache.catalina.deploy.*;
import org.apache.catalina.loader.*;
import org.apache.catalina.mbeans.*;
import org.apache.catalina.startup.*;
import org.apache.catalina.util.*;
import org.apache.commons.logging.*;
import org.apache.commons.modeler.*;
import org.apache.naming.*;
import org.apache.naming.resources.*;
import org.apache.tomcat.util.compat.*;

public class StandardContext extends 
org.apache.catalina.core.StandardContext {


public StandardContext() {

super();
}

public synchronized void start() throws LifecycleException {

super.start();

this.loadServlets();

String[] asServlets = this.getServlets();
for (int i = 0; i  asServlets.length; i++) {
System.out.println(known servlet:  + asServlets[i]);
}
}

private void loadServlets() {

String sParameter = this.findParameter(servlets-jar);
String sBasePath = this.getBasePath();
System.out.println(sParameter:  + sParameter);
System.out.println(sBasePath:  + sBasePath);
if ((sParameter != null)  (sBasePath != null)) {
String sJarPath = sBasePath + sParameter;
System.out.println(servlet jar path:  + sJarPath);
ClassLoader classLoader = 
this.getLoader().getClassLoader();
			//HashMap hashMap = ServletUtility.buildServletMap(sJarPath, 
classLoader); // external lib

HashMap hashMap = new HashMap();
			for (Iterator iterator = hashMap.keySet().iterator(); 
iterator.hasNext();) {

String sPattern = (String)iterator.next();
String sClass = (String)hashMap.get(sPattern);
this.addServlet(sPattern, sClass);
}
}
}

private void addServlet(String sPattern, String sClass) {

		System.out.println(Trying to load servlet for url pattern:  + 
sPattern);


Wrapper wrapper = null;
try {
String sName = sClass;
wrapper = this.createWrapper();
wrapper.setName(sName);
wrapper.setServletClass(sClass);
this.addChild(wrapper);
this.addServletMapping(sPattern, sName);
if (wrapper instanceof Lifecycle) {
((Lifecycle)wrapper).start();
}
System.out.println(succeeded);
System.out.println(sPattern:  + sPattern);
System.out.println(sName:  + sName);
} catch (Throwable throwable) {
System.out.println(failed);
Log log = LogFactory.getLog(StandardContext.class);
log.error(Failed to load servlet:  + sClass, 
throwable);
this.removeServletMapping(sPattern);
this.removeChild(wrapper);
}
}

private String getBasePath() {

String docBase = null;
Container container = this;
while (container != null) {
if (container instanceof Host) {
break;
}
container = container.getParent();
}
File file = new File(getDocBase());
if (!file.isAbsolute()) {
if (container == null) {
docBase = (new File(engineBase(), 
getDocBase())).getPath();
} else {
String appBase = ((Host)container).getAppBase();
   

Problem: Servlet Mappings RequestDispatcher

2001-07-11 Thread Philip Weaver


Hello -

I'm trying to create servlet mappings for a servlet that I'd like to exist
at the TOP LEVEL of my webapp in Tomcat. You'd think that this task wouldn't
be a pain in the rear but I'm finding it to be so. If you know how to
configure web.xml and a servlet to handle this, please help. I'm about to
scream at the walls.

The entire goal that I'm pursuing is to provide login security for my web
app.

I know how to create servlet mappings in web.xml and I basically know how to
use RequestDispatcher. However, I'm running into either of these two
problems.

A. I end up with an entry servlet that is recursively called whenever I
call RequestDispatch.forward() based on the following web.xml setup. I
understand why this occurs, but how do I get around this and still have all
requests be handled by the entry mapping.

servlet
servlet-nameentry/servlet-name
servlet-classEntryServlet/servlet-class
/servlet
servlet-mapping
servlet-nameentry/servlet-name
url-pattern/*/url-pattern
/servlet-mapping

B. The other problem I encounter is that sometimes when I request a
page, is that page is not found (404). I'm begining to think that it's
because the forward() method is relative to the requested URI which is
actually a freaking filename (?). Right? This is driving me nuts.

Can anyone point me to an example of a working setup that handles all
requests sent to the entire webapp (root level)? I had this working before
in Resin but can't get that same setup to work in Tomcat.

Thanks,
Philip




TagLib Class Resolution Problem (TomCat 3.2.1)

2001-06-22 Thread Philip Weaver


Whenever I use my taglib from within the subdirectory pages, for example,
jasper complains that Class pages.SpecialConnectionTag not found in type
declaration. So, it appears to find the taglib but thinks that it's part of
a package? (which it's not)

I can work around this by keeping everything in one directory but how do I
solve this? I don't have the problem using the same pages in Resin so this
could be a configuration problem with Tomcat. Please help.

Thanks,
Philip



org.apache.jasper.JasperException: Unable to compile class for
JSP/Organize/Tomcat/jakarta-tomcat-3.2.1/work/localhost_8080%2Fphilmaker/_00
02fpages_0002findex_0002ejspindex_jsp_0.java:71: Class
pages.SpecialConnectionTag not found in type declaration.
SpecialConnectionTag _jspx_th_pjw_specialconnection_0 = new
SpecialConnectionTag();
^
/Organize/Tomcat/jakarta-tomcat-3.2.1/work/localhost_8080%2Fphilmaker/_0002f
pages_0002findex_0002ejspindex_jsp_0.java:71: Class
pages.SpecialConnectionTag not found in type declaration.
SpecialConnectionTag _jspx_th_pjw_specialconnection_0 = new
SpecialConnectionTag();
   
^
2 errors

at org.apache.jasper.compiler.Compiler.compile(Compiled Code)
...

.




JSP Tag XML Syntax Problem (jsp:include, etc.)

2001-06-22 Thread Philip Weaver


I'm using Tomcat 3.2.1 and am unable to use the XML syntax of certain JSP
tags: 

jsp:include page=page.jsp/ [Error: Invalid jsp:include tag]
jsp:scriptlet/jsp:scriptlet

The same tags work in Resin. What could I be doing wrong?

Thanks,
Philip

org.apache.jasper.compiler.CompileException:
/Organize/Tomcat/jakarta-tomcat-3.2.1/webapps/philmaker//index.jsp(27,2)
Invalid jsp:include tag
at 
org.apache.jasper.compiler.IncludeGenerator.(IncludeGenerator.java:95)
...

.




Re: TagLib Class Resolution Problem (TomCat 3.2.1)

2001-06-22 Thread Philip Weaver

I figured this out. I could not access my taglib from different
sub-directories because my taglib was not in a package.

Philip

on 6/22/01 3:30 AM, Philip Weaver at [EMAIL PROTECTED] wrote:

 
 Whenever I use my taglib from within the subdirectory pages, for example,
 jasper complains that Class pages.SpecialConnectionTag not found in type
 declaration. So, it appears to find the taglib but thinks that it's part of
 a package? (which it's not)
 
 I can work around this by keeping everything in one directory but how do I
 solve this? I don't have the problem using the same pages in Resin so this
 could be a configuration problem with Tomcat. Please help.
 
 Thanks,
 Philip