logo

Servlets in Java

Untitled

Servlet is a technology that are used to extends the capabilities of an server.

Java Servlets are programs that run on a web or application server and acts as middle layer between a request coming from a web browser or other HTTP client and from database or application on the HTP Server.

Servlets is an API in which No. of interface and classes are presents to create web application.

Using servlets , we can collect the data or input from users by or through a web page forms , presents the records from a database or another source , and create web pages dynamically.
Java servlets are thread based or it initialized the thread.

Java servlets are portable.

Untitled

Java servlet API contains two packages:-
1.) Javax.servlet package
2.) Javax.servlet.http package
There are many interface in Javax.servlet package . These are:-
1. ServletRequest
2. ServletResponse
3. ServletConfig
4. ServletContext
5. RequestDispatcher
A. Forword
B. Include
There are many interface in Javax.servlet.http package . These are:-
1. HttpServletRequest
2. HttpServletResponse
3. HttpServletSession etc.

Servlet Life cycle
In servlet life cycle , there are three methods :-
1. init ()
2. service ()
3. destroy ()
Step 1. Servlet class is loaded on the server and the servlet instance is created.
Step 2. The servlet is initialized by calling inint() method.The init() method is initialized only once for a servlet.
Step 3. After servlet initialization , servlet calls service method to process a client request.
Step 4. After successfully execution of program servlet calls the destroy () method and servlet is terminated.
Finally servlet is garbage collected by the garbage collector.

Untitledy6u67

 

Steps to create a servlet:-

The servlet can be created by three steps. These steps are . : –

  1. By  implementing Servlet interface
  2. By inheriting GenericServlet class
  3. By inheriting HttpServlet class

Example for a servlet

class DemoServlet extends  HttpServer {

Private static final long SerialVersionUID=1;

Protected void doGet(HttpServletRequest request , HttpServletResponse response ) {

response.setContentType(“ text /  Html ”);

PrintWriter out=response.getWriter();

out.println (“ Hello World ”);

}

}

Compiling servlet

For compiling servlet , jar file is required to be loaded.

  1. Servlet-api.jar                           for apache tomcat server
  2. Weblogic.jar                             for weblogic server
  3. Javaee.jar                                  for glassfish server
  4. Javaee.jar                                  for jBoss server

 

Creating Deployment Descriptor(web.xml)

It is an xml file , from which web container gets the information about the servlet to be invoked.

Web.xml file

<web-app>

 

<servlet>

<servlet-name>Demo</servlet-name>

 

<servlet-class> DemoServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Demo</servlet-name>

 

<url-pattern> /Hello</ url-pattern>

</servlet-mapping>

 

</web-app>

 

 

 

Description of the web.xml file

 

<web-app>                      represents the whole application

<servlet>                          represents the servlet

<servlet-name>                represents the servlet name

<servlet-class>                 represents the servlet class

<servlet-mapping>           it used to map the servlet

<url-pattern>                   used at client side to invoke the servlet

PrintGoogle GmailFacebookBookmark/Favorites