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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:29:45+00:00 2026-05-14T08:29:45+00:00

Let’s say I have a simple Login servlet that checks the passed name and

  • 0

Let’s say I have a simple Login servlet that checks the passed name and creates User object and stores it in a session.

User user = new User();
user.setId(name);

request.getSession().setAttribute("user", user);
response.sendRedirect("index.jsp");

In the index.jsp page I access the user object through jsp:useBean

<jsp:useBean id="user" scope="session"
             class="package.name.User"/>

<div class="panel">
    Welcome ${user.id}
</div>

It works so far.

From the jsp beans documentation

To locate or instantiate the Bean,
takes the following
steps, in this order:

  1. Attempts to locate a Bean with the scope and name you specify.
  2. Defines an object reference variable with the name you specify.
  3. If it finds the Bean, stores a reference to it in the variable. If
    you specified type, gives the Bean
    that type.
  4. If it does not find the Bean, instantiates it from the class you
    specify, storing a reference to it in
    the new variable. If the class name
    represents a serialized template, the
    Bean is instantiated by
    java.beans.Beans.instantiate.
  5. If has instantiated (rather than located) the Bean, and if
    it has body tags or elements (between
    and ),
    executes the body tags.

The questions:

Attempts to locate a Bean with the scope and name you specify

It does not specify the “locate” process. Does it mean it will check HttpServletRequest.getSession() or just check whether other pages already created this bean or not?

If it does not find the Bean, instantiates it from the class you specify, storing a > reference to it in the new variable.

This actually means that Jsp can associate a newly created bean with session using jsp_internal_name_user.
There is no word about how Jsp stores and finds beans in the session.

There is an option to access session objects by using ${sessionScope.user} and that will guarantee that “user” from the Java session object will be get. The same one I put into by myself.

Java EE 5 example “Book Store” access session objects using ${sessionScope.name} approach.

Using just ${user} works. And this is what worries me. I would like to see a specific sentence in the specification about the locate process and whether ${user} must work or whether it is up to the JSP and/or JSTL reference implementation.

  • 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-14T08:29:46+00:00Added an answer on May 14, 2026 at 8:29 am

    In case of a controller (servlet) which takes care about the model, the jsp:useBean is only useful if the default instance (constructed with the no-arg constructor) exposes different behavior/state than a non-existing instance. E.g. if you would like to have a default user name “Unknown User”, you would do:

    public User {
        this.id = "Unknown User";
    }
    

    Else the enduser may face a “Welcome” instead of “Welcome Unknown User” display. In your particular case, you can safely remove it. It’s superfluous.

    However, I’ve also seen the argument that it’s useful for pure documentation. You could declare “useless” jsp:useBean instances in top of JSP page so that you have an overview which models exactly are been used in the particular JSP page. Although I find it pretty smart, I myself have never had the need for this way of documenting the model in JSP. As per the comments, another argument is indeed that this way IDE’s like IDEA and Eclipse are able to autocomplete bean properties in EL.

    Update: as to the locating, it uses PageContext#findAttribute() for that and then uses reflection/javabean introspection to invoke getter methods on it. E.g.

    ${user.name}
    

    roughly resolves to

    out.print(pageContext.findAttribute("user").getName())
    

    Also see the JSP specification and the JSP EL specification.

    Update 2: the <jsp:useBean> certainly doesn’t use an internal name or so as session attribute prefix. Loop over all session attributes yourself to see the actual keys and values:

    <c:forEach items="${sessionScope}" var="entry">
        ${entry.key} = ${entry.value}<br>
    </c:forEach>
    

    or in a servlet

    for (String name : Collections.list(session.getAttributeNames())) {
       System.out.println(name + " = " + session.getAttribute(name));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a user class with columns name and email: Class User
Let's say I have a structure named vertex with a method that adds two
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say I have an facebook application running using the JS SDK. First user
Let's say after I had login I will be prompt to enter the Name
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say that I have a model that handles recipes, and I want to
Let's say that I have a set of relations that looks like this: relations

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.