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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:27:45+00:00 2026-05-15T06:27:45+00:00

Using Spring 3 MVC and JSP, I simply want to test if a user

  • 0

Using Spring 3 MVC and JSP, I simply want to test if a user is logged in, I’m not interested in using Spring Security currently

<jsp:useBean id="myAppUser" type="com.xxx.MyUser" beanName="myUser" scope="session" />
<c:choose>
    <c:when test="myUser.loggedIn">
        //dostuff
    </c:when>
    <c:otherwise>
        //dootherstuff
    </c:otherwise>
</c:choose>

But the problem is that when there isn’t yet a myAppUser in the session, jsp:useBean throws an exception. Now I realize that I can have the JSP:useBean actually instantiate the object by giving it a class, but I don’t like knowing that somewhere in some JSP fragment I have objects being instantiated and added to my session, so I either want to always set an initial value for that user, and have control over it programatically, or I’d like a way to get that bean that allows it to be null or not exist, if it doesn’t exist just return null

either way would be fine

if my question points to a fundamental misunderstanding in what I should be doing please provide a link to documentation that will thoroughly explain this use case

  • 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-15T06:27:46+00:00Added an answer on May 15, 2026 at 6:27 am

    This is not the right approach, but in fact you could solve the particular issue with the JSTL <c:catch>.

    <c:catch var="e">
        <jsp:useBean id="myAppUser" type="com.xxx.MyUser" beanName="myUser" scope="session" />
    </c:catch>
    <c:choose>
        <c:when test="${empty e && myUser.loggedIn}">Logged in</c:when>
        <c:otherwise>Bean doesn't exist or user is not logged in</c:otherwise>
    </c:choose>
    

    The right approach is described in answer of matt b. You really need to solve it at a higher level. Have a bean something like UserManager which has the User as child property along several methods like login(User), logout() and isLoggedIn(). If the user is logged in, then the User ought to be non-null. When the user logs out, then the User ought to be set to null.


    Update as per comments: As an alternative, you can also just get rid of the whole jsp:useBean declaration and intercept on the presence of the MVC-framework injected ${myUser} in the session.

    <c:choose>
        <c:when test="${myUser.loggedIn}">Logged in</c:when>
        <c:otherwise>Bean doesn't exist or user is not logged in</c:otherwise>
    </c:choose>
    

    EL will namely transparently “suppress” potential nullpointerexceptions and just return false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Spring MVC. I want to include menu page (named menu.jsp) with a
I am using Spring MVC 3.0 I have a guestbook.jsp page where I want
I'm currently building a Spring MVC application. I was looking to use JSP pages
I'm using Spring MVC and my jsp's have a <div id=debug> section that I
I'm writing webapp using Spring 3 MVC with annotations. I use jsp for view
I am using Spring MVC and Hibernate. I want to use the OpenSessionInViewFilter to
I'm using Spring MVC and Spring Security on a project, and am implementing a
I am using Spring MVC and want to develop a website that uses REST
I am using spring mvc 3.0 to build an web application. User can get
I am using Spring MVC to build my web application, and I have a

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.