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 828897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:47:52+00:00 2026-05-15T03:47:52+00:00

I’m kind of new in web development with Java. I am developing a web

  • 0

I’m kind of new in web development with Java.
I am developing a web service and I’ve chosen REST / Jersey for it.

I want to init some stuff on startup of the service and to keep them
all along the life of the service.

First question : Is the constructor of the Jersey Servlet a good place to do that ?

Basically, what I want to do is to load a config.ini file located in my WEB-INF directory.
Following this help, I understand I need a ServletContext to load my file as a resource.

However, it is not clear to me how to get this ServletContext in a Jersey Servlet, as it is not really an instance of a servlet, but rather a POJO with some annotations.
I wanted to try this tip, but the attribute “context” is null in the constructor. I think that Jersey might populate it after the constructor. Right ?

So how is the right way to do this ?

Here is my code so far :

/** Main REST servlet */
@Path("/")
public class Servlet {

    // ---------------------------------------------------- 
    // Constants                     
    // ---------------------------------------------------- 

    static private final String CONFIG_PATH = "/WEB-INF/config.ini";

    // ---------------------------------------------------- 
    // Attributes                     
    // ---------------------------------------------------- 

    /** Context */
    @Context ServletContext context;

    // ---------------------------------------------------- 
    // Constructor                     
    // ---------------------------------------------------- 

    /** Init the servlet */
    public Servlet() {

        // Load config.ini from WEB-INF
        Config.config = new Config(
                this.context.getResourceAsStream(CONFIG_PATH));

        // FAIL! this.context is null ...

    }

    // ---------------------------------------------------- 
    // URI Handlers                    
    // ---------------------------------------------------- 

    /** Welcome page */
    @GET
    @Path("/")
    @Produces(MediaType.TEXT_HTML)
    public String welcome() {
        return "<h1>Hi there.</h1>";
    }
}

Any help would be much appreciated.
Thanks in advance,

Raphael

  • 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-05-15T03:47:53+00:00Added an answer on May 15, 2026 at 3:47 am

    I am not familiar with Jersey, but generally in a Java web application, I think the right thing to do would be to create a ContextListener.

    A context listener is a class that implements the interface javax.servlet.ServletContextListener and is configured in your web.xml. It has a method that is executed when the application is first loded into your container, and another one that is executed when the application is stopped, so it is the ideal place to put some one-time initialization stuff, and clean-up things before the application is stopped.

    So the steps are :

    1. create your Listener class, implement the contextInitialized(ServletContextEvent sce) method. In this method you receive a ServeltContextEvent that has getServletContext() method that gives you access to the ServletContext.
    2. Configure your Listener in your web.xml

    You’ll find additional info here : tutorial

    Or on Sun’s, er, Oracle’s site.

    By the way, if your file will be in a JAR file I am not sure that the ServletContext method is the best way to load it. I think you’re better off with somethinbg like :

    this.getClass().getClassLoader().getResourceAsStream("com.company.my.file.properties");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 421k
  • Answers 421k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Error -9870 is eventParameterNotFoundErr, declared in CarbonEventsCore.h. The header comments… May 15, 2026 at 10:58 am
  • Editorial Team
    Editorial Team added an answer Yes! You can in fact do this in some flavors,… May 15, 2026 at 10:58 am
  • Editorial Team
    Editorial Team added an answer Yup, it was to do with the class declerations, you… May 15, 2026 at 10:58 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.