Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:42:06+00:00 2026-06-13T01:42:06+00:00

I am trying to write a simple java servlet to list the files in

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:42:07+00:00Added an answer on June 13, 2026 at 1:42 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a simple Java function that will take a list
I am trying to write a simple java program which returns me all the
I'm trying to write a simple code in Java to connect to a memcache
I trying to write a simple function to call unmanaged code from managed code.
I'm trying to write a simple game in Java that uses Processing to render
I am trying to write some simple numerical code in Java where one can
I am trying to write a simple Http client application in Java and am
I'm trying to write a simple recursive function that look over list and return
I'm new to java and trying to write a simple program using JDBC. It
I'm new to java and am trying to write a simple program that basicly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.