Applet vs Servlet
Presenting by
Bharat Sahu
MCA Vth Sem., MSIT
Outline
• Applet
• Introduction
• Life Cycle
• Implementation
• Servlet
• Introduction
• Life Cycle
• Implementation
• Difference Between Applet and Servlet
Applet
Introduction
• Applications are stand alone programs
• Executed by Java Interpreter.
• Applet is small program
• Can be placed on web page.
• It will be executed by web browser.
• Provide web page interactive content with power of Java.
• An applet is a Panel that allows interaction with a Java program.
• A applet is typically embedded in a Web page and can be run from a browser.
• You need special HTML in the Web page to tell the browser about the applet.
• Applets run in a sandbox: they have no access to the client’s file system.
• You can run Applet in any browser.
• The best support isn't a browser, but the standalone program appletviewer.
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
Applet
Class Hierarchy
init()
start()
stop()
destroy()
Do Some
Work
Life Cycle of An Applet
• Applet Method
• public void init ()
• public void start ()
• public void stop ()
• public void destroy ()
• public void paint (Graphics g)
note: repaint(), update()
import java.awt.*;
import java.applet.*;
public class WelcomeApplet extends Applet {
public void init() {
}
public void paint(Graphics g) {
g.drawString("Welcome to Java Programming!",
25, 25 );
}
}
<html>
<applet code = "WelcomeApplet.class"
width = "300" height = "45">
</applet>
</html>
Servlet
Introduction
• Server
• Servers are those machine which is responds the clients request.
• Servlets – Web-based solutions executes by Server
• Dynamically generate custom HTML documents
• Replacement to CGI
• Secure access to Website
• Interact with databases
• Servlet are as Applet but Server side
Servlet
How it works?
• Client sends a request to server
• Server starts a servlet
• Servlet computes a result for server and does not quit
• Server returns response to client
• Another client sends a request
• Server calls the servlet again
Server
Client
Client
Servlet
• A servlet is any class that implements the javax.servlet.Servlet
interface
• In practice, most servlets extend the javax.servlet.http.HttpServlet class
• Some servlets extend javax.servlet.GenericServlet instead
• Tomcat is the Servlet Engine than handles servlet requests for Apache
• It’s best to think of Tomcat as a “servlet container”
• Apache can handle many types of web services
• Apache can be installed without Tomcat
• Tomcat can be installed without Apache
Life cycle of Servlet
Initialization
(load resource)
Service
(accept requests)
Destruction
(unload resource)
• Initialize
• Service
• Destroy
Response
Request
• Initialize: init() method called once, when any request occur.
• Service: Any requests will be forwarded to the service() method
• doGet()
• doPost()
• doDelete()
• doOptions()
• doPut()
• doTrace()
• Destroy: called once
• destroy() method called when: Application is stopped or Servlet container shuts down
• Allows resources to be free
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class WelcomeServlet extends HttpServlet {
protected void doGet( HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException{
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
out.println( “<html><body>" );
out.println( “<h1>Hello World</h1>" );
out.println( “</body></html>" );
out.close
}
}
Applet vs Servlet
• Similarities
• Neither has a main()
• Both have init() and destroy()
• Both are part of a larger application made for the web
• Dissimilarity
• Applets run on the client (browser) while servlets run on the HTTP server
• Applets are having limited functionality to look at the local file system, establish
network connections, etc.
• Servlets are generally built to handle multiple clients at once, whereas applets
generally service one client at a time.
• Servlets handle HTTP request
• Applets used for static web pages, Servlet used for dynamic web pages.
Reference
• Sun’s Website
1. http://java.sun.com/docs/books/tutorial/servlets/lifecycle/index.htm
2. http://java.sun.com/docs/books/tutorial/applets/lifecycle/index.htm

Applet Vs Servlet

  • 1.
    Applet vs Servlet Presentingby Bharat Sahu MCA Vth Sem., MSIT
  • 2.
    Outline • Applet • Introduction •Life Cycle • Implementation • Servlet • Introduction • Life Cycle • Implementation • Difference Between Applet and Servlet
  • 3.
    Applet Introduction • Applications arestand alone programs • Executed by Java Interpreter. • Applet is small program • Can be placed on web page. • It will be executed by web browser. • Provide web page interactive content with power of Java.
  • 4.
    • An appletis a Panel that allows interaction with a Java program. • A applet is typically embedded in a Web page and can be run from a browser. • You need special HTML in the Web page to tell the browser about the applet. • Applets run in a sandbox: they have no access to the client’s file system. • You can run Applet in any browser. • The best support isn't a browser, but the standalone program appletviewer.
  • 5.
  • 6.
    init() start() stop() destroy() Do Some Work Life Cycleof An Applet • Applet Method • public void init () • public void start () • public void stop () • public void destroy () • public void paint (Graphics g) note: repaint(), update()
  • 7.
    import java.awt.*; import java.applet.*; publicclass WelcomeApplet extends Applet { public void init() { } public void paint(Graphics g) { g.drawString("Welcome to Java Programming!", 25, 25 ); } } <html> <applet code = "WelcomeApplet.class" width = "300" height = "45"> </applet> </html>
  • 8.
    Servlet Introduction • Server • Serversare those machine which is responds the clients request. • Servlets – Web-based solutions executes by Server • Dynamically generate custom HTML documents • Replacement to CGI • Secure access to Website • Interact with databases • Servlet are as Applet but Server side
  • 9.
    Servlet How it works? •Client sends a request to server • Server starts a servlet • Servlet computes a result for server and does not quit • Server returns response to client • Another client sends a request • Server calls the servlet again Server Client Client Servlet
  • 10.
    • A servletis any class that implements the javax.servlet.Servlet interface • In practice, most servlets extend the javax.servlet.http.HttpServlet class • Some servlets extend javax.servlet.GenericServlet instead • Tomcat is the Servlet Engine than handles servlet requests for Apache • It’s best to think of Tomcat as a “servlet container” • Apache can handle many types of web services • Apache can be installed without Tomcat • Tomcat can be installed without Apache
  • 11.
    Life cycle ofServlet Initialization (load resource) Service (accept requests) Destruction (unload resource) • Initialize • Service • Destroy Response Request
  • 12.
    • Initialize: init()method called once, when any request occur. • Service: Any requests will be forwarded to the service() method • doGet() • doPost() • doDelete() • doOptions() • doPut() • doTrace() • Destroy: called once • destroy() method called when: Application is stopped or Servlet container shuts down • Allows resources to be free
  • 13.
    import javax.servlet.*; import javax.servlet.http.*; importjava.io.*; public class WelcomeServlet extends HttpServlet { protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( “<html><body>" ); out.println( “<h1>Hello World</h1>" ); out.println( “</body></html>" ); out.close } }
  • 15.
    Applet vs Servlet •Similarities • Neither has a main() • Both have init() and destroy() • Both are part of a larger application made for the web
  • 16.
    • Dissimilarity • Appletsrun on the client (browser) while servlets run on the HTTP server • Applets are having limited functionality to look at the local file system, establish network connections, etc. • Servlets are generally built to handle multiple clients at once, whereas applets generally service one client at a time. • Servlets handle HTTP request • Applets used for static web pages, Servlet used for dynamic web pages.
  • 17.
    Reference • Sun’s Website 1.http://java.sun.com/docs/books/tutorial/servlets/lifecycle/index.htm 2. http://java.sun.com/docs/books/tutorial/applets/lifecycle/index.htm

Editor's Notes

  • #4 Normal java program has main method on code. Applet doesn't have main method.
  • #5 Plateform independent Secure
  • #6 JApplet extends Applet Class
  • #7 Init(): execute first, initialize variables, best place define buttons, textfields, layouts, slider, every applet have init method. Start(): called after init, not always needed. Conjuction with strop(). Stop(): not always needed, called when browser leaves the page, called before destroy, conjuction with start(). Destroy(): Seldom needed, Called after stop( ), Use to explicitly release system resources (like threads), System resources are usually released automatically. Paint(): almost always needed, don’t call its called automatically call instead repaint, any painting done here or call by here. Repaint(): it’s a request that internally call update(Graphics g), any changes happened then call repaint.
  • #9 Servlet communicate to Clients and Server through HTTP
  • #11 About 50% and above websites uses Apache server Apache is server, tomcat is container; apache uses tomcat as container both are free source There is also other server available