Вы находитесь на странице: 1из 5

Applets and Servlets - Introduction, Comparison, Life-cycle,

Example
This tutorial explains the concept of Applets and Servlets including basic introduction,
comparison between them for better understanding, Life-cycle of applets and servlets with
the examples ...

This approach is made to provide you an easy and better understanding of these concepts.
Basically, Applets and Servlets both are the Java programs. Both have their different uses
and importance for different applications.

These topics are concluded in a Comparison way under different headings which will help
you to understand and remember them easily.

Introduction to Applets and Servlets :

Applets :
Applets are the Java Programs which run in other applications, typically in webpage
displayed in web browser, on the Client machine. Applets are embedded in a HTML Page.

Like the Other Java Programs, Applets don`t have a main() method.

The packages used for Applets are :

java.applet - as import java.applet.*;


java.awt - as import java.awt.*;

Servlets :
Servlets are the Java Programs which run on the web or application Server.
They extend the functionality of a web server and provide structure for a business
environment.They can be operating system and hardware platform independent.

Servlets process HTTP/HTML requests with all the benefits of the Java language
( portability, performance, re-usability, and crash protection ) from the client and generates
the response for the request as HTML pages.

HTTP Servlet typically used to provide dynamic content like getting the results of a database
query and returning to the client.

Servlet may be considered as an Applet with no face.

Request from Client-->> Servlets <<-->>Response from Server/Database

The packages used for Servlets are :


javax.servlet - as import javax.servlet.*;
javax.servlet.http - as import java.servlet.http.*;

Comparison between Applets and Servlets :

This table provides you the overview on their differences :

Applets Servlets

Part of Core Java Part of Advance Java

Client Side Program Server Side Program

May have GUI No GUI Required

Can generate HTML, Javascript, Applet


Require compatible browser
code

Processed at Server, No Client Resources


Uses the resources of Client
required

Jar files can be accessed and downloaded by the


No Access
Client

Require JRE or Web browser`s plug-in to run Require Java Enabled Web Server

JVM varies with Browser Constant JVM

Use More Network bandwidth as runs loads and Less Network Bandwidth as runs on Server
executes on Client machine and only Results sent to Client

May have Security issue Under Server Security

Life-cycle of Applets and Servlets :


Applet Lifecycle :

init() : It is called when the applet is initialized.

start() : It is called when applet is (re)started

stop() : It is called when applet terminates or browser exits page.

paint(Graphics) : It is called when the applet is to be repainted.

destroy() : It is calledwhen applet is about to be destroyed.

Servlet Lifecycle :
init() : It intializes servlet after loading into memory. It perform operations done
only once at start-up
- reading a current properties
- clearing log files, notifying other services that the servlet is running.

service() [ doGet(), doPost() ] : It is called to process Client`s request.


- where work is done.
- each time the servlet is called a new thread of execution begins.
- input is passed to the Java code via either HTTP GET or POST commands.

destoy() : It is called when server calls servlet to terminate.It used to disconnect


from database.

Simple Examples for Applets and Servlets :

Simple Applet Program :


import java.applet.*;
import java.awt.*;

public class Hello extends Applet


{
public void init() //automatically called on satrt-up ,do initialization of graphics
{}
public void start()
{}

public void stop()


{}

public void paint(Graphics g)


// overrides the empty Applet method used to paint something on screen
{
g.drawString("Simple Applet Example",50,90); // method to draw text on screen
}
}
Embedding in HTML :
<!DOCTYPE html>
<html>
<head>
<title>Embedded Applet</title>
</head>
<body>
<applet code=" Hello.class " width="300" height="300">
</applet>
</body>
</html>

Servlet Program in Java :


Example : 1
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Hello extends HttpServlet


{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Servlet Example</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Welcome to First Sevrlet Program</h1>");
out.println("</body>");
out.println("</html>");
}
}

Example : 2
import java.io.*;
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("Welcome to First Sevrlet Program");


}
public void destroy()
{}
}

The doGet servlet method is used to obtain the two objects: HttpServletRequest and
HttpServletResponse.

HttpServletRequest is used to get the INPUT parameter names and values:


- getParameterNames()
- getParameterValues()

HttpServletResponse used to return HTML response println()

setContentType() method is used as the response object which sets the content type of
the response to text/html.

These examples will display " Welcome to First Sevrlet Program " to the user on visiting the
page.

Deploying the Servlet :


This is the web.xml file for deploying and running the Servlet.

<web-app>
<servlet>
<servlet-name>Servlet Example</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet Example</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>

Вам также может понравиться