You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Levent Gündogdu <le...@feature-it.com> on 2001/08/03 15:08:58 UTC

Q: Wrapping ServletResponse for background processing

Hi everyone,

I'm curious if anyone has an idea for the following:

I have written a Servlet which is able to do background calculations
which take some time. I don't want the user to wait for the request to
complete, so I decided to start a thread and output a message to the
user so that he may continue to work with the
application until the calculations have been made. So far so good.

The output of the calculation is somewhat complex. In the first approach
the thread created a StringBuffer, put everything into that buffer and,
after completion, that StringBuffer was sent to the user. That worked
fine. But: I want the thread to be able to include JSPs in order to
format the output. For that, I need a ServletRequest and ServletResponse
object. I decided to implement own versions of ServletRequest and
ServletResponse, so that I could overwrite getWriter() and
getOutputStream() in order to put the output into my own buffer. But,
when calling RequestDispatcher.include(), I get:

IllegalArgumentException: Response is not a
javax.servlet.ServletResponseWrapper.

thrown in ApplicationResponse.calculateParent().

Why has the ServletReponse to be a ServletResponseWrapper?

I tried wrapping my ServletResponse with a ServletResponseWrapper but
still the same exception occurs.

I thought that defining interfaces (like ServletResponse) is good for
abstracting things and allowing developers to replace functionality. But
in this case abstraction gets meaningless.

Do you have any idea how to solve that?

Thanks in advance, everyone!

Bye,
 Levo.

BTW: I'm using tomcat 4.0 b5.


--
Feature-IT Information Technology

eMail: info@feature-it.de

http://www.feature-it.de  (Deutsch)
http://www.feature-it.com (English)

Phone: +49 511 95981-0
Fax:   +49 511 95981-40



Re: Please help: IllegalArgumentException: Response is not a ServletResponseWrapper

Posted by Levent Gündogdu <le...@feature-it.com>.
Thanks, Craig.

> > I thought that defining interfaces (like ServletResponse) is good for
> > abstracting things and allowing developers to replace functionality. But in
> > this case abstraction gets meaningless.
> >
>
> The abstraction would be fine ... if your calling class did it's part of
> the bargain.  But that means you have to act (from the perspective of the
> JSP page you're calling) like you are Tomcat.

Now I got the point, thanks.

> > Do you have any idea how to solve that?
> >
>
> A completely different strategy would be to have your extra thread do its
> computations and store the data to create the results into a session
> attribute.  Then, when your user asked for the page to display the
> results, the calculations would have already been completed (or, if the
> attribute isn't there, you can display a "still working on it ..." page),
> the response can be displayed in the usual way.

Okay, I'll have to do it that way then.

Thanks a lot for your time.

Bye,
 Levo.





--
Feature-IT Information Technology

eMail: info@feature-it.de

http://www.feature-it.de  (Deutsch)
http://www.feature-it.com (English)

Phone: +49 511 95981-0
Fax:   +49 511 95981-40



Re: Please help: IllegalArgumentException: Response is not a ServletResponseWrapper

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sat, 4 Aug 2001, Levent Gündogdu wrote:

> Hi everyone,
> 
> I would be so happy if someone could help me with the following problem:
> 
> I have written a Servlet which is able to do background calculations which
> take some time. I don't want the user to wait for the request to complete,
> so I decided to start a thread and output a message to the user so that he
> may continue to work with the
> application until the calculations have been made. So far so good.
> 
> The output of the calculation is somewhat complex. In the first approach the
> thread created a StringBuffer, put everything into that buffer and, after
> completion, that StringBuffer was sent to the user. That worked fine, but:
> 
> I want the thread to be able to include JSPs in order to format the output.
> For including some URI, I need a ServletRequest and ServletResponse object
> which are passed to RequestDispatcher.include(). I decided to implement own
> versions of ServletRequest and ServletResponse, so that I could overwrite
> getWriter() and getOutputStream() in order to put the output into my own
> buffer. But, when calling RequestDispatcher.include(), I get:
> 
> IllegalArgumentException: Response is not a
> javax.servlet.ServletResponseWrapper.
> 
> thrown in ApplicationResponse.calculateParent().
> 
> Why has the ServletReponse to be a ServletResponseWrapper?
> 

Because it's required by the spec.

The deeper issue is that your extra thread is "pretending" (from the
point of view of the JSP pages you're trying to use) to be a servlet
container, but it is only doing part of the job.

> I tried wrapping my ServletResponse with a ServletResponseWrapper but still
> the same exception occurs.
> 
> I thought that defining interfaces (like ServletResponse) is good for
> abstracting things and allowing developers to replace functionality. But in
> this case abstraction gets meaningless.
> 

The abstraction would be fine ... if your calling class did it's part of
the bargain.  But that means you have to act (from the perspective of the
JSP page you're calling) like you are Tomcat.

An additional problem you will run into, even if you solve this one, is
that the request object cannot be shared across threads either.


> Do you have any idea how to solve that?
> 

A completely different strategy would be to have your extra thread do its
computations and store the data to create the results into a session
attribute.  Then, when your user asked for the page to display the
results, the calculations would have already been completed (or, if the
attribute isn't there, you can display a "still working on it ..." page),
the response can be displayed in the usual way.

> Thanks in advance, everyone!
> 
> Bye,
>  Levo.
> 
> BTW: I'm using tomcat 4.0 b5.
> 
> 

Craig McClanahan


Please help: IllegalArgumentException: Response is not a ServletResponseWrapper

Posted by Levent Gündogdu <le...@feature-it.com>.
Hi everyone,

I would be so happy if someone could help me with the following problem:

I have written a Servlet which is able to do background calculations which
take some time. I don't want the user to wait for the request to complete,
so I decided to start a thread and output a message to the user so that he
may continue to work with the
application until the calculations have been made. So far so good.

The output of the calculation is somewhat complex. In the first approach the
thread created a StringBuffer, put everything into that buffer and, after
completion, that StringBuffer was sent to the user. That worked fine, but:

I want the thread to be able to include JSPs in order to format the output.
For including some URI, I need a ServletRequest and ServletResponse object
which are passed to RequestDispatcher.include(). I decided to implement own
versions of ServletRequest and ServletResponse, so that I could overwrite
getWriter() and getOutputStream() in order to put the output into my own
buffer. But, when calling RequestDispatcher.include(), I get:

IllegalArgumentException: Response is not a
javax.servlet.ServletResponseWrapper.

thrown in ApplicationResponse.calculateParent().

Why has the ServletReponse to be a ServletResponseWrapper?

I tried wrapping my ServletResponse with a ServletResponseWrapper but still
the same exception occurs.

I thought that defining interfaces (like ServletResponse) is good for
abstracting things and allowing developers to replace functionality. But in
this case abstraction gets meaningless.

Do you have any idea how to solve that?

Thanks in advance, everyone!

Bye,
 Levo.

BTW: I'm using tomcat 4.0 b5.


--
Feature-IT Information Technology

eMail: info@feature-it.de

http://www.feature-it.de  (Deutsch)
http://www.feature-it.com (English)

Phone: +49 511 95981-0
Fax:   +49 511 95981-40