Dan
Dan

Reputation: 8513

Parameter not passing correctly in Java Servlet

I am trying to write a simple java servlet to list the files in a directory. the path is stored in init-param in web.xml. When I call getInitParameters(), it returns the directory path. But when I try to pass it to a handler, it returns null. Not sure what I am doing wrong. Any help?

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

import javax.servlet.ServletException;

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

interface Handler {
  public void doGet (HttpServletRequest request, HttpServletResponse response) 
    throws IOException; 
}

class DispatchChoice {
  public final String param; 
  public final GetHandler getHandler; 
  public DispatchChoice (String param, Handler getHandler) 
  {
    this.param = param;
    this.getHandler = getHandler;
  }
}

public class MyServlet extends HttpServlet
{
    String value;
    public void init() throws ServletException {
        value = getInitParameter("addressfile"); // correct value is saved here
        System.out.println("Init value : "+value);
    }
  DispatchChoice myChoice = new DispatchChoice("/test1", new FileHandler(value));

  public void doGet (HttpServletRequest request, HttpServletResponse response) 
    throws IOException
  {
        myChoice.getHandler.doGet(request, response);
  }
}

class FileHandler implements Handler {
    private String place;
    public FileHandler (String value){
        this.place = value; // this is NULL, not the value from above
        System.out.println("Param value : " + value);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        File directory = new File(place); //is NULL
        File[] files = directory.listFiles();
        PrintWriter pw = response.getWriter();
        for (int index = 0; index < files.length; index++) {
            pw.println(files[index].getName());
        }
    }
}

web.xml

<servlet>
<servlet-name>ListManagerServlet</servlet-name>
<servlet-class>savva.listmanagerservlet.ListManagerServlet</servlet-class>
<init-param>
    <param-name>addressfile</param-name>
    <param-value>d:\\temp\\</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ListManagerServlet</servlet-name>
<url-pattern>/ListManagerServlet</url-pattern>
</servlet-mapping>

Upvotes: 0

Views: 617

Answers (4)

Dan
Dan

Reputation: 8513

after some searching, i found that using getServletContext().get/set Attribute() is a better design than what i had before. but thanks for explaining my issue.

Upvotes: 0

Timo Hahn
Timo Hahn

Reputation: 2496

In your servlet you initialize the class menber

 DispatchChoice myChoice = new DispatchChoice("/test1", new FileHandler(value));

before the init() method initialize the value with hte path from the init parameter, so it's null.

You should implement it as

public void init() throws ServletException {
    value = getInitParameter("addressfile"); // correct value is saved here
    if (myChoice == null) {
        myChoice  = new DispatchChoice("/test1", new FileHandler(value))}
    }
    System.out.println("Init value : "+value);
}
DispatchChoice myChoice = null;

Upvotes: 1

SJuan76
SJuan76

Reputation: 24780

  DispatchChoice myChoice = new DispatchChoice("/test1", new FileHandler(value));

You are initializing myChoice when the instance is created, before init() is called, so value is still null.

Initialize it in init()

Upvotes: 1

Eng.Fouad
Eng.Fouad

Reputation: 117579

DispatchChoice myChoice = new DispatchChoice("/test1", new FileHandler(value));

This line is executed before init(), hence value is still null and not assigned yet! Instead, move the assignment inside init(), something like:

DispatchChoice myChoice;

public void init() throws ServletException
{
    value = getInitParameter("addressfile"); // correct value is saved here
    myChoice = new DispatchChoice("/test1", new FileHandler(value));
    System.out.println("Init value : "+value);
}

Upvotes: 2

Related Questions