You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Diego del Río <di...@hotmail.com> on 2001/11/14 14:01:55 UTC

Wrap an HttpServletRequest

Is there any way to wrap an HttpServletRequest in Tomcat 3.2.x and then
forward the wrapped request to anoher servlet?
We need a 'decorated request' with added functionality, more precisely, we
need to add 'extra-parameters' to that request and then forward it to
another servlet that process both the original and the added parameters.
Thanks,
diego

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Wrap an HttpServletRequest

Posted by Jeff Kilbride <je...@kilbride.com>.
Hi Diego,

You can also do this by adding query string data to the URL you are
forwarding to, for example:

---------
String myPage = "/path/myPage.jsp";
String myForward = myPage + "?newParam1=value1&newParam2=value2";

RequestDispatcher rd = getServletContext().getRequestDispatcher(myForward);
rd.forward(request, response);
---------

This way, your new info is added to the request as additional query string
data. Your existing JSP pages can access the info with the normal
request.getParameter() calls. They don't have to know that the request has
been "wrapped".

Thanks,
--jeff

----- Original Message -----
From: "Diego del Río" <di...@hotmail.com>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Wednesday, November 14, 2001 6:39 AM
Subject: Re: Wrap an HttpServletRequest


> Thank you Thomas and Martin,
> our problem is that the target servlet, i.e. the servlet that would
receive
> the 'wrapped request' forwarded by the first servlet (in this case, the
> target
> servlet is a jsp page), shouldn't know that the request is wrapped.
> We have already implemented a lot of jsp pages (servlets at last) that
> aren't
> aware about attributes in the request they process.
> I know that this functionality is provided by 'filters' in the servlet 2.3
> spec.; unfortunately we are using Tomcat 3.2.3, a servlet 2.2 engine.
>
> > Hi Diego,
> >
> > do it like Martin described. Or try out the RequestDispatcher:
> >
> > request.setAttribute(key1, value1);
> > request.setAttribute(key2, value2);
> > [...]
> > RequestDispatcher rd =
> > getServletContext().getRequestDispatcher(myServletUrl);
> > rd.forward(request, response);
> >
> > Thomas
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>
>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


RE: resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);

Posted by Carl Boudreau <ca...@notiva.com>.
Hey Tom,

Another java developer has sugested another avenue of attack

I have replace that last code snippet with ;

                    if(TxtCheck1.equals(User_Name)) {
                        // User join1 data has been added to Database
                        String Redirect_String = "./ValidUser?firstname=" +
User_Name;
                        WltRedirect myRedirect = new
WltRedirect(Redirect_String);
                        out.println(myRedirect.getRedirect());
                        }

And then built an object;

public class WltRedirect
{
    String redirect;
    String fixedA = "<html><head><META HTTP-EQUIV=\"Expires\" CONTENT=\"Tue,
01 Jan 1980 1:00:00 GMT\">";
    String fixedB = "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\"><meta
http-equiv=\"Refresh\" Content=\"0; URL=";
    String PassedIn;
    String fixedD = "\"><title>Weekly Love
Tips</title></head><body></body></html>";

    public WltRedirect (String PassedIn)
    {
        redirect = fixedA + fixedB + PassedIn + fixedD;
    }

    public String getRedirect()
    {
        return redirect;
    }
}

This seems to be operating smoothly.


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);

Posted by Tom Drake <rt...@pobox.com>.
Carl:

encodeURL() and encodeRedirectURL() do not do what you think they do.
These methods may add a 'jsessioniid=...' entry to url. They will not URL
encode
the url string itself.

However, I think that I was wrong about the '@' anyway. You could still have
a problem with
invalid characters in your URL. Everyone should be urlencoding everything
after the '?' in their url's.
There is some code on the jakarta site that will do this. I think it's in a
class called something
like HttpUtils.java. I think you can find it in taglibs or struts.

I don't know what would cause an illegal state exception.

Tom
----- Original Message -----
From: "Carl Boudreau" <ca...@notiva.com>
To: "'Tomcat Users List'" <to...@jakarta.apache.org>; "'Tom Drake'"
<rt...@pobox.com>
Sent: Wednesday, November 14, 2001 9:54 AM
Subject: RE: resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);


| Well Tom,
|
| I have changed the following code to read;
|                     if(TxtCheck1.equals(User_Name)) {
|                         // User join1 data has been added to Database
|                         String Redirect_String = "./ValidUser?firstname="
+
| User_Name;
|                         //resp.sendRedirect("./ValidUser?firstname=" +
| User_Name);
|                         //resp.encodeRedirectURL("./ValidUser?firstname="
+
| User_Name);
|                         out.println(Redirect_String + "<br>");
|                         //resp.sendRedirect(Redirect_String.trim());
|  Error >> >>
| resp.sendRedirect(resp.encodeRedirectURL(Redirect_String.trim()));
|                         //out.close();
|                         out.println("Found User_Name " + User_Name + " on
| Row " + result.getRow() + "<br>");
|
| //out.println(resp.encodeRedirectUrl("./ValidUser?firstname=" + User_Name)
+
| "<p>");
|                         out.flush();
|                         }
|                         else {
|                             out.println(User_Name + " User_Name Not Found
on
| Row " + result.getRow() + "<p>");
|
| //resp.sendRedirect("./htmlpage/joinerror.html");
|                             out.flush();
|                             //out.close();
|                             }
|
| And this is returning  "java.lang.IllegalStateException"  exception, and
| still crashing if the  "If"  is true.
|
| Any ideas?  Carl
|
|
| -----Original Message-----
| From: Tom Drake [mailto:rtd@pobox.com]
| Sent: Wednesday, November 14, 2001 9:57 AM
| To: Tomcat Users List; carl.boudreau@notiva.com
| Subject: Re: resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);
|
|
| You're @ is likely the problem. You must url-encoude the redirect address.
|
| ----- Original Message -----
| From: "Carl Boudreau" <ca...@notiva.com>
| To: "'Tomcat Users List'" <to...@jakarta.apache.org>
| Sent: Wednesday, November 14, 2001 7:32 AM
| Subject: resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);
|
|
| | Good Morning Fellow Coders,
| |
| | I have a very interesting question for all of you.  Below is a small
| | snippet of code which basically validates a database Insert operation.
| The
| | operation writes to the database then turns around and read the email
| | address and compares it to what was passed to the Servlets.  For example
| | within the 20 params sent, cboudrea@pressenter.com would be sent as an
| email
| | address.  It would then be written and then it would re-query the
database
| | and compare the results.  This snippet makes the comparison between what
| was
| | passed and what was read out of the database, in turn uses the Redirect
to
| | either move on to the next Servlets Join2 if successful, or display
| | joinerror.html if un-successful.  But an exception is caught and ends
the
| | Servlets.  During debug I have reduce the error to the redirect line
| | [resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);].
| |
| | Now I have been monitoring the email forum and had seen there was a
| problem
| | with the redirect.  And in the mean time I had manually started these
| | Servlets by using IE and entering the
| | [./Join2?Email_Address=cboudrea@pressenter.com)]  and it works fine.  I
| | think, I found out, that if I manually do this first, then run it
normally
| | by hitting it from a index.html page it works.  But my results have been
| | very erratic and I don't trust myself.
| |
| | I have tried to use out.flush( ); alone and then followed up by a
| | out.close( ); by this doesn't seem to change anything.
| |
| | DOES ANYONE HAVE ANY SUGGESTIONS, OR A WORK AROUND?
| |
| | Source Code Snippet;
| |
| |                     if(TxtCheck1.equals(MemberMail))
| |                        {
| |                         // User join1 data has been added to Database
| |                        resp.sendRedirect("./Join2?Email_Address=" +
| | EmailAddress);
| |                         }
| |                         else {
| |
| | resp.sendRedirect("./htmlpage/joinerror.html");
| |                                }
| |
| |
| | System Config;
| |
| | Win2000pro.
| | Tomcat 4.0 standalone.
| | MySQL 7.0
| |
| |
| | --
| | To unsubscribe:   <ma...@jakarta.apache.org>
| | For additional commands: <ma...@jakarta.apache.org>
| | Troubles with the list: <ma...@jakarta.apache.org>
| |
| |
| |
|
|
| --
| To unsubscribe:   <ma...@jakarta.apache.org>
| For additional commands: <ma...@jakarta.apache.org>
| Troubles with the list: <ma...@jakarta.apache.org>
|
|
|


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


RE: resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);

Posted by Carl Boudreau <ca...@notiva.com>.
Well Tom,

I have changed the following code to read;
                    if(TxtCheck1.equals(User_Name)) {
                        // User join1 data has been added to Database
                        String Redirect_String = "./ValidUser?firstname=" +
User_Name;
                        //resp.sendRedirect("./ValidUser?firstname=" +
User_Name);
                        //resp.encodeRedirectURL("./ValidUser?firstname=" +
User_Name);
                        out.println(Redirect_String + "<br>");
                        //resp.sendRedirect(Redirect_String.trim());
 Error >> >>
resp.sendRedirect(resp.encodeRedirectURL(Redirect_String.trim()));
                        //out.close();
                        out.println("Found User_Name " + User_Name + " on
Row " + result.getRow() + "<br>");

//out.println(resp.encodeRedirectUrl("./ValidUser?firstname=" + User_Name) +
"<p>");
                        out.flush();
                        }
                        else {
                            out.println(User_Name + " User_Name Not Found on
Row " + result.getRow() + "<p>");

//resp.sendRedirect("./htmlpage/joinerror.html");
                            out.flush();
                            //out.close();
                            }

And this is returning  "java.lang.IllegalStateException"  exception, and
still crashing if the  "If"  is true.

Any ideas?  Carl


-----Original Message-----
From: Tom Drake [mailto:rtd@pobox.com]
Sent: Wednesday, November 14, 2001 9:57 AM
To: Tomcat Users List; carl.boudreau@notiva.com
Subject: Re: resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);


You're @ is likely the problem. You must url-encoude the redirect address.

----- Original Message -----
From: "Carl Boudreau" <ca...@notiva.com>
To: "'Tomcat Users List'" <to...@jakarta.apache.org>
Sent: Wednesday, November 14, 2001 7:32 AM
Subject: resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);


| Good Morning Fellow Coders,
|
| I have a very interesting question for all of you.  Below is a small
| snippet of code which basically validates a database Insert operation.
The
| operation writes to the database then turns around and read the email
| address and compares it to what was passed to the Servlets.  For example
| within the 20 params sent, cboudrea@pressenter.com would be sent as an
email
| address.  It would then be written and then it would re-query the database
| and compare the results.  This snippet makes the comparison between what
was
| passed and what was read out of the database, in turn uses the Redirect to
| either move on to the next Servlets Join2 if successful, or display
| joinerror.html if un-successful.  But an exception is caught and ends the
| Servlets.  During debug I have reduce the error to the redirect line
| [resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);].
|
| Now I have been monitoring the email forum and had seen there was a
problem
| with the redirect.  And in the mean time I had manually started these
| Servlets by using IE and entering the
| [./Join2?Email_Address=cboudrea@pressenter.com)]  and it works fine.  I
| think, I found out, that if I manually do this first, then run it normally
| by hitting it from a index.html page it works.  But my results have been
| very erratic and I don't trust myself.
|
| I have tried to use out.flush( ); alone and then followed up by a
| out.close( ); by this doesn't seem to change anything.
|
| DOES ANYONE HAVE ANY SUGGESTIONS, OR A WORK AROUND?
|
| Source Code Snippet;
|
|                     if(TxtCheck1.equals(MemberMail))
|                        {
|                         // User join1 data has been added to Database
|                        resp.sendRedirect("./Join2?Email_Address=" +
| EmailAddress);
|                         }
|                         else {
|
| resp.sendRedirect("./htmlpage/joinerror.html");
|                                }
|
|
| System Config;
|
| Win2000pro.
| Tomcat 4.0 standalone.
| MySQL 7.0
|
|
| --
| To unsubscribe:   <ma...@jakarta.apache.org>
| For additional commands: <ma...@jakarta.apache.org>
| Troubles with the list: <ma...@jakarta.apache.org>
|
|
|


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);

Posted by Tom Drake <rt...@pobox.com>.
You're @ is likely the problem. You must url-encoude the redirect address.

----- Original Message -----
From: "Carl Boudreau" <ca...@notiva.com>
To: "'Tomcat Users List'" <to...@jakarta.apache.org>
Sent: Wednesday, November 14, 2001 7:32 AM
Subject: resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);


| Good Morning Fellow Coders,
|
| I have a very interesting question for all of you.  Below is a small
| snippet of code which basically validates a database Insert operation.
The
| operation writes to the database then turns around and read the email
| address and compares it to what was passed to the Servlets.  For example
| within the 20 params sent, cboudrea@pressenter.com would be sent as an
email
| address.  It would then be written and then it would re-query the database
| and compare the results.  This snippet makes the comparison between what
was
| passed and what was read out of the database, in turn uses the Redirect to
| either move on to the next Servlets Join2 if successful, or display
| joinerror.html if un-successful.  But an exception is caught and ends the
| Servlets.  During debug I have reduce the error to the redirect line
| [resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);].
|
| Now I have been monitoring the email forum and had seen there was a
problem
| with the redirect.  And in the mean time I had manually started these
| Servlets by using IE and entering the
| [./Join2?Email_Address=cboudrea@pressenter.com)]  and it works fine.  I
| think, I found out, that if I manually do this first, then run it normally
| by hitting it from a index.html page it works.  But my results have been
| very erratic and I don't trust myself.
|
| I have tried to use out.flush( ); alone and then followed up by a
| out.close( ); by this doesn't seem to change anything.
|
| DOES ANYONE HAVE ANY SUGGESTIONS, OR A WORK AROUND?
|
| Source Code Snippet;
|
|                     if(TxtCheck1.equals(MemberMail))
|                        {
|                         // User join1 data has been added to Database
|                        resp.sendRedirect("./Join2?Email_Address=" +
| EmailAddress);
|                         }
|                         else {
|
| resp.sendRedirect("./htmlpage/joinerror.html");
|                                }
|
|
| System Config;
|
| Win2000pro.
| Tomcat 4.0 standalone.
| MySQL 7.0
|
|
| --
| To unsubscribe:   <ma...@jakarta.apache.org>
| For additional commands: <ma...@jakarta.apache.org>
| Troubles with the list: <ma...@jakarta.apache.org>
|
|
|


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);

Posted by Carl Boudreau <ca...@notiva.com>.
Good Morning Fellow Coders,

	I have a very interesting question for all of you.  Below is a small
snippet of code which basically validates a database Insert operation.  The
operation writes to the database then turns around and read the email
address and compares it to what was passed to the Servlets.  For example
within the 20 params sent, cboudrea@pressenter.com would be sent as an email
address.  It would then be written and then it would re-query the database
and compare the results.  This snippet makes the comparison between what was
passed and what was read out of the database, in turn uses the Redirect to
either move on to the next Servlets Join2 if successful, or display
joinerror.html if un-successful.  But an exception is caught and ends the
Servlets.  During debug I have reduce the error to the redirect line
[resp.sendRedirect("./Join2?Email_Address=" + EmailAddress);].

	Now I have been monitoring the email forum and had seen there was a problem
with the redirect.  And in the mean time I had manually started these
Servlets by using IE and entering the
[./Join2?Email_Address=cboudrea@pressenter.com)]  and it works fine.  I
think, I found out, that if I manually do this first, then run it normally
by hitting it from a index.html page it works.  But my results have been
very erratic and I don't trust myself.

	I have tried to use out.flush( ); alone and then followed up by a
out.close( ); by this doesn't seem to change anything.

	DOES ANYONE HAVE ANY SUGGESTIONS, OR A WORK AROUND?

Source Code Snippet;

                    if(TxtCheck1.equals(MemberMail))
                       {
                        // User join1 data has been added to Database
                       	resp.sendRedirect("./Join2?Email_Address=" +
EmailAddress);
                        }
                        else {

resp.sendRedirect("./htmlpage/joinerror.html");
                               }


System Config;

	Win2000pro.
	Tomcat 4.0 standalone.
	MySQL 7.0


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Wrap an HttpServletRequest

Posted by Diego del Río <di...@hotmail.com>.
Thank you Thomas and Martin,
our problem is that the target servlet, i.e. the servlet that would receive
the 'wrapped request' forwarded by the first servlet (in this case, the
target
servlet is a jsp page), shouldn't know that the request is wrapped.
We have already implemented a lot of jsp pages (servlets at last) that
aren't
aware about attributes in the request they process.
I know that this functionality is provided by 'filters' in the servlet 2.3
spec.; unfortunately we are using Tomcat 3.2.3, a servlet 2.2 engine.

> Hi Diego,
>
> do it like Martin described. Or try out the RequestDispatcher:
>
> request.setAttribute(key1, value1);
> request.setAttribute(key2, value2);
> [...]
> RequestDispatcher rd =
> getServletContext().getRequestDispatcher(myServletUrl);
> rd.forward(request, response);
>
> Thomas

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


AW: Wrap an HttpServletRequest

Posted by "Thomas Rickal, IOP Unternehmensberatung GmbH" <t....@iop.de>.
Hi Diego,

do it like Martin described. Or try out the RequestDispatcher:

request.setAttribute(key1, value1);
request.setAttribute(key2, value2);
[...]
RequestDispatcher rd =
getServletContext().getRequestDispatcher(myServletUrl);
rd.forward(request, response);

Thomas



--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Wrap an HttpServletRequest

Posted by Fredrik Westermarck <fr...@mdh.se>.
Martin van den Bemt wrote:
> 
> Pas the request object to that servlet eg
> BlahServlet servlet = new BlahServlet();
> servlet.execute(req, resp);

Why not get a RequestDispatcher and forward (or include) to the other
servlet?

..
  RequestDispatcher rd = request.getRequestDispatcher(pathToServlet);
  rd.forward(request, response); // or rd.include(request, response)
..

Use the setAttribute()-method in the servlet that handles the original
request to add the "extra parameters". Then use getAttribute() in the
servlet that you forwarded or included to read the "extra parameters".

-- 
Fredrik Westermarck

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


RE: Wrap an HttpServletRequest

Posted by Martin van den Bemt <ma...@isallineed.org>.
Pas the request object to that servlet eg
BlahServlet servlet = new BlahServlet();
servlet.execute(req, resp);

Mvgr,
Martin

> -----Original Message-----
> From: Diego del Río [mailto:diegodelrio@hotmail.com]
> Sent: Wednesday, November 14, 2001 2:02 PM
> To: tomcat-user@jakarta.apache.org
> Subject: Wrap an HttpServletRequest
>
>
> Is there any way to wrap an HttpServletRequest in Tomcat 3.2.x and then
> forward the wrapped request to anoher servlet?
> We need a 'decorated request' with added functionality, more precisely, we
> need to add 'extra-parameters' to that request and then forward it to
> another servlet that process both the original and the added parameters.
> Thanks,
> diego
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>