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

Posted on 2008-01-29
Medium Priority
Last Modified: 2013-12-14
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.
Question by:bmenting
  • 4
  • 3
LVL 27

Accepted Solution

mrcoffee365 earned 375 total points
ID: 20770125
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.

Author Comment

ID: 20775320
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

LVL 27

Expert Comment

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

<%  String loginName = request.getUserPrincipal();  
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Author Comment

ID: 20778615
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!

LVL 27

Expert Comment

ID: 20778958
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.

Author Comment

ID: 20787367

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).
LVL 27

Expert Comment

ID: 20788216
That looks fine.  Good luck.

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question