0

I am writing a Servlet that retrieves request parameters but needs to use them in the init method. Since the init method would be called before the doGet method and since the init method does not have a HttpServletRequest object as an argument, I don't know how to get the request parameters.

public class OpenIdServlet extends HttpServlet 
{
    ...
    ...
    private OpenIdManager manager;

    @Override
    public void init() throws ServletException 
    {
        super.init();
        manager = new OpenIdManager();
        manager.setRealm("http://localhost:8080/OpenIDSample"); 
        manager.setReturnTo("http://localhost:8080/OpenIDSample/openid"); //I need to append the value of a request parameter here...
    }
    ...
    ...
}

So the question is: is there any way I can get HttpServletRequest parameters in the init() method of a servlet? If no what other better approach can one use to solve this problem? Thanks.

2
  • Explain what library OpenIdManager comes from.
    – McDowell
    Commented Jan 12, 2014 at 20:44
  • @McDowell it comes from JOpenID-1.08
    – Jevison7x
    Commented Jan 12, 2014 at 21:57

2 Answers 2

3

The init is called once on startup. So you have to live with a partial returnTo, and on request handling (doGet/doPost) complete it with the request parameters.

In fact the manager seems to be request dependent and hence should be created in the request - never as field. As there might be several requests. Maybe persisting as session attribute.

4
  • 1
    was adding a comment saying the same, +1 ;)
    – user180100
    Commented Jan 12, 2014 at 20:29
  • @Joop Eggen: But the manager refuses to work when I declared it in a doGet() method. Are you familiar with this OpenID technology?
    – Jevison7x
    Commented Jan 12, 2014 at 20:33
  • Known technology, though did not use the jOpenId library. If needed in init, you could give a fixed URL, and on calling getAuthentication in doGet/doPost call setUserPrincipal or set some session attribute. The returnTo would then redirect.
    – Joop Eggen
    Commented Jan 12, 2014 at 21:15
  • There are query strings that must be appended to the URL and they can only come from a request parameter. It seems there is no way to get them, I am working on another way though.
    – Jevison7x
    Commented Jan 12, 2014 at 21:55
1

Your question doesn't make sense. There is no request when the init() method is called. It is called during container initialisation.

Do you mean initial parameter? They are available via the ServletContext provided as an argument to init().

Not the answer you're looking for? Browse other questions tagged or ask your own question.