0% found this document useful (0 votes)
73 views19 pages

Cs6501 - Internet Programming

This document outlines the units and topics covered in the CS6501 - Internet Programming course. Unit 1 covers inheritance, packages, exception handling, and multithreading with applet programs as examples. Unit 2 discusses rich internet applications, collaboration tools, differences between websites and web servers, and HTML/CSS programming. Unit 3 focuses on DOM model, Java servlet architecture, servlet lifecycle, and session handling with JDBC examples. Unit 4 involves XML schema, parsers, XSLT transformation. Unit 5 is about Ajax architecture, web services with WSDL, and database driven web services using SOAP.

Uploaded by

muruganv84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views19 pages

Cs6501 - Internet Programming

This document outlines the units and topics covered in the CS6501 - Internet Programming course. Unit 1 covers inheritance, packages, exception handling, and multithreading with applet programs as examples. Unit 2 discusses rich internet applications, collaboration tools, differences between websites and web servers, and HTML/CSS programming. Unit 3 focuses on DOM model, Java servlet architecture, servlet lifecycle, and session handling with JDBC examples. Unit 4 involves XML schema, parsers, XSLT transformation. Unit 5 is about Ajax architecture, web services with WSDL, and database driven web services using SOAP.

Uploaded by

muruganv84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

CS6501 - INTERNET PROGRAMMING

Unit -1
1. Inheritance
2. Packages
3. Exception Handling
4. Multithreading
5. Applet (Smiley and Calculator Program)
Unit -2
1. Rich Internet Applications (8)
2. Collaborations tools (8)
3. Difference between websites and web server (8)
4. Difference between internet and intranet (8)
5. HTML and CSS Program (Given Program)
Unit - 3
1. DOM Model
2. Java Servlet Architecture
3. Servlet Life Cycle
4. Session Handling
5. Database connectivity (JDBC program example)
Unit -4
1. XML Schema
2. XML Parsers and Validation
3. XSL and XSLT Transformation
Unit -5
1. Ajax Client Server Architecture
2. Creating, Publishing, Testing and Describing a Web services (WSDL)
3. Database Driven web service from an application (SOAP).
Unit – I
Q1. Inheritance:

Inheritance can be defined as the process where one class acquires the
properties (methods and fields) of another. With the use of inheritance the
information is made manageable in a hierarchical order.

The class which inherits the properties of other is known as subclass


(derived class, child class) and the class whose properties are inherited is
known as superclass (base class, parent class).

extends is the keyword used to inherit the properties of a class. Following is


the syntax of extends keyword.
Syntax:

class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}

Example

class Calculation {
int z;
public void addition(int x, int y) {
z = x + y;
System.out.println("The sum of the given numbers:"+z);
}
public void Subtraction(int x, int y) {
z = x - y;
System.out.println("The difference between the given numbers:"+z);
}
}
public class My_Calculation extends Calculation {
public void multiplication(int x, int y) {
z = x * y;
System.out.println("The product of the given numbers:"+z);
}
public static void main(String args[]) {
int a = 20, b = 10;
My_Calculation demo = new My_Calculation();
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
}
}

Output

The sum of the given numbers:30


The difference between the given numbers:10
The product of the given numbers:200
Q2. Packages:

Packages are used in Java in order to prevent naming conflicts, to control


access, to make searching/locating and usage of classes, interfaces,
enumerations and annotations easier, etc.

A Package can be defined as a grouping of related types (classes,


interfaces, enumerations and annotations ) providing access protection and
namespace management.

Some of the existing packages in Java are −

 java.lang − bundles the fundamental classes

 java.io − classes for input , output functions are bundled in this package

Programmers can define their own packages to bundle group of


classes/interfaces, etc. It is a good practice to group related classes
implemented by you so that a programmer can easily determine that the
classes, interfaces, enumerations, and annotations are related.

Since the package creates a new namespace there won't be any name
conflicts with names in other packages. Using packages, it is easier to
provide access control and it is also easier to locate the related classes.

Example
Let us look at an example that creates a package called animals. It is a
good practice to use names of packages with lower case letters to avoid any
conflicts with the names of classes and interfaces.

Following package example contains interface named animals −

/* File name : Animal.java */


package animals;

interface Animal {
public void eat();
public void travel();
}
Now, let us implement the above interface in the same package animals −

package animals;
/* File name : MammalInt.java */

public class MammalInt implements Animal {

public void eat() {


System.out.println("Mammal eats");
}

public void travel() {


System.out.println("Mammal travels");
}

public int noOfLegs() {


return 0;
}

public static void main(String args[]) {


MammalInt m = new MammalInt();
m.eat();
m.travel();
}
}

Now compile the java files as shown below −

$ javac -d . Animal.java
$ javac -d . MammalInt.java

Now a package/folder with the name animals will be created in the current


directory and these class files will be placed in it
Q3. Exception Handling:

An exception (or exceptional event) is a problem that arises during the


execution of a program. When an Exception occurs the normal flow of the
program is disrupted and the program/Application terminates abnormally,
which is not recommended, therefore, these exceptions are to be handled.

An exception can occur for many different reasons. Following are some
scenarios where an exception occurs.

 A user has entered an invalid data.

 A file that needs to be opened cannot be found.

 A network connection has been lost in the middle of communications or the JVM
has run out of memory.

Some of these exceptions are caused by user error, others by programmer


error, and others by physical resources that have failed in some manner.

Based on these, we have three categories of Exceptions. You need to


understand them to know how exception handling works in Java.

Checked exceptions − A checked exception is an exception that occurs at


the compile time, these are also called as compile time exceptions. These
exceptions cannot simply be ignored at the time of compilation, the
programmer should take care of (handle) these exceptions.

Unchecked exceptions − An unchecked exception is an exception that


occurs at the time of execution. These are also called as Runtime
Exceptions. These include programming bugs, such as logic errors or
improper use of an API. Runtime exceptions are ignored at the time of
compilation.

Exception Hierarchy
All exception classes are subtypes of the java.lang.Exception class. The
exception class is a subclass of the Throwable class. Other than the
exception class there is another subclass called Error which is derived from
the Throwable class.

Errors are abnormal conditions that happen in case of severe failures, these
are not handled by the Java programs. Errors are generated to indicate
errors generated by the runtime environment. Example: JVM is out of
memory. Normally, programs cannot recover from errors.

Catching Exceptions
A method catches an exception using a combination of
the try and catchkeywords. A try/catch block is placed around the code
that might generate an exception. Code within a try/catch block is referred
to as protected code, and the syntax for using try/catch looks like the
following −

Syntax
try {
// Protected code
}catch(ExceptionName e1) {
// Catch block
}

Multiple Catch Blocks


A try block can be followed by multiple catch blocks. The syntax for multiple
catch blocks looks like the following −

Syntax
try {
// Protected code
}catch(ExceptionType1 e1) {
// Catch block
}catch(ExceptionType2 e2) {
// Catch block
}catch(ExceptionType3 e3) {
// Catch block
}
The Throws/Throw Keywords
If a method does not handle a checked exception, the method must declare
it using the throws keyword. The throws keyword appears at the end of a
method's signature.

You can throw an exception, either a newly instantiated one or an exception


that you just caught, by using the throw keyword.

The Finally Block


The finally block follows a try block or a catch block. A finally block of code
always executes, irrespective of occurrence of an Exception.

Using a finally block allows you to run any cleanup-type statements that
you want to execute, no matter what happens in the protected code.

A finally block appears at the end of the catch blocks and has the following
syntax −

Syntax
try {
// Protected code
}catch(ExceptionType1 e1) {
// Catch block
}catch(ExceptionType2 e2) {
// Catch block
}catch(ExceptionType3 e3) {
// Catch block
}finally {
// The finally block always executes.
}

Example Program (Divide by Zero)

class Division {
public static void main(String[] args) {
 
int a, b, result;
 
Scanner input = new Scanner(System.in);
System.out.println("Input two integers");
 
a = input.nextInt();
b = input.nextInt();
 
// try block
 
try {
result = a / b;
System.out.println("Result = " + result);
}
 
// catch block
 
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero.");
}
}
}

Q4. Multithreading:
Java is a multi-threaded programming language which means we can
develop multi-threaded program using Java. A multi-threaded program
contains two or more parts that can run concurrently and each part can
handle a different task at the same time making optimal use of the
available resources specially when your computer has multiple CPUs.

By definition, multitasking is when multiple processes share common


processing resources such as a CPU. Multi-threading extends the idea of
multitasking into applications where you can subdivide specific operations
within a single application into individual threads. Each of the threads can
run in parallel. The OS divides processing time not only among different
applications, but also among each thread within an application.

Multi-threading enables you to write in a way where multiple activities can


proceed concurrently in the same program.

Life Cycle of a Thread


A thread goes through various stages in its life cycle. For example, a thread
is born, started, runs, and then dies. The following diagram shows the
complete life cycle of a thread.

Following are the stages of the life cycle −


 New − A new thread begins its life cycle in the new state. It remains in this
state until the program starts the thread. It is also referred to as a born
thread.
 Runnable − After a newly born thread is started, the thread becomes runnable.
A thread in this state is considered to be executing its task.
 Waiting − Sometimes, a thread transitions to the waiting state while the thread
waits for another thread to perform a task. A thread transitions back to the
runnable state only when another thread signals the waiting thread to continue
executing.
 Timed Waiting − A runnable thread can enter the timed waiting state for a
specified interval of time. A thread in this state transitions back to the runnable
state when that time interval expires or when the event it is waiting for occurs.
 Terminated (Dead) − A runnable thread enters the terminated state when it
completes its task or otherwise terminates.

Thread Priorities
Every Java thread has a priority that helps the operating system determine
the order in which threads are scheduled.

Java thread priorities are in the range between MIN_PRIORITY (a constant


of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is
given priority NORM_PRIORITY (a constant of 5).

Threads with higher priority are more important to a program and should be
allocated processor time before lower-priority threads. However, thread
priorities cannot guarantee the order in which threads execute and are very
much platform dependent.

Create a Thread by Implementing a Runnable Interface


If your class is intended to be executed as a thread then you can achieve
this by implementing a Runnable interface. You will need to follow three
basic steps −

Step 1
As a first step, you need to implement a run() method provided by
a Runnableinterface. This method provides an entry point for the thread
and you will put your complete business logic inside this method. Following
is a simple syntax of the run() method −

public void run( )


Step 2
As a second step, you will instantiate a Thread object using the following
constructor −

Thread(Runnable threadObj, String threadName);

Where, threadObj is an instance of a class that implements


the Runnableinterface and threadName is the name given to the new
thread.

Step 3
Once a Thread object is created, you can start it by calling start() method,
which executes a call to run( ) method. Following is a simple syntax of
start() method −

void start();

Example Program:
Here is an example that creates a new thread and starts running it −

class RunnableDemo implements Runnable {


private Thread t;
private String threadName;

RunnableDemo( String name) {


threadName = name;
System.out.println("Creating " + threadName );
}

public void run() {


System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
}catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}

public void start () {


System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
}

public class TestThread {

public static void main(String args[]) {


RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();

RunnableDemo R2 = new RunnableDemo( "Thread-2");


R2.start();
}
}

This will produce the following result −

Output
Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.

Q5. 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 gives 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.

Draw Smiley In Applet Example:


import java.awt.*;

import java.applet.*;

public class Smiley extends Applet{

public void paint(Graphics g){

Font f = new Font("Helvetica", Font.BOLD,20);

g.setFont(f);

g.drawString("Keep Smiling!!!", 50, 30);

g.drawOval(60, 60, 200, 200);

g.fillOval(90, 120, 50, 20);

g.fillOval(190, 120, 50, 20);

g.drawLine(165, 125, 165, 175);

g.drawArc(110, 130, 95, 95, 0, -180);

Simple Calculator Using Applet


import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;;
public class calculator extends Applet implements ActionListener, TextListener
{
String s,s1,s2,s3,s4;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
Button add,sub,eq,cl,mul,div;
TextField t1;
int a,b,c;
public void init()
{
t1=new TextField(10);
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b0=new Button("0");
add=new Button("+");
sub=new Button("-");
mul=new Button("*");
div=new Button("/");
eq=new Button("=");
cl=new Button("Clear");

GridLayout gb=new GridLayout(4,5);


setLayout(gb);
add(t1);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(add);
add(sub);
add(mul);
add(div);
add(eq);
add(cl);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
eq.addActionListener(this);
cl.addActionListener(this);
paint();
//t1.addTextListener(this);
}
public void paint()
{
setBackground(Color.green);
}

public void actionPerformed(ActionEvent e)


{
s=e.getActionCommand();
if(s.equals("0")||s.equals("1")||s.equals("2")||
s.equals("3")||s.equals("4")||s.equals("5")||s.equals("6")||s.equals("7")||s.equals("8")||
s.equals("9")||s.equals("0"))
{
s1=t1.getText()+s;
t1.setText(s1);
}
if(s.equals("+"))
{
  s2=t1.getText();
  t1.setText("");
  s3="+";
}
if(s.equals("-"))
{
  s2=t1.getText();
  t1.setText("");
  s3="-";
}
if(s.equals("*"))
{
  s2=t1.getText();
  t1.setText("");
  s3="*";
}
if(s.equals("*"))
{
  s2=t1.getText();
  t1.setText("");
  s3="*";
}
if(s.equals("="))
{
s4=t1.getText();
a=Integer.parseInt(s2);
b=Integer.parseInt(s4);
if(s3.equals("+"))
c=a+b;
if(s3.equals("-"))
c=a-b;

t1.setText(String.valueOf(c));
}
if(s.equals("Clear"))
{
t1.setText("");
}
}
public void textValueChanged(TextEvent e)
{
}
}

Unit -2
HTML and CSS program:
HTML with inline CSS:
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output:

HTML with External CSS:


<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="styles.css">
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

styles.css
body {
    background-color: powderblue;
}
h1 {
    color: blue;
}
p {
    color: red;
}

Output:

You might also like