Where to call getUserPrincipal(), or other methods to find out the logged on user

I have succesfully setup security (with Glassfish but that seems to be somewhat irrelevant to this question) for a Netbeans visual application.
Within my application I need to know the user name (for logging f.i.) and I found out that I can get it via request.getUserPrincipal(). However, I do not have access to a request within, either my sessionbean (which I can understand since it is something else) but also not in my jsp (and the related java classes).
The only place in code where exist a HTTPServletRequest is in the login_jsp.java but that seems to early (and I cannot access any sessionbean from there).

So the question is: How to put request.getUserPrincipal() into a sessionbean? Accessable via f.i. getSessionBean1().getUsername() throughout my application.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Since a .jsp page defines the request object, you can always call
in a .jsp page.  What did you try in your .jsp page that didn't work?

Perhaps it was the "related java classes" -- yes, if they don't have the request object, then you won't be able to call getUserPrincipal() from it.

To have the login name in your session bean, then you can add a login name property to your session bean.  You can assign the getUserPrincipal() value to it from any JSP page or servlet.  If your application has a JSP page or servlet that is always executed after login, then you can set the login name in one place.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bmentingAuthor Commented:
Hi Mr. Coffee,

Ok, I understand that. The next question is: How exactly? I tried adding <% getSessionBean1().setUserName(request.getUnserPrincipal().getName()) %> as well as <jsp:scriptlet getSessionBean1.setUserName(request.getUserPrincipal().getName()) /> on various places inside the JSP code but I get errors about incorrect syntax.

I include the code of the page that is visited at startup (after logon). It consists of 3 page fragments.
Can you tell me where to add which code inside the JSP(F)  to set sessionBean UserName (the sessionBean call already exist)

<?xml version="1.0" encoding="UTF-8"?>
<jsp:root version="1.2" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:ui="http://www.sun.com/web/ui">
    <jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>
        <ui:page binding="#{Dossier$Startpagina.page1}" id="page1">
            <ui:html binding="#{Dossier$Startpagina.html1}" id="html1">
                <ui:head binding="#{Dossier$Startpagina.head1}" id="head1">
                    <ui:link binding="#{Dossier$Startpagina.link1}" id="link1" url="/resources/stylesheet.css"/>
                <ui:body binding="#{Dossier$Startpagina.body1}" id="body1" style="-rave-layout: grid">
                    <ui:form binding="#{Dossier$Startpagina.form1}" id="form1">
                        <div class="fragmentinhoud">
                            <jsp:directive.include file="Informatie.jspf"/>
                        <div class="fragmentinhoud">
                            <jsp:directive.include file="StartpaginaFragment.jspf"/>
                        <div class="fragmentmenu">
                            <jsp:directive.include file="Menu.jspf"/>

Open in new window

You can do a plain JSP scriptlet to get it, like this:

<%  String loginName = request.getUserPrincipal();  
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

bmentingAuthor Commented:
Ok, bit further now...

With <% %> seems not to work within JSP netbeans but
    login="Some value";
works. I changed it to "Some value" since at the moment I have not setup security.
login is a private variable inside the java.
This however does not work... How can I set some variable from inside the JSP to have it accessable from Java? Can I put it into some bean? Preferably a session bean?

Thanks so far!

Yes, you can also do <jsp:scriptlet> for your JSP scriptlet code.

Yes, if the login variable is not declared in your JSP page, then you can't assign a value to it.

>>login is a private variable inside the java.
I'm not sure what you mean here -- it's all Java code.  Do you mean that it's a private variable in some other class, and not declared in the JSP page?  If so, then yes, if the variable is not declared in the JSP page, you can't assign it, just as in any Java code.

>>How can I set some variable from inside the JSP to have it accessable from Java?

Okay, I'll assume here that you mean that the Java you're referring to is not in your JSP page.  So if you have other Java classes in your application, you can pass variables to those classes by parameters on the method calls, or by putting values into a user session object.

Yes, you can create a session bean and write your login name to it, then read it from other classes which have access to the session.

You should try these and see how they work for you.
bmentingAuthor Commented:

Found what I needed. In the JSP I do:

 <jsp:useBean class="MyPackage.Information" scope="session" id="IKB">
        if (request.getUserPrincipal()!=null) {

and in java I do:

With some NULL test since I do not know the timing exactly.

And MyPackage.Information is a simple Java bean with get + set.

Actually rather easy, but I didn't know that, and it took me several hours to get it working.

Thanks for pointing me in the right direction (request is available in JSP).
That looks fine.  Good luck.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.