Escolar Documentos
Profissional Documentos
Cultura Documentos
JAVA APPLET
An applet is a Java program that runs in a Web browser. An applet can be a fully
functional Java application because it has the entire Java API at its disposal.
There are some important differences between an applet and a standalone Java
application, including the following:
An applet is a Java class that extends the java.applet.Applet class.
A main() method is not invoked on an applet, and an applet class will not define
main().
Applets are designed to be embedded within an HTML page.
When a user views an HTML page that contains an applet, the code for the applet is
downloaded to the user's machine.
A JVM is required to view an applet. The JVM can be either a plug-in of the Web
browser or a separate runtime environment.
The JVM on the user's machine creates an instance of the applet class and invokes
various methods during the applet's lifetime.
Applets have strict security rules that are enforced by the Web browser. The security
of an applet is often referred to as sandbox security, comparing the applet to a child
playing in a sandbox with various rules that must be followed.
Other classes that the applet needs can be downloaded in a single Java Archive
(JAR) file.
Life Cycle of an Applet
Four methods in the Applet class give you the framework on which you build any
serious applet:
init: This method is intended for whatever initialization is needed for your applet. It
is called after the param tags inside the applet tag have been processed.
start: This method is automatically called after the browser calls the init method. It
is also called whenever the user returns to the page containing the applet after
having gone off to other pages.
stop: This method is automatically called when the user moves off the page on
which the applet sits. It can, therefore, be called repeatedly in the same applet.
destroy: This method is only called when the browser shuts down normally.
Because applets are meant to live on an HTML page, you should not normally leave
resources behind after a user leaves the page that contains the applet.
paint: Invoked immediately after the start() method, and also any time the applet
needs to repaint itself in the browser. The paint() method is actually inherited from
the java.awt.
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Hello World", 25, 50);
}
}
The APPLET CLASS methods that do the following:
Get applet parameters
Get the network location of the HTML file that contains the applet
Get the network location of the applet class directory
Print a status message in the browser
Fetch an image
Fetch an audio clip
Play an audio clip
Resize the applet
Additionally, the Applet class provides an interface by which the viewer or browser
obtains information about the applet and controls the applet's execution.
request information about the author, version and copyright of the applet
Servlets are commonly used with HTTP, hence the name HTTP Servlet
extension
classes
in
packages
Uses of Servlets:
- Cookies are a message given to a Web browser by a Web server. The browser
stores the message in a text file.
- Session tracking keeps track of all previously accessed web pages
The Web Browser then sends the request to Web Server. The Web Server
finds the requested servlet
The obtained servlet gathers the relevant information by clients request and
builds a Web Page
Web Servers:
Communication with web servers established with the help of web browser
Deployment of Servlets:
Installation : http://www.java.sun.com/j2se
Tomcat Webserver:
We can also set the username and password for administrator login
( C:\Tomcat5.5\apache-tomcat-5.5.27\bin)
Client enters the URL in the web browser and makes a request in which it
generates HTTP request and sends it to the Webserver
Server invokes the init() method and is called,only,when the servlet is loaded
in memory for first time
Server can invoke the service for particular HTTP request using service()
method
Finally server unloads the servlet from the memory using the destroy()
method
UNIT - 5
Web Application
Web Applications are used to create dynamic websites. Java provides support for
web application through Servlets and JSPs. We can create a website with static
HTML pages, but when we want information to be dynamic, we need web
application.
Why we need Servlet?
Web servers are good for static contents HTML pages but they dont know how to
generate dynamic content or how to save data into databases, so we need another
tool that we can use to generate dynamic content. There are several programming
languages for dynamic content like PHP, Python, Ruby on Rails, Java Servlets and
JSPs.
Java Servlet and JSPs are server side technologies to extend the capability of web
servers by providing support for dynamic response and data persistence.
Web Container
Since servlet is a server side technology, we will need a web container that supports
Servlet technology, so we will use Apache Tomcat server.
Tomcat is a web container, when a request is made from Client to web server, it
passes the request to web container and its 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.
Web Container
When web container gets the request and if its 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.
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 dont 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.
Multithreading Support Container creates new thread for every request to the
servlet and when its processed the thread dies. So servlets are not initialized for
each request and saves time and memory.
JSP Support JSPs doesnt look like normal java classes and web container
provides support for JSP. Every JSP in the application is compiled by container and
converted to Servlet and then container manages them like other servlets.
Miscellaneous Task Web container manages the resource pool, does memory
optimizations, run garbage collector, provides security configurations, support for
multiple applications, hot deployment and several other tasks behind the scene that
makes our life easier.
Web Application Directory Structure
A Web application exists as a structured hierarchy of directories. The root of this
hierarchy serves as the document root for files that are part of the application.
WEB-INF
A special directory exists within the application hierarchy named "WEB-INF". This
directory contains all things related to the application that aren't in the document
root of the application.
The WEB-INF node is NOT part of the public document tree of the application.
NO file contained in the WEB-INF directory may be served directly to a client by the
container.
However, the contents of the WEB-INF directory are visible to servlet code using the
getResource and getResourceAsStream method calls on the ServletContext, and
may be exposed using the RequestDispatcher calls.
The contents of the WEB-INF directory are:
A special directory exists within the application hierarchy named "WEB-INF". This
directory contains all things related to the application that aren't in the document
root of the application.
The WEB-INF node is NOT part of the public document tree of the application.
NO file contained in the WEB-INF directory may be served directly to a client by the
container.
However, the contents of the WEB-INF directory are visible to servlet code using the
getResource and getResourceAsStream method calls on the ServletContext, and
may be exposed using the RequestDispatcher calls.