Você está na página 1de 12

UNIT- 4

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

request a description of the parameters the applet recognizes

initialize the applet

destroy the applet

start the applet's execution

stop the applet's execution


Invoking an Applet:
An applet may be invoked by embedding directives in an HTML file and viewing the
file through an applet viewer or Java-enabled browser.
The <applet> tag is the basis for embedding an applet in an HTML file. Below is an
example that invokes the "Hello, World" applet:
<html>
<title>The Hello, World Applet</title>
<hr>
<applet code="HelloWorldApplet.class" width="320" height="120">
If your browser was Java-enabled, a "Hello, World"
message would appear here.
</applet>
<hr>
</html>
Web Servlet

Servlets are simple java programs that run on servers.

Servlets are commonly used with HTTP, hence the name HTTP Servlet

Servlets make use of Java standard


javax.servlet and javax.servlet.http

extension

classes

in

packages

Uses of Servlets:

Process and store data submitted by HTML form

Useful for retrieving and updating databases

Servlets working is based on Request-Response

Used in Cookies and session tracking

- 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

How Servlet Works?

Client makes a request for some servlet using Web Browser

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

The Web page is displayed by clients request

Web Servers:

Communication with web servers established with the help of web browser

Examples of web browsers: IE,Mozilla..

Examples of web servers: Apache web server, Microsoft IIS(Internet


Information Services)server...

Client makes a request to web server by sending a Uniform Resource


Locator(URL)

URL is divided into 3 parts:

(a) First part of URL is Protocol Name(HTTP or FTP)


(b) In second, name of web server is mentioned
example: www.university.com
(c)Request for webpage is displayed. Typically it is .html file

Web browser sends the GET request to web server.

Server sends HTML page to the browser

Deployment of Servlets:

Writing the servlets is best choice for server side programming

Servlets need a special environment for execution i.e., Tomcat, a servlet


container & open-source product is recommended which contains class
libraries, documentation and runtime support which is useful for executing
and testing the servlets.

For execution of servlets,following software must be installed

- JDK: As servlets are basically java files,JDK must be installed


- Tomcat: servlet can be executed
JDSK:

The Java Development Standard Edition Kit(JDSK) is available from Sun


Microsystems in order to develop and deploy Java applications on desktop
and servers

Installation : http://www.java.sun.com/j2se

After installation, important task is setting up of environment variables

Steps: Control panel Click System Click Advanced System Settings-Click


Environment Variables

Set the path variable by mentioning the directory C:\jdk1.06_4(your jdk


directory)\bin.Click OK to save settings.

Finally, go to cmd prompt and type javac

Tomcat Webserver:

Tomcat installation : http://tomcat.apache.org

In configuration window, under Configuration Options setup, we can set the


connector port.

By default ,the port is 8080

We can also set the username and password for administrator login

After installation, we need to configure with environment variables

- Go to control panel-System Advanced tab


- Set the environmental variable i.e., Variable name : JAVA_HOME
- Set the environmental variable i.e., Variable value : your_jdk_directory
( C: \jdk1.6.0_04 )

Again , Set the environmental variable i.e., Variable name : CATALINA_HOME

Set the environmental variable i.e., Variable value : your_tomcat_bin


directory

( C:\Tomcat5.5\apache-tomcat-5.5.27\bin)

Now,you can start tomcat server by clicking on startup.bat batch file


in( C:\Tomcat5.5\apache-tomcat-5.5.27\bin)

The Servlet API:

Servlet API consists of Classes and Interfaces

Classes and Interfaces come in two packages

- javax.servlet Protocol independent classes and interfaces exist


- javax.servlet.http HTTP specific classes and interfaces exist

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.

Você também pode gostar