Initializing servlets with references to existing objects

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Initializing servlets with references to existing objects

Benjamin Podszun
Hi there.

In my project I have a default/delegator servlet that needs access to  
a (reference to an) object. I currently start the server like this:

WebApplicationContext webAppContext;
_jetty = new Server();

// logging config here..

webAppContext = new WebApplicationContext();
webAppContext = _jetty.addWebApplication("/", config.getServerRoot
().getAbsolutePath());
WebApplicationHandler handler = new WebApplicationHandler();
ServletHolder delegator = handler.addServlet("/*",  
DelegatorServlet.class.getName());

// Does't work
// delegator.getServletContext().setAttribute("MyObject", someObject);

FilterHolder filterHolder = handler.defineFilter("DBFilter",  
DBFilter.class.getName());
handler.addFilterHolder(filterHolder);
webAppContext.addHandler(handler);
_jetty.start();

I added the commented part of my last try: I thought I maybe could  
add my reference to the ServletContext, but that ends up with a NPE  
although the object is not null when I setAttribute() it..

Any hints how to do that? I don't want the servlet to access the rest  
of the application via static methods.

Thanks in advance,
Ben


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Jetty-support mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-support
Reply | Threaded
Open this post in threaded view
|

Re: Initializing servlets with references to existing objects

Martin Roos
stacktrace from the exception please :)

martin


Benjamin Podszun wrote:
> Hi there.
>
> In my project I have a default/delegator servlet that needs access to  a
> (reference to an) object. I currently start the server like this:


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Jetty-support mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-support
Reply | Threaded
Open this post in threaded view
|

Re: Initializing servlets with references to existing objects

Benjamin Podszun

On 29.11.2005, at 15:17, Martin Roos wrote:

> stacktrace from the exception please :)
>


I get this:

SEVERE: null
java.lang.NullPointerException
         at  
org.mortbay.jetty.servlet.ServletHandler.getContextAttribute
(ServletHandler.java:933)
         at  
org.mortbay.jetty.servlet.WebApplicationHandler.setContextAttribute
(WebApplicationHandler.java:612)
         at org.mortbay.jetty.servlet.ServletHandler
$Context.setAttribute(ServletHandler.java:1210)

It only happens if I try to do a setAttribute(). I checked that  
delegator and delegator.getServletContext() don't return null. If I  
comment this line I get no Exception (but obviously nothing in my  
servlet either).

ServletHolder delegator = handler.addServlet("/*",  
DelegatorServlet.class.getName());
delegator.getServletContext().setAttribute("foo", "bar");


Am I doing something terribly stupid here?

Thanks in advance,
Ben


> Benjamin Podszun wrote:
>> Hi there.
>> In my project I have a default/delegator servlet that needs access  
>> to  a (reference to an) object. I currently start the server like  
>> this:
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through  
> log files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD  
> SPLUNK!
> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
> _______________________________________________
> Jetty-support mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jetty-support



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Jetty-support mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-support
Reply | Threaded
Open this post in threaded view
|

Re: Initializing servlets with references to existing objects

Greg Wilkins-5
In reply to this post by Benjamin Podszun

Benjamin,

the problem is that the context attributes are a merge of the ones set each
start cycle from web.xml and by the app itself   PLUS any configured attributes
set before the webapp is started.

You are using  getServletContext().setAttribute  before the webapp is started
and hence you get the NPE (OK crappy error!)

You can just set your attributes directly on the webApplicationContext
and they will be available as context attributes to your webapp.


Benjamin Podszun wrote:

> Hi there.
>
> In my project I have a default/delegator servlet that needs access to  a
> (reference to an) object. I currently start the server like this:
>
> WebApplicationContext webAppContext;
> _jetty = new Server();
>
> // logging config here..
>
> webAppContext = new WebApplicationContext();
> webAppContext = _jetty.addWebApplication("/", config.getServerRoot
> ().getAbsolutePath());
> WebApplicationHandler handler = new WebApplicationHandler();
> ServletHolder delegator = handler.addServlet("/*",
> DelegatorServlet.class.getName());
>
> // Does't work
> // delegator.getServletContext().setAttribute("MyObject", someObject);
>
> FilterHolder filterHolder = handler.defineFilter("DBFilter",
> DBFilter.class.getName());
> handler.addFilterHolder(filterHolder);
> webAppContext.addHandler(handler);
> _jetty.start();
>
> I added the commented part of my last try: I thought I maybe could  add
> my reference to the ServletContext, but that ends up with a NPE
> although the object is not null when I setAttribute() it..
>
> Any hints how to do that? I don't want the servlet to access the rest
> of the application via static methods.
>
> Thanks in advance,
> Ben
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
> files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
> _______________________________________________
> Jetty-support mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jetty-support
>



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Jetty-support mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-support
Reply | Threaded
Open this post in threaded view
|

Re: Re: Initializing servlets with references to existing objects

Benjamin Podszun
Hi Greg.
First of all: Thanks for your answer.

On 02.12.2005, at 23:03, Greg Wilkins wrote:
> the problem is that the context attributes are a merge of the ones  
> set each
> start cycle from web.xml and by the app itself   PLUS any  
> configured attributes
> set before the webapp is started.

Sounds exactly like I want it to be.

> You are using  getServletContext().setAttribute  before the webapp  
> is started
> and hence you get the NPE (OK crappy error!)

Makes sense - I understand that the ServletContext doesn't exist  
before I actually start the server.

> You can just set your attributes directly on the webApplicationContext
> and they will be available as context attributes to your webapp.

I'm not sure if I understood this one. I tried

WebApplicationHandler handler = new WebApplicationHandler();
handler.addServlet("/*", DelegatorServlet.class.getName());
handler.setContextAttribute("foo", "bar");

which unfortunately results in exactly the same problem/error. Unless  
I comment this entry I get

Dec 5, 2005 2:17:48 AM myclass start
SEVERE: null
java.lang.NullPointerException
         at  
org.mortbay.jetty.servlet.ServletHandler.getContextAttribute
(ServletHandler.java:934)
         at  
org.mortbay.jetty.servlet.WebApplicationHandler.setContextAttribute
(WebApplicationHandler.java:585)
         at myclass.start(Unknown Source)
         at myclass.main(Unknown Source)

I tried as well

WebApplicationHandler handler = new WebApplicationHandler();
ServletHolder delegator = handler.addServlet("/*",  
DelegatorServlet.class.getName());
delegator.setInitParameter("foo", "bar");

which works fine - but only for String(s) and not for Objects/
references..

Did I missunderstand what you suggested? Or am I doing something  
stupid anyway? I just want to put some properties (Objects)  
programmatically into the servlet context before it is initialized..

Thanks in advance,
Ben

>
> Benjamin Podszun wrote:
>> Hi there.
>>
>> In my project I have a default/delegator servlet that needs access  
>> to  a
>> (reference to an) object. I currently start the server like this:
>>
>> WebApplicationContext webAppContext;
>> _jetty = new Server();
>>
>> // logging config here..
>>
>> webAppContext = new WebApplicationContext();
>> webAppContext = _jetty.addWebApplication("/", config.getServerRoot
>> ().getAbsolutePath());
>> WebApplicationHandler handler = new WebApplicationHandler();
>> ServletHolder delegator = handler.addServlet("/*",
>> DelegatorServlet.class.getName());
>>
>> // Does't work
>> // delegator.getServletContext().setAttribute("MyObject",  
>> someObject);
>>
>> FilterHolder filterHolder = handler.defineFilter("DBFilter",
>> DBFilter.class.getName());
>> handler.addFilterHolder(filterHolder);
>> webAppContext.addHandler(handler);
>> _jetty.start();
>>
>> I added the commented part of my last try: I thought I maybe  
>> could  add
>> my reference to the ServletContext, but that ends up with a NPE
>> although the object is not null when I setAttribute() it..
>>
>> Any hints how to do that? I don't want the servlet to access the rest
>> of the application via static methods.
>>
>> Thanks in advance,
>> Ben



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Jetty-support mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-support
Reply | Threaded
Open this post in threaded view
|

Re: Initializing servlets with references to existing objects

Greg Wilkins-5

WebapplicationContext - not webApplicationHandler!

WebApplicationContext is a HttpContext

HttpContext.setAttribute is what you want!

cheers



Benjamin Podszun wrote:

> Hi Greg.
> First of all: Thanks for your answer.
>
> On 02.12.2005, at 23:03, Greg Wilkins wrote:
>
>> the problem is that the context attributes are a merge of the ones
>> set each
>> start cycle from web.xml and by the app itself   PLUS any  configured
>> attributes
>> set before the webapp is started.
>
>
> Sounds exactly like I want it to be.
>
>> You are using  getServletContext().setAttribute  before the webapp  is
>> started
>> and hence you get the NPE (OK crappy error!)
>
>
> Makes sense - I understand that the ServletContext doesn't exist  before
> I actually start the server.
>
>> You can just set your attributes directly on the webApplicationContext
>> and they will be available as context attributes to your webapp.
>
>
> I'm not sure if I understood this one. I tried
>
> WebApplicationHandler handler = new WebApplicationHandler();
> handler.addServlet("/*", DelegatorServlet.class.getName());
> handler.setContextAttribute("foo", "bar");
>
> which unfortunately results in exactly the same problem/error. Unless  I
> comment this entry I get
>
> Dec 5, 2005 2:17:48 AM myclass start
> SEVERE: null
> java.lang.NullPointerException
>         at  org.mortbay.jetty.servlet.ServletHandler.getContextAttribute
> (ServletHandler.java:934)
>         at
> org.mortbay.jetty.servlet.WebApplicationHandler.setContextAttribute
> (WebApplicationHandler.java:585)
>         at myclass.start(Unknown Source)
>         at myclass.main(Unknown Source)
>
> I tried as well
>
> WebApplicationHandler handler = new WebApplicationHandler();
> ServletHolder delegator = handler.addServlet("/*",
> DelegatorServlet.class.getName());
> delegator.setInitParameter("foo", "bar");
>
> which works fine - but only for String(s) and not for Objects/ references..
>
> Did I missunderstand what you suggested? Or am I doing something  stupid
> anyway? I just want to put some properties (Objects)  programmatically
> into the servlet context before it is initialized..
>
> Thanks in advance,
> Ben
>
>>
>> Benjamin Podszun wrote:
>>
>>> Hi there.
>>>
>>> In my project I have a default/delegator servlet that needs access
>>> to  a
>>> (reference to an) object. I currently start the server like this:
>>>
>>> WebApplicationContext webAppContext;
>>> _jetty = new Server();
>>>
>>> // logging config here..
>>>
>>> webAppContext = new WebApplicationContext();
>>> webAppContext = _jetty.addWebApplication("/", config.getServerRoot
>>> ().getAbsolutePath());
>>> WebApplicationHandler handler = new WebApplicationHandler();
>>> ServletHolder delegator = handler.addServlet("/*",
>>> DelegatorServlet.class.getName());
>>>
>>> // Does't work
>>> // delegator.getServletContext().setAttribute("MyObject",  someObject);
>>>
>>> FilterHolder filterHolder = handler.defineFilter("DBFilter",
>>> DBFilter.class.getName());
>>> handler.addFilterHolder(filterHolder);
>>> webAppContext.addHandler(handler);
>>> _jetty.start();
>>>
>>> I added the commented part of my last try: I thought I maybe  could  add
>>> my reference to the ServletContext, but that ends up with a NPE
>>> although the object is not null when I setAttribute() it..
>>>
>>> Any hints how to do that? I don't want the servlet to access the rest
>>> of the application via static methods.
>>>
>>> Thanks in advance,
>>> Ben
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc. Do you grep through log
> files
> for problems?  Stop!  Download the new AJAX search engine that makes
> searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
> http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
> _______________________________________________
> Jetty-support mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jetty-support
>



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Jetty-support mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jetty-support