When trying to set Context attributes like so:
void init()
{
String testing = new String();
testing = "This is a test";
getServletContext().setAttribute("test", testing);
}
In one servlet, and getting the attribute like so:
String testing = (String) getServletContext().getAttribute("test")
In a second servlet, testing is null.
Does this mean my servlets are in separate contexts? If so, how could I access the context attributes of the first servlet? Please provide a reference for this as I am relatively new to java/servlets.
I am using Netbeans with Glassfish 3.
EDIT: They are both in the same webapp and are both defined in the same WEB-INF/web.xml
If both servlets are in the same webapp, by default the order of initialization is undefined. So it may be, your "second" servlet is initialized before the "first" (according to the order in the web.xml). You may fix it by adding a load-on-startup tag to the servlet tag:
<servlet>
<servlet-name>first<servlet-name>
...
<load-on-startup>1<load-on-startup>
</servlet>
<servlet>
<servlet-name>second<servlet-name>
...
<load-on-startup>2<load-on-startup>
</servlet>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With