ServletContextAttributeListener example

Hi,

I was trying 'ServletContextAttributeListener  example'

It simply printing on the console attribute is added replaced, removed etc but not saying exactly which attribute. I am adding 2 different attributes. But my console is not saying attribute1, attribute2 etc.
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ServletListener implements ServletContextListener,
		ServletContextAttributeListener {

	public void contextInitialized(ServletContextEvent arg0) {

		System.out.println("Context has been Initialized");
	}

	public void contextDestroyed(ServletContextEvent arg0) {

		System.out.println("Context has been Destroyed");
	}

	public void attributeAdded(ServletContextAttributeEvent arg0) {
		System.out.println("Attribute has been Added");

	}

	public void attributeRemoved(ServletContextAttributeEvent arg0) {

		System.out.println("Attribute has been Removed");

	}

	public void attributeReplaced(ServletContextAttributeEvent arg0) {
		System.out.println("Attribute has been Replaced");

	}

}






import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ServletListenerDemo extends HttpServlet {
	public ServletListenerDemo() {

		System.out.println("ServletListenerDemo ----");
	}

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		res.setContentType("text/html");
		ServletContext sctx = getServletContext();

		sctx.setAttribute("COUNTRY", "USA");
		sctx.setAttribute("COUNTRY", "UK");
		sctx.setAttribute("STATE", "TX");
		sctx.removeAttribute("COUNTRY");

	}
}




web.xml entry

 <listener>
    <listener-class>xyz.ServletListener</listener-class>
  </listener>

Open in new window

Please advise.

Any links resources ideas highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
You need to use the getValue method if you want to read the old value, or the getAttribute method of the session if you want to read the new value.
0
gudii9Author Commented:
I want to print attribute 1='USA'
attribute 2='UK'

Not like old attribute 'UK'

New attribute 'tx'


As of now it prints like

attribute added as USA
attribute modified to UK
attribute added TX




please advise
0
girionisCommented:
You need to use the getAttribute method then.
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

gudii9Author Commented:
let me try


when i change program like

package com.test;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ServletListenerDemo extends HttpServlet {
	public ServletListenerDemo() {

		System.out.println("ServletListenerDemo ----");
	}

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		res.setContentType("text/html");
		ServletContext sctx = getServletContext();
		ServletContext sctx2 = getServletContext();

		sctx.setAttribute("COUNTRY", "USA");
		sctx2.setAttribute("COUNTRY", "UK");
		sctx.setAttribute("STATE", "TX");
		sctx.removeAttribute("COUNTRY");

	}
}
instead of


package com.test;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ServletListenerDemo extends HttpServlet {
	public ServletListenerDemo() {

		System.out.println("ServletListenerDemo ----");
	}

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		res.setContentType("text/html");
		ServletContext sctx = getServletContext();
		//ServletContext sctx2 = getServletContext();

		sctx.setAttribute("COUNTRY", "USA");
		sctx.setAttribute("COUNTRY", "UK");
		sctx.setAttribute("STATE", "TX");
		sctx.removeAttribute("COUNTRY");

	}
}

Open in new window


nothing is printing to console. Cannot i have two servlet contexts in one program. Please advise
0
girionisCommented:
I am lost now. What are you trying to do?
0
gudii9Author Commented:
>>ServletContext sctx = getServletContext();
>>ServletContext sctx2 = getServletContext();


 Cannot i have two servlet contexts in one program. Please advise
0
girionisCommented:
You can only have one context per web application per JVM.

ServletContext sctx = getServletContext();
ServletContext sctx2 = getServletContext();

Open in new window

Here you have one servlet context, and two variables that point to the same servlet context.
0
gudii9Author Commented:
>>Here you have one servlet context, and two variables that point to the same servlet context.

can i have two variable instances pointing to same servlet context like above?. please advise
0
girionisCommented:
Yes (but it's pointless).
0
gudii9Author Commented:
>>>Yes (but it's pointless).

I wonder why it is pointless?
cannot i do two different operations using two different servlet context object instances??
0
girionisCommented:
If you want two diffeent servlet contexts you need to have two different web apps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.