Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:37:52+00:00 2026-05-31T12:37:52+00:00

I have a normal Java class in a Spring MVC 3.06 web application. In

  • 0

I have a normal Java class in a Spring MVC 3.06 web application.

In this class I would like to inject or get hold of the HttpServletRequest object in a method.

I know I can pass this around, but I was wondering how can I get hold of the request without passing it in to the method. Perhaps using annotations or similar?

Also, what are the “real” concerns with getting hold of the request this way, except some peoples opinions of it being ugly coding. I mean, is it unstable to access it this way?

Preferably non application server dependent way.

I have seen

(HttpServletRequest) RequestContextHolder.getRequestContext().getExternalContext().getNativeRequest() 

but this doesn’t seem to work for Spring MVC 3.06 . RequestContextHolder doesn’t have the method getRequestContext().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:37:54+00:00Added an answer on May 31, 2026 at 12:37 pm

    Use

    ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
    

    I’m not sure where you got RequestContextHolder.getRequestContext(), that’s completely wrong.

    is it unstable to access it this way?

    No, it’s stable enough, assuming you’re always running the code as part of an HttpServlet request thread. The main issue is that yes, it’s ugly, and it makes your code hard to test. That is reason enough not to use it.

    If you must use it, then decouple it from your code, e.g.

    public void doSomething() {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        doSomething(request);
    }
    
    void doSomething(HttpServletRequest request) {
       // put your business logic here, and test this method
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At this moment I start work on small web application based on MVC. Now
Spring MVC web app: I have a stack trace w/o line numbers (shown at
I have this piece of code in my java class mystring = mysuperstring.split(/); I
I have a base class, which is normal Java class. Three subclasses extend it
I have a normal textbox in my application. I can paste data to it
I have turned a normal PHP class into a library so I can use
I have troubles using the java ServiceLoader in a NetBeans module application. Here is
I have a three layered Java application consisting of a presentation layer, a business
I have a class in Java which is written bellow.I want to know does
We have normal java files residing in the SVN. We have made some changes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.