Can we define constructor in Servlet class?

Answers were Sorted based on User's Feedback



Can we define constructor in Servlet class?..

Answer / santhosh kumar

Yes, we can define constructor in servlet.

Is This Answer Correct ?    28 Yes 4 No

Can we define constructor in Servlet class?..

Answer / sitaram

in java, we can access any class, first JVM search the any
type(with or with out parameters) constructor. if it their
the same constructor will be calling other wise JVM create
the default constructor for that class.

Is This Answer Correct ?    9 Yes 1 No

Can we define constructor in Servlet class?..

Answer / avinash

yes,wecan

Is This Answer Correct ?    6 Yes 0 No

Can we define constructor in Servlet class?..

Answer / naveen

yes we can create a constructor
by default JVM calls default constructor.In addition to that if write both default and parameterized constructor then JVM call default one and then parameterized one

hence the ans is yes we hav to write two constuctors
no if write our own constructor one itself

Is This Answer Correct ?    5 Yes 2 No

Can we define constructor in Servlet class?..

Answer / jayakrishna

init() is there what is the need of constructor.If is ther
who will call the constructor? servlet container will call
the init method

Is This Answer Correct ?    6 Yes 4 No

Can we define constructor in Servlet class?..

Answer / karnakar thallapalli

we can create default constructor and parameterised constructor.my servlet will anyways run and works fine.this question basically test our knowledge on servlet life cycle and how the container instantiate our servlet.but in real time i never recomend this .u may still use this concept in very exceptional cases.
below code

package com.oracle.scwcdtest;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public MyServlet() {
super();
System.out.println("in default constructor");
}
public MyServlet(String test) {
super();
System.out.println("param constructor");
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {

}

}

Is This Answer Correct ?    4 Yes 2 No

Can we define constructor in Servlet class?..

Answer / komal singh adhikari

YES,
we can initialized the default constructor.

but we cannot call the parameterized one.

Is This Answer Correct ?    4 Yes 4 No

Can we define constructor in Servlet class?..

Answer / srikanth b

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 object is
created after the execution of the constructor.

Is This Answer Correct ?    1 Yes 3 No

Can we define constructor in Servlet class?..

Answer / sanjeev kumar

We never can create the Servlet instance. hence there is no
chance of having constructor in servlets. Servlet life
cycle methods (init(), service(), destoy())are called by
the servlet container.

Is This Answer Correct ?    4 Yes 8 No

Can we define constructor in Servlet class?..

Answer / amit tomar

we can't define constructor in Servlet class

Is This Answer Correct ?    3 Yes 14 No

Post New Answer

More Servlets Interview Questions

What are different types of SessionTracking?

2 Answers  


What are cookies and how will you use them?

2 Answers  


can we create more than ServletContext and ServletConfig in your application

6 Answers   Patni,


how can we execute servelt? what the use ".war" or ".jar" file creation

3 Answers   CTS,


How ThreadSafe page attribute will be working in Servlet as well as in JSP?Automatically ThresdSafe is true in JSP so service method will be destroy in each and every request or not?so how thresd will handle srevice method?

1 Answers   AZTEC,






Explain the custom jsp tags and the beans.

0 Answers  


Difference between get and post in java servlets?

0 Answers  


How do you create a cookie using servlet?

0 Answers  


What are the uses of servlets?

0 Answers  


When a servlet accepts a call from a client, it receives two objects. What are they?

0 Answers  


What are the uses of servlet

0 Answers  


What is pure servlet?

0 Answers  


Categories