wood burning stoves 2.0
The moose likes Servlets and the fly likes not able to get the context attribute set in ServletContextListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic

not able to get the context attribute set in ServletContextListener

john kerl

Joined: Jul 29, 2011
Posts: 13
Hi all,

I have written a simple ServletContextListener and set an attribute in it. when i am trying to retrieve the attribute value in my servlet, I am getting as null. Could anybody please let me know about this if i need to add any more changes.

Below is the complete code of my sample application



package com.mypackage;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyContextListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
public void contextInitialized(ServletContextEvent arg0) {
ServletContext context = arg0.getServletContext();
String testApplication = context.getInitParameter("testMe");
context.setAttribute("myTest", "mkmmkmmkm");


package com.mypackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response){
ServletContext context = getServletContext();
if(context != null){
try {
String output = (String) getServletContext().getAttribute("testMe");
PrintWriter out = response.getWriter();
out.print("The required value is... "+output);
} catch (IOException e) {
// TODO Auto-generated catch block




When accessed with any url as below:

gettting output as "The required value is... null" . Because i set the context attribute, I am expecting not null. Can anybody suggest me on this.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63540

Check your code again carefully. Carefully.

And please UseCodeTags.

[Asking smart questions] [About Bear] [Books by Bear]
john kerl

Joined: Jul 29, 2011
Posts: 13

Thankyou and Sorry, it is my fault. I didn't givt te correct attribute name.
I agree. Here's the link: http://aspose.com/file-tools
subject: not able to get the context attribute set in ServletContextListener
It's not a secret anymore!