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 719235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:37:07+00:00 2026-05-14T05:37:07+00:00

I’m trying to write a custom servlet (for AJAX/JSON) in which I would like

  • 0

I’m trying to write a custom servlet (for AJAX/JSON) in which I would like to reference my @ManagedBeans by name. I’m hoping to map:

http://host/app/myBean/myProperty

to:

@ManagedBean(name="myBean")
public class MyBean {
    public String getMyProperty();
}

Is it possible to load a bean by name from a regular servlet? Is there a JSF servlet or helper I could use for it?

I seem to be spoilt by Spring in which all this is too obvious.

  • 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-14T05:37:08+00:00Added an answer on May 14, 2026 at 5:37 am

    In a servlet based artifact, such as @WebServlet, @WebFilter and @WebListener, you can grab a "plain vanilla" JSF @ManagedBean @RequestScoped by:

    Bean bean = (Bean) request.getAttribute("beanName");
    

    and @ManagedBean @SessionScoped by:

    Bean bean = (Bean) request.getSession().getAttribute("beanName");
    

    and @ManagedBean @ApplicationScoped by:

    Bean bean = (Bean) getServletContext().getAttribute("beanName");
    

    Note that this prerequires that the bean is already autocreated by JSF beforehand. Else these will return null. You’d then need to manually create the bean and use setAttribute("beanName", bean).


    If you’re able to use CDI @Named instead of the since JSF 2.3 deprecated @ManagedBean, then it’s even more easy, particularly because you don’t anymore need to manually create the beans:

    @Inject
    private Bean bean;
    

    Note that this won’t work when you’re using @Named @ViewScoped because the bean can only be identified by JSF view state and that’s only available when the FacesServlet has been invoked. So in a filter which runs before that, accessing an @Injected @ViewScoped will always throw ContextNotActiveException.


    Only when you’re inside @ManagedBean, then you can use @ManagedProperty:

    @ManagedProperty("#{bean}")
    private Bean bean;
    

    Note that this doesn’t work inside a @Named or @WebServlet or any other artifact. It really works inside @ManagedBean only.


    If you’re not inside a @ManagedBean, but the FacesContext is readily available (i.e. FacesContext#getCurrentInstance() doesn’t return null), you can also use Application#evaluateExpressionGet():

    FacesContext context = FacesContext.getCurrentInstance();
    Bean bean = context.getApplication().evaluateExpressionGet(context, "#{beanName}", Bean.class);
    

    which can be convenienced as follows:

    @SuppressWarnings("unchecked")
    public static <T> T findBean(String beanName) {
        FacesContext context = FacesContext.getCurrentInstance();
        return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class);
    }
    

    and can be used as follows:

    Bean bean = findBean("bean");
    

    See also:

    • Backing beans (@ManagedBean) or CDI Beans (@Named)?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I am trying to understand how to use SyndicationItem to display feed which is
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to select an H1 element which is the second-child in its group
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.