Sunday, November 24, 2019

Can You Lot Declare Constructor Within Servlet Class?

Yes, Servlet tin conduct maintain Constructor, it's perfectly legal but it's not the correct agency to initialize your Servlet. You should purpose the init() method provided past times the Servlet interface to initialize the Servlet. If you lot remember, Servlet's are particular inward a feel that they are instantiated past times the container as well as managed past times the container. Influenza A virus subtype 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 you lot conduct maintain 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 render a constructor inward Servlet, but if you lot do, brand certain you lot besides add together a default constructor in that place for Servlet container.


Even though Servlet is the backbone of Java Web Application, many Programmer doesn't know commutation of Servlet e.g. who creates Servlet instance, life-cycle of Servlet instance, thread-safety as well as concurrency, Session administration etc. If you lot are only learning Servlet as well as JSP inward hotch potch manner, perhaps this is the fourth dimension to alternative upwardly a practiced mass similar Head First Servlet as well as JSP as well as read it from start to end. This is an former mass which is non yet updated to comprehend Servlet 3.0 as well as Asynchronous Servlet but all the same a practiced mass to larn fundamentals. Once yo acquire through this book, you lot tin response questions similar this past times yourself.

s are particular inward 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 inward 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 alone adds the default constructor inward whatever shape if in that place is no constructor specified, it volition non add together default constructor if you're Servlet shape already conduct maintain one. In this case, if Servlet container e.g. Tomcat tries to instantiate a Servlet inward guild to serve the outset asking from client, it volition throw the next exception:



HTTP Status 500 - Error instantiating servlet shape HelloServlet

javax.servlet.ServletException: Error instantiating servlet shape 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 endeavour this yourself, only do a Servlet without a default no-argument constructor. Though it's only a error if you lot are sure, you lot tin conduct maintain your constructor but retrieve Servlet container calls that. I suggest, you lot should purpose the init() method to initialize the Servlet because that's the measure way.

Further Learning
Java Web Fundamentals By Kevin Jones
Spring Framework 5: Beginner to Guru
JSP, Servlets as well as JDBC for Beginners: Build a Database App

>

No comments:

Post a Comment