Can we define constructor in Servlet class ?
- Yes, we can define constructor in Servlet class.
- Constructor may be a Default Constructor or Parameterised Constructor. If we didn’t write any constructor then the default constructor will be executed. Because, to read init parameters of a servlet we need servlet config object in the life cycle of the servlet.
- Servlet config object is created after the execution of the constructor.
Servlet Constructor
Sample Code
import javax.servlet.http.*;
import java.io.*;
public class ConstructorServlet extends HttpServlet
{
String str1, str2, str3;
public ConstructorServlet()
{
str1 = "Constructor";
}
public void init()
{
str2 = "init() method";
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
str3 = " service() method";
out.println("<h3>"+ str1 + "<br>" + str2 + "<br>" + str3 + "</h3>");
out.close();
}
}
Output
Constructor
init() method
service() method
Categorized in:
Tagged in:
Accenture interview questions and answers, Allstate Solution interview questions and answers, AT&T interview questions and answers, Atos interview questions and answers, can servlet class declare constructor with servletconfig object as a argument?, can we write parameterized constructor in servlet, Capgemini interview questions and answers, CASTING NETWORKS INDIA PVT LIMITED interview questions and answers, CGI Group Inc interview questions and answers, Ciena Corporation interview questions and answers, Collabera Technologies interview questions and answers, constructor in servlet java example, define constructor in servlet class, Dell International Services India Pvt Ltd interview questions and answers, difference between constructor and init method of servlet, Flipkart interview questions and answers, Genpact interview questions and answers, httpservlet, httpservlet class source code, httpservletrequest, IBM interview questions and answers, Indecomm Global Services interview questions and answers, Infogain interview questions and answers, init method in servlet, java servlet, java servlet example, jsp and servlet, jsp servlet, L&T Infotech interview questions and answers, Mphasis interview questions and answers, NetApp interview questions and answers, polaris financial technology interview questions and answers, Prokarma Softech Pvt Ltd interview questions and answers, R Systems interview questions and answers, RBS India Developmen interview questions and answers, SAP Labs India Pvt Ltd interview questions and answers, servlet, servlet apiservlet example, servlet container, servlet example, servlet filter, servlet init method, servlet initialization, servlet interface, servlet interview questions, servlet is java class then why there is no constructor in servlet, servlet life cycle, servlet mapping, servlet program, servlet session, servlet structure, servletcontext, servletrequest, Tech Mahindra interview questions and answers, UnitedHealth Group interview questions and answers, Virtusa Consulting Services Pvt Ltd interview questions and answers, webservlet, Wells Farg interview questions and answers, what is servlet, why do we need constructor in servlet even though we have a init () method, why no constructor in servletgenericservlet class, Wipro Infotech interview questions and answers, Wipro interview questions and answers, Xoriant Solution interview questions and answers, yugma interview questions and answers