| Author |
How to acess servlet context variables from java class
|
chinna kumar
Greenhorn
Joined: Sep 29, 2004
Posts: 18
|
|
This is my servlet::::: ServletContext context=getServletContext(); context.setAttribute("Imsi",imsi); context.setAttribute("Msisdn",msisdn); context.setAttribute("Sim",sim); now we need to access these variables from java class file raher than jsp file. cud anyone help me out??? Chinna
|
 |
Serghei Jelauc
Ranch Hand
Joined: Jul 24, 2002
Posts: 128
|
|
Why do not you declare a ServletContext instance variable and create the setter method for it in your java class for example? Something like this: public void setContext(ServletContext context) { this.context = context; } After that you can use the context as usual: context.getAttribute("Sim");
|
SCJP 1.4 <br />SCBCD 1.3<br />SCWCD 1.4
|
 |
chinna kumar
Greenhorn
Joined: Sep 29, 2004
Posts: 18
|
|
Hi, i tried this but this does not work. this is the modification i have done ServletContext context; now am trying access value using: context.getAttribute("Sim"); it says context needs to be initialized, for this is it possible to intialize without setter method as ServletContext context=getServletContext(); but this too does work.......... please help me out... Chinna
|
 |
Neeraj Dheer
Ranch Hand
Joined: Mar 30, 2005
Posts: 225
|
|
yes, you can always do a context = getServletContext() without any issues. just curious : have you over-ridden the init() method of the servlet, if yes, then have you called super.init(config) in it?
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
I prefer that my beans are container unaware. If I needed to do something like this, I would put all the variables that need to be shared into one object and bind that to context scope. then I would pass a reference to that object to any beans that need it's properties.
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
 |
|
|
subject: How to acess servlet context variables from java class
|
|
|