Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Servlet Context Listener Problem

843842Oct 8 2008 — edited Oct 14 2008
Hi Guys,

Im having a bit of trouble with the servlet context listener. everywhere i have read the implementation of a context listener is approached the same way as i have however my code seems to report:

java.lang.NullPointerException
DisplayNodes.doPost(DisplayNodes.java:29)
DisplayNodes.doGet(DisplayNodes.java:14)

which says that the linked list i am using is not populated. I have followed this through and it appears the thread i am starting on the context initialisation is not starting.

Can anyone see what i am doing wrong ?

Thanks in advance

Ron

Heres my code:
import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;

public class Serial extends Thread {

	private LinkedList<Plug>		  plugList = new LinkedList<Plug>();
	
	public synchronized void addtoplugList(Plug plug){
		//add to front of list so that other iterating threads will not cause race
		//hazards if accessing at the same time.
		plugList.addFirst(plug);
	}
	
	//plugList accessor
	public synchronized LinkedList<Plug> getplugList(){return plugList;}

/*----------------------------------------------------------------------------*/	
    public void run () {
    
    	//create a new plug instance
    	Plug plug = new Plug();
    	plug.setaddress64("64bitaddress");
    	//add it to the pluglist
    	addtoplugList(plug);

	}//end run
/*----------------------------------------------------------------------------*/
} //end class Serial
public class Plug {
/*
	This class represents a plug
*/
	
	private String address64;

	//address64 mutator
	public synchronized void setaddress64(String address64_){
		address64 = address64_;
	}
	
	//address64 accessor
	public synchronized String getaddress64(){return address64;}
	
}//end Plug
import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public final class PlugListener
    implements ServletContextListener {
/*
This class acts as a listener for the ServletContext. When the servlet context
is started an instance of Serial is created.

When destroyed the Serial thread is stopped cleanly.
*/

  private ServletContext context = null;

  public PlugListener() {}

  private Serial newSerial;

  public LinkedList<Plug> getPlugList() {
       return this.newSerial.getplugList();
  }

  public void contextDestroyed(ServletContextEvent event){
    this.context = null;
  }

  public void contextInitialized(ServletContextEvent event)
  {
    this.context = event.getServletContext();
    //start thread
    System.out.println("the context has started");
    this.newSerial = new Serial();
    this.newSerial.start();
    //add this class to the context
    this.context.setAttribute("PlugListener", this);

  }
}
import java.io.*;
import java.util.*;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;
import javax.servlet.*;
import javax.servlet.http.*;


public class DisplayNodes extends HttpServlet {
        private final static long serialVersionUID = 1L;
   
    public void doGet( HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
                doPost(request, response);
    }//end doGet

    public void doPost(HttpServletRequest request,
                                HttpServletResponse res)throws ServletException, IOException{

                res.setContentType("text/html");
                PrintWriter out = res.getWriter();

        out.println("<h1>Intelligent Plugs</h1>");
        out.println("<font size=2> Devices in this list are respresentative of the plugs found.");
        out.println("Red buttons represent plugs that are off and green are those that are on");

        PlugListener plugListener = (PlugListener)getServletContext().getAttribute("plugListener");
        
        LinkedList<Plug> plugList = plugListener.getPlugList();

        Iterator i1 = plugList.iterator();

        //check to see if there are any plugs
                if (plugList.size() == 0){out.println("no plugs found");return;}

                //Search all nodes adding each node as a button object.
                while (i1.hasNext()) {
                        Plug plug = (Plug)i1.next();
                        out.print(plug.getaddress64());
                }//end while (i1.hasNext())
          out.flush();
        }//end doPost

}//end DisplayNodes
and here is my web.xml file
<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

   <listener>
        <listener-class>
          PlugListener
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>Display</servlet-name>
        <servlet-class>DisplayNodes</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Display</servlet-name>
        <url-pattern>/display</url-pattern>
    </servlet-mapping>

 </web-app>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 8 2008
Added on Oct 8 2008
17 comments
690 views