in Education by
Can I declare parameterized constructor inside servlet which is only constructor ? If no then why ? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
No. Servlet instances are created by the container via reflection, and they expect to find a public, no-arg constructor (the default constructor). To configure your servlet, use servlet parameters specified in the web.xml file. These are passed to your servlet's init() method. While it would be possible for a servlet container to choose a non-default constructor and coerce character strings to simple types and invoke the constructor reflectively, this isn't what the Servlet specification requires. Part of the reason may be historical; servlets were first specified long before dependency injection systems made this alternative widely practiced. However, such constructors would be fairly limited; it would be practical to pass arguments that can be created from a simple character string specified in the web.xml, but more useful objects—a DataSource, for example—would be awkward. It would be nice to have final member variables in a servlet though. The JSR formerly known as "WebBeans" (JSR 299, I think), will provide some standards for dependency injection support in Servlets. This might address some of the drawbacks in the current approach.

Related questions

0 votes
    I am wondering, how can I set in the web.xml a Filter that is called on every request? JavaScript ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    What would be the behaviour if one parameterized constructor is explicitly defined? (a) Compilation error (b) ... questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    How constructor can be used for a servlet? (a) Initialization (b) Constructor function (c) Initialization and ... Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    Can servlet class declare constructor with ServletConfig object as an argument? (a) True (b) False I have been ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    How constructor can be used for a servlet? (a) Initialization (b) Constructor function (c) Initialization ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
0 votes
    Which of the following is an incorrect statement regarding the use of generics and parameterized types in Java? (a ... Packages of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which of these Exception handlers cannot be type parameterized? (a) catch (b) throw (c) throws (d ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    Parameterized stored procedures are compiled after the user input is added. (1)True (2)False...
asked May 17, 2021 in General by JackTerrance
0 votes
    What is Parameterized constructors in C#?...
asked Jan 1, 2021 in Technology by JackTerrance
0 votes
    I am working on SAML SSO Authentication. I have created a servlet to generate SAML metadata and i deployed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I am working on SAML SSO Authentication. I have created a servlet to generate SAML metadata and i deployed ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    Servlet are used to program which component in a web application? (a) client (b) server (c) tomcat (d) ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    How does applet and servlet communicate? (a) HTTP (b) HTTPS (c) FTP (d) HTTP Tunneling I have been ... Technologies & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
...