Copying an HttpServletRequest

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Copying an HttpServletRequest

cristi-2
Hello all

Is there any way to make a copy of an object implementing
the HttpServletRequest interface ?

I have thought that maybe using HttpServletRequestWrapper could
solve my problem but I'm not sure since the API does not specify
if a new copy of the object sent to the constructor is created
or not.

Thx
Cristi



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Copying an HttpServletRequest

Peter Crowther
> From: cristi [mailto:[hidden email]]
> Is there any way to make a copy of an object implementing
> the HttpServletRequest interface ?

Alter the Tomcat codebase to implement that copy facility, remembering
to check through the codebase for pieces that would break.  You would
then be maintaining your own version of Tomcat.  However, see below for
why this is not a good idea.

> I have thought that maybe using HttpServletRequestWrapper could
> solve my problem but I'm not sure since the API does not specify
> if a new copy of the object sent to the constructor is created
> or not.

It doesn't, and this probably wouldn't help you even if it did.  If the
object was a shallow copy, then many of its dependent objects would be
shared with the Request from which it was copied; this means that you
would probably lose some context when the original Request was recycled.
If the object was a deep copy, then it would have its own copy of all of
the relevant fields - its own session and so on.  So you would have to
experiment in order to copy exactly the correct fields to exactly the
correct depth - and then the next version of Tomcat might change
something that caused your approach to fail, so you would almost
certainly have to pick a version and stick with it.


Can you tell us what you are trying to achieve?  There is almost
certainly an alternative way of getting to the same end without having
to copy requests but, until you give us a bit more detail, we can't help
you.

                - Peter

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Copying an HttpServletRequest

cristi-2
> Can you tell us what you are trying to achieve?  There is almost
> certainly an alternative way of getting to the same end without having
> to copy requests but, until you give us a bit more detail, we can't help
> you.

We have  a software project up  an running but  some modifications are
required.

The  requirement  of  making  a  copy  of  the  HttpServletRequest  is
generated  by the fact  that the  former programmers  have used  (in a
natural manner):

1) the HttpServletRequest.setAttributes()  to  send data  to the  jsp
    pages  creating  the response.  ( These    data   is  the  servlet
    computation result).

2) within  the jsp  pages creating the  result various methods  of the
    HttpServletRequest (other than HttpServletRequest.getAttributes() )
    object are used to get information from the request object.

Now there  is the requirement that  in the second request  sent to the
servlet we  have to  use the computation  result of the  first request
(that   is    those   data   that    you   could   find    using   the
HttpServletRequest.getAttributes() applied to  the request object sent
in the first request ) to create the final HTML page.

Let's supose now  that in the second request we have  a way of finding
the computation  result of  the first request.  This is not  enough to
satisfy the second request because in order to render the HTML page of
the second request  we also need other information  which can be found
in   the    request   object    sent   during   the    first   request
(e.g. HttpServletRequest.getParameter() );

I hope that I was explicit enough.

Final word: We  knew it from the start that  a solution exists (saving
             and using all  the data that we need between the  requests
             but  this  is  not  an  easy  task  now  since  we have to
             analyze - and  to write - a lot of code),  but  since  all
             this  data can be found  in the request object sent to the
             servlet with the  first  request we thought  that making a
             copy of  the request object should  be very easy (and very
             natural at the same time).

Thx
C r i s t i   Z o i c a s.


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Copying an HttpServletRequest

Peddireddy Srikanth
I got a doubt .
is your problem is not solved by  putting the data computed by first
JSP in the session object ??
or putting all the required data in a bean (which is created by
<jsp:useBean> in first jsp) with session scope and accessing that bean
in second jsp ??
with out seeing the entirety of your problem I can say that copying
HttpRequest attributes into a bean (which in turn may store attributes
and values in a hashmap) and setting it in Session and reusing it in
second jsp should not take much time.

O n 7/15/05, cristi <[hidden email]> wrote:

> > Can you tell us what you are trying to achieve?  There is almost
> > certainly an alternative way of getting to the same end without having
> > to copy requests but, until you give us a bit more detail, we can't help
> > you.
>
> We have  a software project up  an running but  some modifications are
> required.
>
> The  requirement  of  making  a  copy  of  the  HttpServletRequest  is
> generated  by the fact  that the  former programmers  have used  (in a
> natural manner):
>
> 1) the HttpServletRequest.setAttributes()  to  send data  to the  jsp
>    pages  creating  the response.  ( These    data   is  the  servlet
>    computation result).
>
> 2) within  the jsp  pages creating the  result various methods  of the
>    HttpServletRequest (other than HttpServletRequest.getAttributes() )
>    object are used to get information from the request object.
>
> Now there  is the requirement that  in the second request  sent to the
> servlet we  have to  use the computation  result of the  first request
> (that   is    those   data   that    you   could   find    using   the
> HttpServletRequest.getAttributes() applied to  the request object sent
> in the first request ) to create the final HTML page.
>
> Let's supose now  that in the second request we have  a way of finding
> the computation  result of  the first request.  This is not  enough to
> satisfy the second request because in order to render the HTML page of
> the second request  we also need other information  which can be found
> in   the    request   object    sent   during   the    first   request
> (e.g. HttpServletRequest.getParameter() );
>
> I hope that I was explicit enough.
>
> Final word: We  knew it from the start that  a solution exists (saving
>             and using all  the data that we need between the  requests
>             but  this  is  not  an  easy  task  now  since  we have to
>             analyze - and  to write - a lot of code),  but  since  all
>             this  data can be found  in the request object sent to the
>             servlet with the  first  request we thought  that making a
>             copy of  the request object should  be very easy (and very
>             natural at the same time).
>
> Thx
> C r i s t i   Z o i c a s.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Copying an HttpServletRequest

cristi-2
> I got a doubt .
> is your problem is not solved by  putting the data computed by first
> JSP in the session object ??
> or putting all the required data in a bean (which is created by
> <jsp:useBean> in first jsp) with session scope and accessing that bean
> in second jsp ??
> with out seeing the entirety of your problem I can say that copying
> HttpRequest attributes into a bean (which in turn may store attributes
> and values in a hashmap) and setting it in Session and reusing it in
> second jsp should not take much time.
>

Ok. Your solution is what I proposed in my last paragraph, the only
difference is that you are more explicit.

The example below explains what is difficult :

EX1 :

     Imagine that we have a set of servlets forwarding to the same jsp page.

     After we do the modification to our system some of these servlets
     must store the data temporarly (as you said, in the session).

     Now the JSP rendering the result must have a way of distinguishing who
     forwarded to it. If the forward was performed by one of our modified
     servlets then it must read the results from the session. Otherwise
     it can read the results from the place where it used to read.

     We don't want to introduce such kind of dependency in our system.


Without analyzing the problem in detail I believe I can provide
you with other (more or less) inconvenient problems.

regards
cristi

> O n 7/15/05, cristi <[hidden email]> wrote:
>
>>>Can you tell us what you are trying to achieve?  There is almost
>>>certainly an alternative way of getting to the same end without having
>>>to copy requests but, until you give us a bit more detail, we can't help
>>>you.
>>
>>We have  a software project up  an running but  some modifications are
>>required.
>>
>>The  requirement  of  making  a  copy  of  the  HttpServletRequest  is
>>generated  by the fact  that the  former programmers  have used  (in a
>>natural manner):
>>
>>1) the HttpServletRequest.setAttributes()  to  send data  to the  jsp
>>   pages  creating  the response.  ( These    data   is  the  servlet
>>   computation result).
>>
>>2) within  the jsp  pages creating the  result various methods  of the
>>   HttpServletRequest (other than HttpServletRequest.getAttributes() )
>>   object are used to get information from the request object.
>>
>>Now there  is the requirement that  in the second request  sent to the
>>servlet we  have to  use the computation  result of the  first request
>>(that   is    those   data   that    you   could   find    using   the
>>HttpServletRequest.getAttributes() applied to  the request object sent
>>in the first request ) to create the final HTML page.
>>
>>Let's supose now  that in the second request we have  a way of finding
>>the computation  result of  the first request.  This is not  enough to
>>satisfy the second request because in order to render the HTML page of
>>the second request  we also need other information  which can be found
>>in   the    request   object    sent   during   the    first   request
>>(e.g. HttpServletRequest.getParameter() );
>>
>>I hope that I was explicit enough.
>>
>>Final word: We  knew it from the start that  a solution exists (saving
>>            and using all  the data that we need between the  requests
>>            but  this  is  not  an  easy  task  now  since  we have to
>>            analyze - and  to write - a lot of code),  but  since  all
>>            this  data can be found  in the request object sent to the
>>            servlet with the  first  request we thought  that making a
>>            copy of  the request object should  be very easy (and very
>>            natural at the same time).
>>
>>Thx
>>C r i s t i   Z o i c a s.
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: [hidden email]
>>For additional commands, e-mail: [hidden email]
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: Copying an HttpServletRequest

Peter Crowther
In reply to this post by cristi-2
> From: cristi [mailto:[hidden email]]
> The  requirement  of  making  a  copy  of  the  HttpServletRequest  is
> generated  by the fact  that the  former programmers  have used  (in a
> natural manner):
>
> 1) the HttpServletRequest.setAttributes()  to  send data  to the  jsp
>     pages  creating  the response.  ( These    data   is  the  servlet
>     computation result).
>
> 2) within  the jsp  pages creating the  result various methods  of the
>     HttpServletRequest (other than
> HttpServletRequest.getAttributes() )
>     object are used to get information from the request object.

OK.  I agree, that's natural - if not a good design for future
requirements change.

> Now there  is the requirement that  in the second request  sent to the
> servlet we  have to  use the computation  result of the  first request
> (that   is    those   data   that    you   could   find    using   the
> HttpServletRequest.getAttributes() applied to  the request object sent
> in the first request ) to create the final HTML page.

OK.  So there's been a requirement change for which the original
approach was not designed, and so somebody on the business side (an
internal or external client) has to find the effort and the budget to
make that change, or decide that the change is too expensive and not
make it.  Somebody on the technical side has to inform them of the
expected effort, and keep them informed if that changes.

> Let's supose now  that in the second request we have  a way of finding
> the computation  result of  the first request.  This is not  enough to
> satisfy the second request because in order to render the HTML page of
> the second request  we also need other information  which can be found
> in   the    request   object    sent   during   the    first   request
> (e.g. HttpServletRequest.getParameter() );
>
> I hope that I was explicit enough.

You have been - many thanks.

> We  knew it from the start that  a solution exists (saving
> and using all  the data that we need between the requests
> but  this  is  not  an  easy  task  now  since we have to
> analyze - and  to write - a lot of code),  but since  all
> this  data can be found  in the request object sent to the
> servlet with the  first  request we thought that making a
> copy of  the request object should  be very easy

A suggestion for future projects: never presuppose that an external
piece of software will make your life easy.  Sounds cynical, I know.

> (and very natural at the same time).

Um.  Yes, until someone makes another change in the original pages that
breaks your code to pull out the data.  The approach is fast to code,
but fragile in the face of changes.

I suspect that, if you still want to follow this approach, about the
best you can do will be to create an alternative request implementation
that stores the fields you need, plus code to copy them out of the
original request into your alternative implementation.  Your alternative
only needs to store the minimum of data that you need for your
application - of course, this is fragile if further changes are made and
you realise more data should have been stored, and your developer
assumes that your partially-implemented class is actually complete.  You
can then store that alternative object; as you have implemented it, you
will know about its lifecycle.

If you do this, I think you're making your application more and more
fragile.  Instead of this, I would take the time at least to write a few
functions to retrieve your data - you could choose to simply pass a
parameter to decide which data source to use, or you could use (say) an
Adapter pattern to write an interface to the data, one implementation
that takes that from the current request, and one implementation that
takes it from your preserved data store.  You've then insulated yourself
from further changes.  It may be up to the development team to decide
how much insulation they want; I'll just note that most applications
change more, and more frequently, than the developers expect.

                - Peter

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Copying an HttpServletRequest

cristi-2
>
>
> Um.  Yes, until someone makes another change in the original pages that
> breaks your code to pull out the data.  The approach is fast to code,
> but fragile in the face of changes.
>

Yes. You are right. Besides the problem I presented to Peddireddy
(one of the guys who has answered my question) there is also the
problem you nominate.

In fact we will give up implemnting this kind of data transfer between
requests.Nice talking with you :)

Thank you for your colaboration.
C r i s t i a n       Z o i c a s



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]