Hello friends , I am new to Java Servlets . I hope the below discussed question and answer can be very helpful for a beginner to start with Java Servlets.

Question 1) Servlet Technology?

Answer :- A servlet is a Java class that is designed to respond  with dynamic content to client requests over a network.  The main functions of a servlet are reading requests from clients , generate the result and sent result back to the client.(Definition from the point of its working ). Don’t upset with this  definition.

Answer :- Programming point of view

A servlet is any class that implements the javax.servlet.Servlet interface .
In practice, most servlets extend the javax.servlet.http.HttpServlet class Some servlets extend javax.servlet.GenericServlet instead . We must override doGet() and doPost() methods to handle GET and POST requests.

Simple Program to start with Servlets
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(“Hello Servlet”);

Note:- Before running the above program you must install Tomcat (which is the s the Servlet Engine than handles servlet requests)

Hope this will be helpful for you to start with Java Servlets

Servlets Life Cycle

Why know about Servlet Life Cycle?

Answer is simple. To  better understand the behaviour of Servlets.

Servlet Life Cycle is mainly divided into three. They are

1) Initialisation Phase 2) Service Phase and 3) Destruction Phase. Lets define each of them briefly.

1) Initialisation Phase : A servlet is first loaded and initialized usually when it is requested by the clients.

public void init(ServletConfig config) throws ServletException .This is the function executed once when the servlet is  first loaded .It will not exectute for further requests.

2) Service Phase : After initialization phase next is the Service phase in which the Servlet serve the clients on requests .

public void service(ServletRequest request, ServletResponse response) throws ServletException , IOException
This method is called to process a request.

3) Destruction Phase : When all   requests are processed a  they may be destroyed by the server and release all the resources they occupy.

public void destroy() : This method is called once just before the servlet is unloaded and taken out of service.

I will be back with more topics about Java Servlets soon.


Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s