What is Servlet and Why needs Servlet ?

Servlet:
In begining internet consists only static contents written using HTML. Soon dynamic web contents were made possible using CGI technology. CGI enable the web server to call an external program and pass HTTP request information to that external program to process the request. The response from external program to pass back to the web server, which forwards it to the client browser.
When no. of users visiting popular site, CGI had failed to deliver scalable internet application.
Web application development technologies:
ColdFusion, Server Side Java Script (SSJS), PHP, Servlet, Jsp, Asp, Asp.net. Now, Asp (Asp.net) and Servlet/jsp have been the main technology.
A Servlet is a java program that programmatically extends the functionality of a web server in request-response programming model. Servlets are responsible for generating dynamic html. Servlet is executing within a web container and it does not have a visible interface. Servlets are managed objects that are object of a Servlet is created and managed by the web container.
The Servlet aware web server is called Servlet container. It is also known as Servlet engine early.

Default functionality of a web server is that it can only serve static resource. After Servlet, web server can serve static as well as dynamic resources. Servlet technology was introduced in 1996.
Servlet API
Servlet API is provided in javax.servlet and javax.servlet.http package. At the core of Servlet API is an interface name ‘Servlet’.
Javax.servlet.Servlet provides life cycle methods for a Servlet and must be implemented by each Servlet class.
Life cycle methods-
(1)    init()
This method is executed only once when Servlet object is created. This method is used by the web container to provide initialization parameters in the form of ServletConfig object.
Syntax-
public void init (ServletConfig config);
(2)    service()
This method is executed each time request for the Servlet is received. This method is responsible for generating dynamic contain.
Syntax-
public void service (ServletRequest request, ServletResponse response) throws ServletException, IOException;
(3)    destroy()
This method is executed only once when the Servlet object is unloaded form the memory.
Syntax-
public void destroy ();
A part from life cycle method Servlet provide to non-life cycle method that may be used Servlet developer.
(a)    getServletConfig()
return the reference of ServletConfig object.
Syntax: public ServletConfig getServletConfig()
(b)    getServletInfo()
can be used by the Servlet developer to provide information about the Servlet.
Syntax: public String getServletInfo()
Public class YourServlet implements Servlet
{
//Define all method of Servlet interface
}
Javax.servlet.GenericServlet class provides default implementation of Servlet interface.
Public class YourServlet extends GenericServlet
{
//override derived methods
}
GenericServlet class provides protocol independent implementation of Servlet interface. ‘Javax.servlet.http.HttpServlet’ class provides http specific implementation of Servlet interface. This class provides additional methods to handle http request. Most commonly used among these methods are:-
doGet() and doPost(), which are used to handle http get and post request.
Syntax:
public void doGet(HttpServletRequest request, HttpServletResponse respone) throws ServletException, IOException.
public void doPost(HttpServletRequest request, HttpServletResponse respone) throws ServletException, IOException.
In case of get Request request parameter are appended to the url after a question mark and send as a part of request header. Name and value of all request parameter are displayed in the address bar of the browser as well as maximum no. of parameter or size of the data that can be send as request parameter is limited by the size of the header.
In case of post Request request parameter are send as a part of request body (i.e data is sent as a separate link) that means unlimited data can be send as request parameter and name and values of request parameter are not show in the address bar.   
Method attribute of form element (in html page) specifies how the data will be transmitted to the Http Server.
Get is default and is also the method that is used when the user types a URL into the address bar or clicks on a hypertext link.
Advantage of get:
(a)    save the result of a form submission
you can submit data and bookmark the resultant url, send it to a collegeagus by email, or put it in a normal hypertext link.
(b)    Type data in by hand
You can test servlets or jsp pages that use get simply by entering url with appropriate data attached. The ability is convenient during initial development.
Advantage of post:
(a)    Transmit lager amount of data
Many browser limit urls to a few thousand characters, it used when form submit large amount of data.
(b)    Send binary data
Space, tabs, and many other character are illegal in urls. If you upload large binary file. It would be a time consuming process to encode all the characters before transmitting and decoded them on the other end.
(c)         Keep the data private from somone looking over the users shoulder.
Enctype:- This attribute specify the way in which data will be encoded before being transmitted. The default is application/x-www-form-urlencoded.Each space into + sign, = sign between entry name and value, and ampersand (&) between the pairs.
Post data is also encoded as application/x-www-form-urlencoded. Most recent browser support an additional ENCTYPE of multipart/form-data. This encoding transmit each field as a separate part of a MIME compatible document. To use this enctype, you must specify post as the method type. This encoding sometimes make it easier for server side program to handle complex data, and it is required when you are using file upload controls to send entire document.
Why Enctype?
In Http get request, the parameters are appended to the url as a query string, the request url will stick the parameter on the end of the request url. But an http request won’t work correctly if it contains unsafe character.
Each request runs in a separate thread!
You might hear people say things kike, “Each instance of the servlet…”but that’s just wrong. There aren’t multiple instances of any Servlet class, except in one special case (called SingleThreadMode).
The container runs multiple threads to process multiple request to a single Servlet and every client request generates a new pair of request and response objects.
That means multiple thread can concurrently be accessing the same Servlet object. Your doGet() and doPost() method must be careful to synchronize access to field and other share data. Local variables are not shared by multiple threads, and thus need no special protection.
If you implement SingleThreadModel (a marker interface) a single instance of Servlet not used by more than one request.

No comments:

Post a Comment