Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
javax.servlet.Servlet interface
WhatsApp
Satyapriya Nayak
5y
5.4
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
Up Next
Servlet Chaining using request dispatcher
Ebook Download
View all
Solutions Manual to Objects First with Java – A Practical Introduction using BlueJ
Read by 1.8k people
Download Now!
Learn
View all
Membership not found