
how to write a constructor in Java Servlet?

Servlet is like a POJO .You can create constructor in servlet. You can also use constructor for initialising purpose but it is not a useful approach as you have init() in Servlet life cycle for initialisation.
package roseindia;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletConstructor extends HttpServlet {
public ServletConstructor() {
System.out.println("Its a servlet constructor");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hello servlet");
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.