Results 1 to 6 of 6

Thread: Accessing HttpServletRequest using RequestContext

  1. #1

    Unhappy Accessing HttpServletRequest using RequestContext

    Hi All,

    How can I get a handle of HttpServletRequest throught a RequestContext. My motive is to upload a file using multipart and the MultipartParser Class.

    I would be great full if you can help me out.


    Thank in advance.

    Regards,
    Ravi.

  2. #2
    Join Date
    Feb 2008
    Posts
    10

    Default

    Code:
    ServletExternalContext externalContext =
      (ServletExternalContext)context.getExternalContext();
    HttpServletRequest resquest = externalContext.getRequest();
    Bye!

  3. #3

    Default

    Thanks a lot,

  4. #4
    Join Date
    Mar 2010
    Posts
    2

    Default

    My apologies for bumping an old post back from the dead, but...

    The solution above does not work for me. externalContext.getRequest() is protected. I need to have access to the request.getServerName() method. I have a solution that involves using reflection to access the request, but it is messy and I would really like to replace it.

    Here is the reflection code:

    Code:
                ServletExternalContext servletExternalContext = ((ServletExternalContext) (context.getExternalContext()));
                HttpServletRequest request;
                Field requestField = ServletExternalContext.class.getDeclaredField("request");
                requestField.setAccessible(true);

    Is there a better way?

  5. #5
    Join Date
    Nov 2008
    Posts
    742

    Default

    Have you tried externalContext.getNativeRequest() ? You should be able to cast this into an HttpServletRequest, and not deal with reflection at all.

  6. #6
    Join Date
    Mar 2010
    Posts
    2

    Default

    That did the trick, thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •