All,
I have a servlet that needs to use another one to generate some content,
then post-process it. My current pseudo code looks like this, which is
rather ugly IMO:
URL url = new URL("http://localhost:port/path/to/resource");
HttpURLConnection conn = url.openConnection();
InputStream in = conn.getOutputStream();
StringBuilder sb = new StringBuilder();
while() {
sb.append(in.read());
}
conn.close();
// Now process the string
This is ugly for a couple of reasons:
1. Hard-coded URL; could be made to be dynamic but IMO there are better ways
2. Actual HTTP connection is necessary ... while processing an HTTP request
So what I'd like to do is change this to something like the following:
HttpServletRequest subrequest = ...;
HttpServletResponse subResponse = ...;
context.getRequestDispatcher("/path/to/resource").forward(request,
response);
// or
context.getRequestDispatcher("/path/to/resource").include(request,
response);
This avoids both of the above issues, plus uses one fewer
request-processing thread to handle what is conceptually a single request.
My problem is that I'm not really sure the best way to construct the
subrequest and subresponse objects.
One way to do it would be to wrap the current request and response in
HttpServletRequestWrapper and HttpServletResponseWrapper objects, each
of which toss-out any modifications to the original objects, and
captures the response. With enough code, I'm sure I can do it, having
written a capturing filter in the past [1].
In this particular case, I feel like this should be a little simpler
than the capturing filter I wrote back then, because I'm not trying to
"intercept" anything while also allowing the usual request/response flow
to continue. I'm just doing a normal forward/include ... but capturing
the response.
Does anyone have any suggestions other than "yeah, you have to build it
from scratch yourself"?
-chris
[1] https://lists.apache.org/thread/wvzy026rcz8ztmrz159c9nl11dodkkz9
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org