Defining Constructor In Servlets

Discussions

Web tier: servlets, JSP, Web frameworks: Defining Constructor In Servlets

  1. Defining Constructor In Servlets (5 messages)

    Hi,
      Can we define Constructors in servlets and if we can then
      how can we call it?
      one more thing is can we define methods in Jsp file..
     Regards,
     Chandu.

    Threaded Messages (5)

  2. Defining Constructor In Servlets[ Go to top ]

    You can define methods in JSP, the same way as u declare variables.
    <%!..%>
  3. Defining Constructor In Servlets[ Go to top ]

    Probably you don't need that. Why do you want to do this?
  4. Defining Constructor In Servlets[ Go to top ]

    If you want to define a constructor in a servlet, you can only define a no-parameter constructor. In general, most developers don't do this. Typically, they put construction-type logic in the servlet's init() method instead.
  5. Defining Constructor In Servlets[ Go to top ]

    You can't define constructors for your servlet since the servlets are initialized by the container. Would give you compilation error.
  6. Constructor in servlets[ Go to top ]

    One can definitely have constructor in servlet.Even you can user the constrctor in servlet for initialization purpose,but this type of approch is not so common.You can perform common operations with the constructor as you normally do.The only thing is that you cannot call that constructor explicitly by the new keyword as we normally do.In the case of servlet, servlet container is responsible for instentiating the servlet, so the constructor is also called by servlet container only.