C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
javax.servlet.Servlet interface
WhatsApp
Satyapriya Nayak
5y
5.5
k
0
1
25
Blog
Introduction
The javax.servlet.Servlet interface defines the three methods as
public
void
init(ServletConfig config)
public
void
service( ServletRequest req, ServletResponse res)
public
void
destroy()
Whenever a servlet gets called by the first user then the servlet container creates a new instance of the servlet class.The servlet container creates a new thread for each user to call service() method,doGet() method ,doPost() method.All users use a common instance of the servlet class. If the service has no user to access it and memory of the servlet container gets full then the instance of the servlet gets destroy and before to it destroy method executes. If servlet container gets closed then all existing instances of the servlet get destroyed and before to it the destroy() method executes.
Can a servlet contain constructor?
A servlet class can contain any type of constructors. The servlet container calls only the default constructor of the servlet class. The parameterized constructor cannot be called by the servlet container but these can be called by the default constructor by using this keyword.
Program
import
javax.servlet.*;
import
javax.servlet.http.*;
import
java.io.*;
/* Execute this in tomcat 4.1 to get the o/p in console window */
public
class
lifecycle
extends
HttpServlet
{
public
lifecycle(
int
x)
{
System.out.println(
"from constructor"
);
}
public
void
init()
{
System.out.println(
"Servlet Started Its Execution"
);
}
public
void
doGet(HttpServletRequest req,HttpServletResponse res)
throws
IOException,ServletException
{
res.setContentType(
"text/html"
);
Thread th=Thread.currentThread();
System.out.println(
"new user arrived.."
);
PrintWriter out=res.getWriter();
out.println(
"<html><body bgcolor='pink'>"
);
out.println(
"<h1 align='center'>Yur thread name is : "
+th.getName()+
"</h1>"
);
out.println(
"</body></html>"
);
}
public
void
destroy()
{
System.out.println(
"servlet gets out..."
);
}
}
javax.servlet.Servlet interface
People also reading
Membership not found