Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Can You declare Constructor inside Servlet Class in Java? Answer

Yes, Servlet can have Constructor, it's perfectly legal but it's not the right way to initialize your Servlet. You should use the init() method provided by the Servlet interface to initialize the Servlet. If you remember, Servlet's are special in the sense that they are instantiated by the container and managed by the container. A servlet container like Tomcat creates a pool of multiple Servlets to serve multiple clients at the same time.

They instantiate Servlet by calling the default no-argument constructor and suppose you have declared another constructor which takes a parameter like  HelloServlet(String name) then Java compiler will not add the default no-argument constructor and Servlet container will not able to initialize the Servlet.

 That's why it's important not to provide a constructor in Servlet, but if you do, make sure you also add a default constructor there for the Servlet container.

Even though Servlet is the backbone of Java Web Application, many Programmer doesn't know fundamental of Servlet like who creates Servlet instance, life-cycle of Servlet instance, thread-safety and concurrency, Session management, etc.

If you are just learning Servlet and JSP in a hotchpotch manner, maybe this is the time to pick up a good book like Head First Servlet and JSP and read it from start to end. This is an old book that is not yet updated to cover Servlet 3.0 and Asynchronous Servlet but still a good book to learn fundamentals. Once you go through this book, you can answer questions like this by yourself.

No, it will throw java.lang.InstantiationException in Servlet if you put parameterized constructor

Sometimes, when developer declares constructor on Servlet they add a parameterized constructor i.e. a Servlet constructor which accepts a parameter. Since java only adds the default constructor in any class if there is no constructor specified, it will not add a default constructor if you're Servlet class already have one. I

In this case, if a Servlet container like Tomcat tries to instantiate a Servlet in order to serve the first request from a client, it will throw the following exception:

HTTP Status 500 - Error instantiating servlet class HelloServlet

javax.servlet.ServletException: Error instantiating servlet class HelloServlet

Can You Declare Constructor inside Servlet Class? Answer

Root cause:

java.lang.InstantiationException: HelloServlet
at java.lang.Class.newInstance0(
at java.lang.Class.newInstance(
at org.apache.catalina.core.StandardWrapper.loadServlet(
at org.apache.catalina.core.StandardWrapper.allocate(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.Http11Processor.process(
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(

You can try this yourself, just create a Servlet without a default no-argument constructor. Though it's just a mistake if you are sure, you can have your constructor but remember Servlet container calls that. I suggest you should use the init() method to initialize the Servlet because that's the standard way.


  1. but for me it worked.I tried on tomcat 8.

    1. Hello Deepak, Can you please post your code here?

    2. It should not work. It will be good if we can share your working code.


Feel free to comment, ask questions if you have any doubt.