Forums Register Login

Add a constructor in a subclass

+Pie Number of slices to send: Send
I'm using the com.oreilly.servlet class, and wanting to 'tweak' one of its classes.

For a very quick background on *exactly* what I'm trying to do, you can check this article

My problem is with the MultipartWrapper. Code of interest: The problem is the call to MultipartRequest(req, dir).

MultipartRequest has about 8 constructors. I need the one that allows me to set a file upload size larger than 1 MB (which is the default when you don't specify). So I thought to do the following: If I leave the call to super(req, dir) commented out, I get this error:By uncommenting that call to super(req, dir), it compiles. But unfortunately, because of what super(req, dir) *Does* to the req object, I cannot use it again in the next line.. (actually it compiles fine, but runtime you get "unexpected end of part".. which is what happens when you try to read a request object twice, but beyond the scope of the question...

The compiler will insert a call to the no-args constructor, when you don't specify one. But if I specify one, it breaks the functioning of the class. Is there a way around this? Am I doing something that is fundamentally wrong (trying to add constructor signature in a subclass) or is this just a side-effect of the *req* object being ... flakey.. when its touched more than once?
+Pie Number of slices to send: Send
It looks like you are out of luck with regards to overridding the constructor. However, you aren't completely out of luck. One sneaky thing you could do (besides not using poorly defined and inextensible 3rd party code) would be to pass in a DIFFERENT HttpServletRequest (think mock object) to the super class constructor.
I don't know what the MultipartRequest constructor is doing to the real HttpServletRequest that is making it unusable, but you can always pass a fake HttpServletRequest to it. Then you can override mreq with the value constructed with the real HttpServletRequest.
+Pie Number of slices to send: Send
It seems like this is the sort of thing that might get run into a lot.

Is providing a no-args constructor (even if it does nothing) a good-neighbor sort of thing, considering that at some point, someone might want to extend your class?
+Pie Number of slices to send: Send
what happens if we put like this?
public MultipartRequestWrapper()
{
super();
}
+Pie Number of slices to send: Send
The problem is, there is no superclass, no-args constructor.

So calling super() explicitly will result in the same problem as not calling super() explicity and having the compiler do it for you.
+Pie Number of slices to send: Send
Did the "mock" HttpServletRequest idea work? I don't have the code in from of me, so I can't test it. But I don't see any reason it wouldn't work...
+Pie Number of slices to send: Send
i'm very sure it would work, but I've gone ahead and done something different.

The reason I wanted to extend MultipartWrapper was because it defined a whole bunch of other methods of use.

Instead of extending though (since it seems impossible because of the constructor difficulty), I've gone ahead and implemented MultipartRequestWrapper as extending HttpServletRequestWrapper , and i've got 8 constructors for it, to match the 8 constructors of the MultipartRequest. AND i've boiler-plated the other code into my class.
Don't play dumb with me! But you can try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 954 times.
Similar Threads
Upload file and fetch other parameters as well...
Set Ant Classpath for Compile
Error compiling code that uses MultipartRequest
Servlet can't find other servlet
ANT help required
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:20:26.