Best Answer

Yes you can but it is not required. A Servlet is nothing but another .java file and all rules that are applicable to standard Java classes are applicable to them.

Note: Even if you write a constructor in a servlet, it will not get executed.

User Avatar

Wiki User

โˆ™ 2010-02-24 02:26:15
This answer is:
User Avatar
Study guides

Add your answer:

Earn +20 pts
Q: Can you use constructor in servlet?
Write your answer...
Still have questions?
magnify glass
Related questions

Why servlet has no constructor?

Because it is not a regular Java Class that is executed by a JVM. It is a special Java class that is executed by the Web Container which initializes and loads the servlet and the service methods get executed everytime it receives a request.

What is use of constructor in java?

Constructor is used to do something (written in constructor) immediately after object creation.

Which situation constructor is used?

Constructor is necessary when you are about to use instance of a class.

Why you use constructor overloading?

When we are initializing our object with different internal state then we can use the constructor overloading.

How do you create object of javabeen in servlet?

A JavaBean is nothing but a regular Java Class and hence it can be instantiated using the new keyword and invoking the bean constructor

When a object is passed to a function as argument why it is not generating an error if the parameterized constructor is defined and not the default constructor?

default constructor is used only when the programmer does not use a constructor to initialize objects. Once the programmer defines a constructor then the default constructor is no longer used

Can you rewrite the default constructor?

You are free to override the default constructor whenever you like. In fact, if you want to keep the default constructor when you also need a parameterized constructor, you have to override the default constructor in order to use it. Once you've written the parameterized constructor, the default constructor goes away.

What is use of private constructor?

A Private constructor is simply a constructor that can only be used within the class it was declared in. By default java creates you a constructor which is public though. Hope that helps

Why you use constructor instead of function?

A constructor is simply a function that is called when a new object is created.

What is the use of Servlet in java?

Java Servlet is used for Server Side programming for developing Web Applications. It easily employs Database Connectivity. We can also use JSP however it cannot replace a Java Servlet.

What are types of servlet?

Http servlet and Generic servlet

Use of constructor?

to create an instance of object

People also asked