Can You Declare Constructor inside Servlet Class? 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.



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 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
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:662)

Can You Declare Constructor inside Servlet Class? Answer




Root cause:

java.lang.InstantiationException: HelloServlet
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1149)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

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.

3 comments:

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

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

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

      Delete

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