1

I have to set initialization parameters based on environment to a servlet, NOT via web.xml but via code, but my servlet version is not 3.0 , so I cannot use this http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#setInitParameter%28java.lang.String,%20java.lang.String%29

I dont have access to the servlet code so I am writing a new servlet that extends it and I want to add initialization parameters via java code ..Any suggestions?

<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>abc</servlet-class>
<init-param>
<param-name>abc</param-name>
<param-value>localhost:2001</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Is there a way to do the above thing(adding init-params) by extending servlet abc and setting attributes to the servlet config (overriding init())?

3
  • Could you be more specific? A ServletFilter wrapping the servlet would be a solution. Commented Dec 11, 2012 at 10:32
  • @mjamal14 - Can you subclass or decorate the servlet you can't edit? Commented Dec 11, 2012 at 10:49
  • Yes I can subclass.I am writing a new servlet that extends servlet abc, but I cant figure out how to pass the init params, when I override the init() method Commented Dec 11, 2012 at 10:53

2 Answers 2

4

I overrode the method getInitParameter of GenericServlet and I was able to solve my problem..

@Override
public String getInitParameter(String name) {

//Get initparams here
    return "MyInitParams";
}
Sign up to request clarification or add additional context in comments.

Comments

1

See if following code helps you

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloWorld
 */
public class HelloWorld extends HttpServlet {
    private static final long serialVersionUID = 1L;

    ServletContext context;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorld() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void init(ServletConfig config) throws ServletException {
        // Do required initialization
        Properties prop = new Properties();
        try {
            prop.load(getServletContext().getResourceAsStream(
                    "/WEB-INF/properties/sample.properties"));
            context.setAttribute("abc", prop.getProperty("abc"));
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println(e);
        }
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.