Wrapper, Filter and Servlet
Asked Answered
P

1

6

since I am new to Servlet programming, it is possible that I ask a basic question.

I am writing an application where a Filter gets the response from a servlet, and does some computation with it. I found out that I need a wrapper class to catch the response.

My question now is why the wrapper is needed? Thanks in advance!

Parturition answered 10/5, 2013 at 10:34 Comment(5)
Only you can answer this question as you already found out that you need a wrapper class :-) What are you trying to achieve? And no, a wrapper (request/response) is not always necessary...Politburo
as far as I understood it, when I want to catch the Servlet response and do something with it in my filter, I need a wrapper. Is that correct? What about this case: I have a form, where a user can type in username and some text. This should then be displayed on another page. I know a servlet can take the input parameters and forward to another page. Can I achieve the same with a filter?Parturition
1. No, who told you that (an example might help here)? 2. I'd rather not implement business logic in a filter, why you think a filter is the better choice?Politburo
I think I'm just really confused with what both things are for ...Parturition
In general, you should use filters for non-functional aspects. See docs.oracle.com/javaee/6/api/javax/servlet/Filter.html for some examples.Politburo
Y
15

1) Lets first understand how Request and Request Filter work:

When, lets say client, makes a request to servlet, it goes through container. Container decides what servlet Request needs to be forwarded to. Which means, container is in total control.

Container control makes implementing request filter easy because we can just let container know that the Request should go to filter first and then to servlet. Because container is in total control. So implementing request filter is easy.

2) Now lets understand how Response and Response Filter work:

When container invokes Servlet service methods, it passes 2 objects to the methods, Request and Response.

This simply means that Servlet is in total control to send the response back to client. How ?

Because Response object has the pointer to Output Stream Writer object. Which means, once Servlet finishes processing the request, it will straight write the Response back to the client using pointer to Output Writer Stream object. So, Servlet won't wait for anyone (middle men like filter) and straight serve the client. By the time, it will be too late to intervene.

So, what's the solutions ?

Wrapper is our solution.

How Wrapper works ?

So, before container passes the real Request and Response objects to Servlet, we will wrap our Response object and then send the Real Request and Wrapped Response objects to the Servlet service methods.

So now, Servlet has pointer to our Wrapped Output Stream Writer object and not the Real Response Output Stream Writer object. So, when Servlet finishes the request, it will write the response to our Wrapped Stream and then Our Wrapped Response Object will write back to Real Response Writer Stream.

Moral of the story: Use wrapper when dealing with Response. Request doesn't need wrapper concept.

Yingyingkow answered 11/5, 2013 at 0:56 Comment(1)
Request might need a wrapper concept, i.e. javacodegeeks.com/2012/07/…Creatinine

© 2022 - 2024 — McMap. All rights reserved.