Can You Declare Constructor inside Servlet Class?

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 a 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 e.g. HelloServlet(String name) than 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 Servlet container.

Even though Servlet is the backbone of Java Web Application, many Programmer doesn't know fundamental of Servlet e.g. 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 hotch potch 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 which is not yet updated to cover Servlet 3.0 and Asynchronous Servlet but still a good book to learn fundamentals. Once yo go through this book, you can answer questions like this by yourself.

Best book to learn Servlet and JSP

java.lang.InstantiationException in Servlet

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 default constructor if you're Servlet class already have one. In this case, if Servlet container e.g. Tomcat tries to instantiate a Servlet in order to serve the first request from client, it will throw the following exception:

HTTP Status 500 - Error instantiating servlet class HelloServlet

javax.servlet.ServletException: Error instantiating servlet class HelloServlet

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.