Thursday, May 7, 2015

Java Servlet

In the early days, web servers used to deliver
static contents that are indifferent to users' requests. But Now-a-days, Java servlets
are server-side programs (running inside a web server) that handle clients' requests and return a
customized or dynamic response for each request. The dynamic response could be based on user's input  (e.g., search, online shopping, online transaction) with data retrieved from databases or other applications, or time-sensitive data (such as news and stock prices).
Java servlets typically run on the HTTP protocol. HTTP is an
asymmetrical request-response protocol.The client sends a request message  to the server, and the server returns a response message  as illustrated.

Monday, May 4, 2015

Servlet Interface

The javax.servlet.Servlet is the base interface of Java Servlet API. Servlet interface declares the
life cycle methods of servlet. All the servlet classes are required to implement this interface. The methods declared in this interface are:
public abstract void init(ServletConfig paramServletConfig) throws ServletException – This
is the very important method that is invoked by servlet container to initialize the servlet and
unless ServletConfig parameters. The servlet is not ready to process client request until init()
method is finished executing. This method is called only once in servlet life cycle and make
Servlet class different from normal java objects. We can extend this method in our servlet classes
to initialize resources such as DB Connection, Socket c connection etc.

Servlet Class


Servlet is J2EE server driven technology to create web applications in java. The javax.servlet and
javax.servlet.http packages provide interfaces and classes for writing our own servlets.
javax.servlet.Servlet interface, which defines servlet lifecycle
All servlets must implement the javax.servlet.Servlet
methods. When implementing a generic service, we can extend the GenericServlet class provided
with the Java Servlet API. The HttpServlet class provides methods, such as doGet() and doPost(),
specific for handling HTTP-specific services.
Most of the times, web applications are accessed using HTTP protocol and thats why we mostly extend HttpServlet class.

Web Application Directory Structure


Java Web Applications are packaged as Web Archive (WAR) and it has a defined structure. You can
hierarchy. export above dynamic web project as WAR file and unzip it to check the hierarchy. It will be something like below image.
Web Archive Root Directory
Web Archive Root Directory


Important Work of Web Container

Some of the important work done by web container are:
Communication Support – Container provides easy way of communication between web server
and the servlets and JSPs. Because of container, we don’t need to build a server socket to listen
for any request from web server, parse the request and generate response. All these important and
complex tasks are done by container and all we need to focus is on our business logic for our
applications.
Lifecycle and Resource Management – Container takes care of managing the life cycle of
servlet. Container takes care of loading the servlets into memory, initializing servlets, invoking
servlet methods and destroying them. Container also provides utility like JNDI for resource
pooling and management.

Web Container


Tomcat is a web container, when a request is made from Client to web server, it passes the request to web container and it’s web container job to find the correct resource to handle the request (servlet or JSP) and then use the response from the resource to generate the response and provide it to web server. Then web server sends the response back to the client.
When web container gets the request and if it’s for servlet then container creates two Objects
HTTPServletRequest and HTTPServletResponse. Then it finds the correct servlet based on the URL and creates a thread for the request. Then it invokes the servlet service() method and based on the HTTP method service() method invokes doGet() or doPost() methods. Servlet methods generate the dynamic page and write it to response. Once servlet thread is complete, container converts the response to HTTP response and send it back to client.

Contact Form

Name

Email *

Message *