Avatar of Ajay Chowdary Kandula
Ajay Chowdary Kandula
Flag for United States of America asked on

Obtain User ID in ContextListener

We have ContextListener implemented ( i.e. our class implements javax.servlet.ServletContextListener)

Am trying to obtain the user id and I have ServletContextEvent object available as parameter for contextInitialized

Right now it is hardcoded.

The same username is obtained in JSP like this

      <jstl:set var="userId" scope="session" value="<%= request.getRemoteUser() %>"></jstl:set>

Is there anyway, I obtain the same from JSP, as I need the value that is being set there
Web ServersJavaJava EE

Avatar of undefined
Last Comment
Ajay Chowdary Kandula

8/22/2022 - Mon
SOLUTION
rrz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ajay Chowdary Kandula

ASKER
What do you really want to accomplish here ?  
Obtain the username in the file carsContextListener.java


What is hardcoded ? Which user ? Please show us that code.
//code below


 public void contextInitialized(ServletContextEvent contextEvent) {
            try{
                  String fileSeperator = System.getProperty("file.separator");
                  
                //FIXME: the user is hardcoded, need to get an actual session attrib
                contextEvent.getServletContext().setAttribute("user", " rrz@871311");
ASKER CERTIFIED SOLUTION
rrz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dpearson

I think if you're trying to work with the "current user" inside the ServletContext initialization you probably have the wrong design in mind.

The ServletContext is a single context shared by all Servlet and created at startup.  It shouldn't be used to store user specific information.

You probably should instead be implementing a Servlet (not a ServletContext) - which is initialized to handle an individual request (e.g. a page load) and so has the idea of a "current user" (or current session).  That Servlet could then make use of the ServletContext to get work done if necessary (e.g. gaining access to a database which has connections stored in the context).

Hope that helps,

Doug
Ajay Chowdary Kandula

ASKER
Thanks Guys for the Help, your inputs helped me to understand the concepts
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy