Link to home
Start Free TrialLog in
Avatar of Ajay Chowdary Kandula
Ajay Chowdary KandulaFlag 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
SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpearson
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
Thanks Guys for the Help, your inputs helped me to understand the concepts