Can You Declare Constructor within Servlet Class?

Yes, Servlet tin forcefulness out direct keep Constructor, it's perfectly legal but it's not the correct means to initialize your Servlet. You should purpose the init() method provided past times the Servlet interface to initialize the Servlet. If yous remember, Servlet's are particular inwards a feel that they are instantiated past times the container as well as managed past times the container. H5N1 servlet container similar Tomcat creates a puddle of multiple Servlets to serve multiple clients at the same time. They instantiate Servlet past times calling the default no-argument constructor as well as suppose yous direct keep declared unopen to other constructor which takes a parameter e.g. HelloServlet(String name) than Java compiler volition non add together the default no-argument constructor as well as Servlet container volition non able to initialize the Servlet. That's why it's of import non to furnish a constructor inwards Servlet, but if yous do, brand certain yous likewise add together a default constructor at that spot for Servlet container.


Even though Servlet is the backbone of Java Web Application, many Programmer doesn't know primal of Servlet e.g. who creates Servlet instance, life-cycle of Servlet instance, thread-safety as well as concurrency, Session administration etc. If yous are merely learning Servlet as well as JSP inwards hotch potch manner, perhaps this is the fourth dimension to alternative upwards a expert mass similar Head First Servlet as well as JSP as well as read it from start to end. This is an quondam mass which is non yet updated to comprehend Servlet 3.0 as well as Asynchronous Servlet but nonetheless a expert mass to larn fundamentals. Once yo acquire through this book, yous tin forcefulness out respond questions similar this past times yourself.

s are particular inwards a feel that they are instantiated past times the container as well as managed past times the co Can You Declare Constructor within Servlet Class?



java.lang.InstantiationException inwards Servlet

Sometimes, when developer declares constructor on Servlet they add together a parameterized constructor i.e. a Servlet constructor which accepts a parameter. Since coffee solely adds the default constructor inwards whatever course of report if at that spot is no constructor specified, it volition non add together default constructor if you're Servlet course of report already direct keep one. In this case, if Servlet container e.g. Tomcat tries to instantiate a Servlet inwards social club to serve the kickoff asking from client, it volition throw the next exception:


HTTP Status 500 - Error instantiating servlet course of report HelloServlet

javax.servlet.ServletException: Error instantiating servlet course of report 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)


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 tin forcefulness out elbow grease this yourself, merely exercise a Servlet without a default no-argument constructor. Though it's merely a error if yous are sure, yous tin forcefulness out direct keep your constructor but recollect Servlet container calls that. I suggest, yous should purpose the init() method to initialize the Servlet because that's the criterion way.

Further Learning
Java Web Fundamentals By Kevin Jones
Introduction to Spring MVC
Head First Servlet as well as JSP


Subscribe to receive free email updates:

0 Response to "Can You Declare Constructor within Servlet Class?"

Posting Komentar