Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

getting the ServletContext in CF

Guest
May 30, 2008 May 30, 2008
I am trying basically to interact with a Java class file installed under WEB-INF/classes. I want to pass the ServletContext for cf to my new class when I instantiate an object from it.

What I wanted to do was to create an instance of my class through <cfobject> and then use init(getPageContext.getServletContext).

So I used:

<cfscript>
cfServlet = getPageContext().getServletContext();
url_mappings = CreateObject("java", "com.dev.myFilter.UrlMapper");
</cfscript>

Then I went to initialize this new object and pass the ServletContext into it:

url_mappings.init(cfServlet).

This returns an error because (as I soon discovered) the object "cfServlet" gotten from the first line is of class "coldfusion.runtime.ServletContextWrapper", and my Constructor method for my class is designed to handle a ServletContext object.

I also tried making a constructor method that takes argument of time coldfusion.runtime.ServletContextWrapper, but I still got an error in CF that there are no constructor methods designed to handle argument of type coldfusion.runtime.ServletContextWrapper, which is wierd!

In my class file I have these 2 constructor methods as a test, so that I am explicitly handling ServletContextWrappers:

UrlMapper(ServletContextWrapper servletcontextwrapper) {
this.ReadFile();
}

// Constructor w/out filename arg
UrlMapper(ServletContext servletcontext) {
UrlMapper.servletcontext = servletcontext;
this.ReadFile();
}

But this still throws an error in my CFM template!

"Unable to find a constructor for class com.webworld.filter.UrlMapper that accepts parameters of type ( coldfusion.runtime.ServletContextWrapper ). "

Is there any way to get around this, either in CF by casting this ServletContextWrapper object to a ServletContext, or by simply getting the actual ServletContext?

--- Thanks
624
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 14, 2008 Jun 14, 2008
LATEST
I am unclear on your ultimate goal. However, you should have no problem passing in the CF ServletContext to your class, since the coldfusion.runtime.ServletContextWrapper class implements the javax.servlet.ServletContext interface. Passing in the servletContext as you described works fine for me with CF8

<cfscript>
cfServlet = getPageContext().getServletContext();
url_mappings = CreateObject("java", "com.dev.myFilter.UrlMapper");
url_mappings.init(cfServlet);
</cfscript>

import javax.servlet.ServletContext;


public class UrlMapper {
//... declare variables

public UrlMapper(ServletContext servletcontext) {
//... etc
}

}

I would verify the class constructors are accessible (ie public). You may also need to restart CF, to ensure sure it is using the correct version of your class. For that reason, I usually add a version number to my classes. It eliminates the guesswork.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources