Results 1 to 3 of 3
  1. #1
    JKoder70014 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default not able to get the context attribute set in ServletContextListener

    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

    web.xml
    -----------
    <web-app>
    <servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>com.mypackage.MyServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <listener>
    <listener-class>
    com.mypackage.MyContextListener
    </listener-class>
    </listener>
    <context-param>
    <param-name>testMe</param-name>
    <param-value>TestedSuccessfully</param-value>
    </context-param>
    </web-app>

    ContextListener
    ---------------------

    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");
    }
    }


    Servlet
    ----------

    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){
    response.setContentType("text/html");
    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
    e.printStackTrace();
    }

    }
    }

    }


    OUTPUT
    ---------------------------------------------------

    When accessed with any url as below:
    http://localhost:8080/helloworld/u

    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.
    Last edited by JKoder70014; 08-03-2011 at 07:41 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Java Code:
    context.setAttribute("[U]myTest[/U]", "mkmmkmmkm");
    
    getServletContext().getAttribute("[U]testMe[/U]");
    You're not retrieving the same attribute name...

  3. #3
    JKoder70014 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Hi,

    Thankyou and Sorry, it is my fault. I didn't givt te correct attribute name

Similar Threads

  1. Undefined attribute name
    By Dieter in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-03-2010, 10:44 AM
  2. Sending Response From ServletContextListener
    By mitesh1984_agrawal in forum Java Servlet
    Replies: 4
    Last Post: 12-03-2009, 08:59 AM
  3. how to read an attribute of an xml tag in jsp.
    By himacherla in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-21-2009, 06:27 AM
  4. how to add an attribute with sax?
    By cecily in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 04:09 AM
  5. Problem with Attribute in JSP
    By Albert in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-13-2007, 03:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •