Ajay Chowdary Kandula
asked on
Obtain User ID in ContextListener
We have ContextListener implemented ( i.e. our class implements javax.servlet.ServletConte xtListener )
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
Thanks Guys for the Help, your inputs helped me to understand the concepts
ASKER
Obtain the username in the file carsContextListener.java
What is hardcoded ? Which user ? Please show us that code.
//code below
public void contextInitialized(Servlet
try{
String fileSeperator = System.getProperty("file.s
//FIXME: the user is hardcoded, need to get an actual session attrib
contextEvent.getServletCon