Get the HttpServletRequest (request) object from Java code

后端 未结 5 687
一生所求
一生所求 2020-12-31 07:14

I need to get hold of the request object in Java code. I can\'t pass this object down to my code for certain reasons. Is there any way I can say something like: getCur

5条回答
  •  半阙折子戏
    2020-12-31 07:56

    Assuming the top-level servlet really is taboo for some crazy business-related reason, there is still the option of defining a ServletFilter to pre-view the request and stuff it into a ThreadLocal. Assuming that the web.xml is not also sacrosanct.

    But I agree with Jon Skeet in that this would be very ugly. I'd code this up and then try to find a different job. :)

    Actually, given the fact that a filter can totally wrest away control from the receiving servlet, you could use this technique to divert the code to a servlet of your own, do whatever you want, and THEN run the other, "official" servlet... or anything else along those lines. Some of those solutions would even allow you to deal correctly and robustly with your request data.

提交回复
热议问题