Sunday 22 December 2013

Java Web Server Core 1- Servlet



Java Web Server Core 1- Servlet

Servlet is the foundation of all Java Web Server. Almost all of the Java Web Server’s request-response is finished by Servlet. It is important to understand what Servlet is and how it is working.

In this blog, I am going to talk about the follow topics:


  • ·         Introduction to Servlet
  • ·         the Servlet lifecycle
  • ·         how to develop a simple servlet
  • ·         how to deploy the servlet


The introduction to Servlet.

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.
 Typically, the Java Web Server (or sometimes called Servlet container) will parse the HTTP protocol and prepare the request object, meanwhile it prepares the response object. The servlet gets the request object and will process it according to the business logic (computation, database access, etc) then it will pass the results to the response object.


Servlet lifecycle


  1.   Init: the function will be excuted when the servlet container was initialised (load-on-startup=1) or the client first calls the servlet (load-on-startup=0)
  2.  Service: the function will be called every time to server the request. doPost or doGet.
  3.    Destroy: when the container offloads the servlet, the function will be called.

 

how to develop a simple servlet

usually Servlet can be developed by three ways:
1)      Implement the servlet interface javax.servlet.Servlet
2)      Extend the abstract class javax.servlet.GenericServlet
3)      Extend the abstract class javax.servlet.http.HttpServlet

The third one is most commonly used and the only way used now in production. It provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:

  • ·         doGet, if the servlet supports HTTP GET requests
  • ·         doPost, for HTTP POST requests
  • ·         doPut, for HTTP PUT requests
  • ·         doDelete, for HTTP DELETE requests
  • ·         init and destroy, to manage resources that are held for the life of the servlet
  • ·         getServletInfo, which the servlet uses to provide information about itself

After you develop the servlet, you may need to deploy the servlet to the web container.

How to deploy a servlet to Java Web Server (TOMCAT)

Here is some demonstrate steps
  • Under the $CATALINA_HOME/webapps folder create a new folder eg called servletTestApp.
  • Under servletTestApp create another folder called WEB-INF.
  • Under WEB-INF, create a folder called classes.
  • Also, under WEB-INF, create a file called web.xml
  • Next, create the servlet, testServlet.
  • Edit the web.xml to add the following servlet mapping:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <servlet>
        <servlet-name>testServlet</servlet-name>
        <servlet-class>testServlet</servlet-class>
    </servlet>


    <servlet-mapping>
        <servlet-name>testServlet</servlet-name>
        <url-pattern>/testServlet</url-pattern>
    </servlet-mapping>
</web-app>

  • This is to tell Tomcat which servlet is to be made available under which URL.
  • Restart Tomcat, if its already running.
You should now be able to access your servlet under http://[localhost]:[portno]//servletTestApp

No comments:

Post a Comment