Skip to document

Java Programming

Java Programming notes
Course

Programming 1 (CS101)

40 Documents
Students shared 40 documents in this course
Academic year: 2023/2024
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
University of the People

Comments

Please sign in or register to post comments.

Preview text

Java Programming :

applets, servlets and JSP.

SR

Dritan Nace

A summary of Java

Java is a language developed by Sun, which is designed to be object

oriented and Simple, robust and secure, independent of hardware

architectures and Multitasking.

Object oriented and simple : Simpler than C++, transparent memory

managment...

Robust et secure : Data typing is extremely strict. For applets, it is in principle

impossible to access the resources of the host machine.

Independant of hardware architectures : The compiler generates a universal

code : the « byte-code ». An interpreter which is specific to the host machine,

« virtual machine », executes the programs.

Multitasking : Java seemingly allows execution of several processes. In reality,

a time slot is given to each process on the processor (Multithreaded).

Java and object oriented

programming

  • Classes, and objects
    • The objects include data and processing for the data.
Communication is done via messages (methods).
  • A class corresponds to an abstract model for object construction. A
class is made up of:
  • attributes (static part)
  • Methods (dynamic part), which define the behaviour.
  • Inheritance : the « is a » relationship : a car is a vehicule,
  • Polymorphism : the same message can be recognised by several
objects and entail different behaviour.

Portability: JVM

The compiler compiles the java source in byte code : javac car => car

Then, java is the name of the program which will interpret the generated byte code.

Java and packages

• Packages

– java

– java

– java

– java

– java

– java

– java

– java

– javax

– java

classpath: specifies to the virtual machine the location from which the resources (bytecode

and others) should be taken.

– The directory /project/classes

– The archive /project/lib/archive

import : command used for importing remote packages or classes...

Applets

• An applet is a special Java application that will run in an HTML

document viewed by a browser.

– Goal : transmit executable code to the client.

• The Applet class is a sub-class of the Panel class (from the

java package). (An applet is a graphic component container.)

• An applet will be able to respond to mouse and keyboard events and

use graphic components like buttons, checkboxes etc..

• An applet runs in a specific JVM, the JVM of a web browser.

The java package

The java package:
allows programmers to integrate java applications (applets) into Web
documents.
This package contains:
The Applet class
The following interfaces : AppletContext, AppletStub et
AudioClip.
The Applet class is derived from the java.awt class.

Object | +----- Component { paint(); resize(); ... } | +----- Container | +----- Panel | +--- Applet

Creating an applet

In order to create an applet, a class which inherits from the Applet
class must be defined, and certain methods must be redefined.
It has four special methods, which are :
public void init()
public void start()
public void stop()
public void destroy()
It also inherits from the public void paint(Graphics g) methods
declared in the Component class

Methods

####### public void start()

####### The start() method is executed :

  • just after the init() method
  • every time the browser comes back to the HTML page which contains the applet.

####### public void stop()

####### The stop() method is executed each time the user leaves the web page which contains the

####### applet or whenever the page is no longer visible.

####### public void destroy()

####### The destroy() method is called at the end of the applet, so when the user leaves the

####### window or the web page browser.

####### public void paint(Graphics g)

####### The paint() method is obtained by inheritance, and is declared in the Component class

####### The paint() method is called each time the window manager has to draw the content of the

####### applet.

Other methods

Other methods allow obtaining of information about the applet which is running.
  • public String getParameter(String name)
Recovers the parameters passed to the HTML document.
  • public AppletContext getAppletContext()
Recovers the display context of the current applet.
It is generally a browser (Netscape etc.) or the appletviewer.
  • public URL getDocumentBase()
Returns the URL of the HTML document which includes the applet.
  • public Image getImage(URL url)
Loads an image which can be used afterwards. url is an absolute URL.

The <APPLET> tag

The <applet> tag enables the integration of a space for the executtion of a Java
application in an HTML document.

<APPLET CODE="AClassName" HEIGHT= anInteger WIDTH= anotherInteger > <PARAM NAME="firstParameter" VALUE="valueOfFirstParameter" > <PARAM NAME= "secondParameter" VALUE="valueOfSecondParameter" > ... </APPLET> The HTML file has the ability to pass parameters to an applet (the PARAM tag). The recovery of these parameters is done in the source of the applet using the following method String getParameter(String name) of theApplet class.

Structure of an applet

import java.; import java.; public class <AppletName> extends Applet { public void init() { <Initialisations> <Start of the processes> } public void start() { <Start the applet, the Web page is visited or becomes visible again> } public void paint(Graphics g) { <Draw the current content of the applet> } public void stop() { <Stop the applet, the web page is no longer visible or the user leaves the navigator> } public void destroy() { <Release the resources, the applet will leave the memory> } }

Life cycle of an applet : init() ( start() paint() stop() ) destroy()

Loading a JAR (Java Archive) file

  • This file format de fichier allows the fusion of several files which are
used by an applet (".class", sounds and images) into one file in the
JAR format which will be loaded with a singe request by the HTTP
protocol.
  • Creation : jar cfv file file_1 ... file_n
  • Appel : <applet code = "file" archive="file" width= "200" height= "200" > </applet>

Servlets and JSP

Was this document helpful?

Java Programming

Course: Programming 1 (CS101)

40 Documents
Students shared 40 documents in this course

University: Kabul University

Was this document helpful?