0% found this document useful (0 votes)
40 views

Java Unit 6 Questions

Embaded systems technical seminar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
40 views

Java Unit 6 Questions

Embaded systems technical seminar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
What is an applet? * a small Java program that can be inserted into a web page and run by loading that page in a browser. + An applet, like any application program, can do many things. + Itcan perform arithmetic operations, display graphics, play sounds, accept user Ee, create animation, and so on. + This is the feature of Java that i 13 primarily HanansDle for its initial i popularity: Applet execution by a browser L) request HTML page / < na, | a Chana | / | 6) eotcute ( Java yeplet PL {GI Sat TH Reference to Java yy peeae mM. Applet classes in Java + Implementation ~ a top-level container, like a Frame. * — behaves more like a Panel. — It does not contain a title bar, menu bar, or border. — This is why you don’t see these items when an applet is run inside a’ browser. Wi HAH 5 — When you run an applet us ‘ viewer patel thet - -~ java.apple App. ree or i) How Applets Differ a * Although both the A et stand-alone i applications are Java programs, there are certain i restrictions are impdsed on Applets, due to security concerns: ~ Applets don’t use the main() method, but when they are loaded, “automatically call certain methods (init, start, paint, stop, destroy), ~ They are embedded inside a web page and executed in browsers. ~ Takes input through Graphical User Interface(GUI). — They cannot read from or write to the files on local computer. They cannat run any programs from the local computer, — They are restricted from using libraries from other languages. * The aboye restrictions ensures that an Applet gannot do any damage to the local system Building Applet Code: An Example import java.awt.*; impart ict. Applet Building Applet Code: An Example import java.awt.*; import java.applet. Applet; public class SimpleApplet extends Applet { public void paint(Graphics g) { t g.drawString ("A Simple Applet",20, 20); } } > Begins with two import classes. - _ ¥javaawi" required for GUI > java.applet.* -- every ap > of Applet > The class should start wit from outside. + ‘An applet begins a when the name of its class is passed to an applet viewer or to a network browser. * Compile the applet in the same way that we have been compiling programs. + Running an applet involves a different process. Running an Applet 1. Using web browser Running an Applet 1. Using web browser 2. Using appletviewer + To execute an applet short HTML file that c apelet 1 a web browser, you need to write a sa tag veer) that loads the HTML file that pate */ public class Sample extends Applet { String msg; // set the foregrotind and background colors. public void init() { “setBackground(Color.green); setForeground(Color.red); msg = "Inside init( ) +-"; yuie // Initialize the string to be displayed. public void start() { i msg +=" Inside start() --"; } | g // Display msg in applet window. public void paint(Graphics g) { msg +=" Inside paint( )."; gdrawString(msg, 10, 30); ‘ } ef public void stop() ? <= i { : System.out.printin("Inside stop( )"); public void destroy() t System.out.println("Inside destroy( )"); J Output: } _| Applet started, — Afier closing appletyiewer stop( ) and destroy() methods will be called. Outpul in the conmuind prompt: Inside stop( ) Inside destroy( ) Basics of network programming IP Address * 32-bit identifier ¢ * Dotted-quad: 192.118.56.25 + Identifies a host Identifying the specific application * IP addresses identify hosts it + Host has many applications Hay * Ports (16-bit identifier) Application WWW E-mail Telnet | Port a A 192.18.22.13 x Sockets * Socket is an object used for network programming. * A sockel is bound to a specific port number * Network communication using Sockets is very much, similan ta nerforming file HO Sockets * Socket is an object used for nRWSER Pro wrainIng, * A-socket is bound to a specific port number * Network communication using Sockets is very much similar to performing file I/O f ocket that i is bound to a specific port. The | server waits and listens to + Ifeverything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bounds to a different port. It needs a new socket (consequently a different port number) so that it can continue to listen to the original e client-serever program simp! implified code) imple server (81 ple server program A&l 1 Simpler, or. a a sim ble java net? import java-to. public class Sopteteret static void main(Striog argall) mieosrs JOEsception 4 fil " Register service on port 128 bew ee cue t238 Serversocket server > Socket wserven aecept(hi kia nod accept a com! System.out.petntin("C Established" )5 Geta ‘communication stream ‘aici with the socket - DatsOnipusierss dos = new Datadutperserse ts .gecougpatstreamO5 1) Senda string! | i | paneer Hella Client) AEH i Clase the connection, but not the server yeh dos.close()s | ; s.close()s | 41 eee cnt eee PETE TT } 1 ETT Eeay PERTTI A simple clie1 1 SimpleClient java: a simp! import javasnet.*3 import java.io; i public class SimpleClient { public static void main( mE { // Open your connection to a ser bees ¢ client = uew Suckel(Hacallost? 1234) ||| beam input file handle from the socket and read the input talnputStream dis = new Datall r li oon So inpatStream(slent,getlupurStresin()) Hi System.out-printin(str); iy a 17 When done, just close the c ! | aapreals Just close the connection and exit | client.close(); Applet Program Accepting Parameters //HelloAppletMsg.java import java.applet Applet; import java.awt.*; /* */ cight=200> - "Hello World!"> - public class HelloAppletMsg extends Applet { String msg; i a ne ‘This is name of parameter specified in PARAM gs i “This method returns the value of paramter, ‘ | > public void init), | { i msg = es : “if msg == null) msg = "Hello"; i ih cH Tm ea : } oy : i _Public void paint(Graphics gt g.drawString (mse.50y 100); Types of applets 9. Ther tun tines of annletes

You might also like