CodeWhisperer
CodeWhisperer

Reputation: 1195

ArrayLists in ServletContentListener

I'm new with Jsp, and start some testing with Servlets and Listeners. My question is why I get a HTTP Status 404 - /DynamicExample/ListenTest.do when i start the ListenTest.do?

What I want to do is to declare multiple Dog Objects and put this in a ArrayList. When all the objects are stored into the ArrayList, then the "MyServletContextListener.java" whill set the attribute "dog". When that is done, will I send the attribute to my index.jsp page, but than get I a 404 file not fount.

ListenTester.java

package com.app.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.app.model.Dog;

public class ListenerTester extends HttpServlet {

    public void doGet (HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Dog dog = (Dog) getServletContext().getAttribute("dog");
        RequestDispatcher forwardToLoginPage = req.getRequestDispatcher("index.jsp");
        forwardToLoginPage.forward(req, resp);
    }
}

MyServletContextListener.java

package com.app.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.app.model.Dog;

public class MyServletContextListener implements ServletContextListener {

    ArrayList<Dog> allDogs;

    public void contextInitialized(ServletContextEvent event) { 
        if (allDogs == null) { allDogs = new ArrayList<Dog>(); } 
        allDogs = (ArrayList<Dog>) event.getServletContext().getAttribute("dog");

        Dog d1 = new Dog("SomeThing");
        Dog d2 = new Dog("someThing");
        allDogs.add(d1);
        allDogs.add(d2);
        event.getServletContext().setAttribute("dog", allDogs);
    }

    public void contextDestroyed(ServletContextEvent event) { 
         // nothing to do here
    }
}

Upvotes: 0

Views: 161

Answers (1)

Braj
Braj

Reputation: 46841

ArrayList<Dog> can't be cast to Dog


You are setting it in ServletContext as shown below

allDogs = new ArrayList<Dog>();
...
event.getServletContext().setAttribute("dog", allDogs);

Now you are retrieving it as shown below

Dog dog = (Dog) getServletContext().getAttribute("dog");//the problem is here

It should be

ArrayList<Dog> dogs = (ArrayList<Dog>) getServletContext().getAttribute("dog");

Do it in this way to avoid NullPointerException

public void contextInitialized(ServletContextEvent event) { 
    //if (allDogs == null) { allDogs = new ArrayList<Dog>(); } //move this line below
    allDogs = (ArrayList<Dog>) event.getServletContext().getAttribute("dog");
    if (allDogs == null) { allDogs = new ArrayList<Dog>(); } 
    ...
}

No need to set the "dog" attribute in request because it is already added as session attribute.

Sample code for index.jsp:

<%@ page import="java.util.ArrayList,com.x.y.z.Dog" %>
<%
    ArrayList<Dog> dogs = (ArrayList<Dog>) session.getAttribute("dog"); 
    for(Dog dog:dogs){
        out.println(dog.getName());
    }
%> 

Note: use JavaServer Pages Standard Tag Library instead of Scriplets.

Upvotes: 2

Related Questions