Object-oriented programming (OOP) is popular because: • It enables reuse of previous code saved as classes
• All Java classes are arranged in a hierarchy • Object is the superclass of all Java classes
• Inheritance and hierarchical organization capture idea: • One thing is a refinement or extension of another
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
JAVA PROGRAMMING
1. Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
PAPER NAME: JAVA PROGRAMMING
PAPER CODE: BCA 206
CLASS: BCA IVth Semester
2. UNIT-I Inheritance and Class Hierarchies
• Object-oriented programming (OOP) is popular because:
• It enables reuse of previous code saved as classes
• All Java classes are arranged in a hierarchy
• Object is the superclass of all Java classes
• Inheritance and hierarchical organization capture idea:
• One thing is a refinement or extension of another
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
4. Is-a Versus Has-a Relationships
• Confusing has-a and is-a leads to misusing inheritance
• Model a has-a relationship with an attribute (variable)
public class C { ... private B part; ...}
• Model an is-a relationship with inheritance
• If every C is-a B then model C as a subclass of B
• Show this: in C include extends B:
public class C extends B { ... }
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
5. A Superclass and a Subclass
• Consider two classes: Computer and Laptop
• A laptop is a kind of computer: therefore a subclass
variables of Computer
and all subclasses
additional variables for
class Laptop
(and its subclasses)
methods of Computer
and all subclasses
additional Methods for
class Laptop
(and its subclasses)
6. Illustrating Has-a with Computer
public class Computer {
private Memory mem;
...
}
public class Memory {
private int size;
private int speed;
private String kind;
...
}
A Computer has only one Memory
But neither is-a the other
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
7. Initializing Data Fields in a Subclass
• What about data fields of a superclass?
• Initialize them by invoking a superclass constructor with the
appropriate parameters
• If the subclass constructor skips calling the superclass ...
• Java automatically calls the no-parameter one
• Point: Insure superclass fields initialized before
subclass starts to initialize its part of the object
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
8. Example of Initializing Subclass Data
public class Computer {
private String manufacturer; ...
public Computer (String manufacturer, ...) {
this.manufacturer = manufacturer; ...
}
}
public class Laptop extends Computer {
private double weight; ...
public Laptop (String manufacturer, ...,
double weight, ...) {
super(manufacturer, ...);
this.weight = weight;
}
}
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
9. Protected Visibility for Superclass Data
• private data are not accessible to subclasses!
• protected data fields accessible in subclasses
(Technically, accessible in same package)
• Subclasses often written by others, and
• Subclasses should avoid relying on superclass details
• So ... in general, private is better
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
10. Method Overriding
• If subclass has a method of a superclass (same signature),
that method overrides the superclass method:
public class A { ...
public int M (float f, String s) { bodyA }
}
public class B extends A { ...
public int M (float f, String s) { bodyB }
}
• If we call M on an instance of B (or subclass of B), bodyB runs
• In B we can access bodyA with: super.M(...)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
11. Method Overloading
• Method overloading: multiple methods ...
• With the same name
• But different signatures
• In the same class
• Constructors are often overloaded
• Example:
• MyClass (int inputA, int inputB)
• MyClass (float inputA, float inputB)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
12. Example of Overloaded Constructors
public class Laptop extends Computer {
private double weight; ...
public Laptop (String manufacturer,
String processor, ...,
double weight, ...) {
super(manufacturer, processor, ...);
this.weight = weight;
}
public Laptop (String manufacturer, ...,
double weight, ...) {
this(manufacturer, “Pentium”, ...,
weight, ...);
}
}
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
13. Overloading Example From Java Library
ArrayList has two remove methods:
remove (int position)
• Removes object that is at a specified place in the list
remove (Object obj)
• Removes a specified object from the list
It also has two add methods:
add (Element e)
• Adds new object to the end of the list
add (int index, Element e)
• Adds new object at a specified place in the list
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
14. Polymorphism
• Variable of superclass type can refer to object of subclass type
• Polymorphism means “many forms” or “many shapes”
• Polymorphism lets the JVM determine at run time which method to invoke
• At compile time:
• Java compiler cannot determine exact type of the object
• But it is known at run time
• Compiler knows enough for safety: the attributes of the type
• Subclasses guaranteed to obey
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
15. Interfaces vs Abstract Classes vs Concrete Classes
• A Java interface can declare methods
• But cannot implement them
• Methods of an interface are called abstract methods
• An abstract class can have:
• Abstract methods (no body)
• Concrete methods (with body)
• Data fields
• Unlike a concrete class, an abstract class ...
• Cannot be instantiated
• Can declare abstract methods
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
16. Abstract Classes and Interfaces
• Abstract classes and interfaces cannot be instantiated
• An abstract class can have constructors!
• Purpose: initialize data fields when a subclass object is created
• Subclass uses super(…) to call the constructor
• An abstract class may implement an interface
• But need not define all methods of the interface
• Implementation of them is left to subclasses
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
17. Example of an Abstract Class
public abstract class Food {
public final String name;
private double calories;
public double getCalories () {
return calories;
}
protected Food (String name, double calories) {
this.name = name;
this.calories = calories;
}
public abstract double percentProtein();
public abstract double percentFat();
public abstract double percentCarbs();
}
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
18. Example of a Concrete Subclass
public class Meat extends Food {
private final double protCal; ...;
public Meat (String name, double protCal,
double fatCal double carbCal) {
super(name, protCal+fatCal+carbCal);
this.protCal = protCal;
...;
}
public double percentProtein () {
return 100.0 * (protCal / getCalories());
}
...;
}
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
19. Example: Number and the Wrapper Classes
Declares what the
(concrete)
subclasses have in
common
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
20. Inheriting from Interfaces vs Classes
• A class can extend 0 or 1 superclass
• Called single inheritance
• An interface cannot extend a class at all
• (Because it is not a class)
• A class or interface can implement 0 or more interfaces
• Called multiple inheritance
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
21. UNIT-II EXCEPTION HANDLING
Use
try,
throw,
catch
to
watch for indicate
exceptions
handle
How to process exceptions and failures.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
22. Overview
• Exception
• Indication of problem during execution
• Uses of exception handling
• Process exceptions from program components
• Handle exceptions in a uniform manner in large projects
• Remove error-handling code from “main line” of execution
• A method detects an error and throws an exception
• Exception handler processes the error
• Uncaught exceptions yield adverse effects
• Might terminate program execution
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
23. Overview
• Code that could generate errors put in try blocks
• Code for error handling enclosed in a catch clause
• The finally clause always executes
• Termination model of exception handling
• The block in which the exception occurs expires
• throws clause specifies exceptions method throws
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
25. Terminology
• Thrown exception – an exception that has occurred
• Stack trace
• Name of the exception in a descriptive message that indicates the
problem
• Complete method-call stack
• ArithmeticException – can arise from a number of different problems in
arithmetic
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
26. Terminology
• Throw point – initial point at which the exception occurs, top row of call
chain
• InputMismatchException – occurs when Scanner method nextInt receives a
string that does not represent a valid integer
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
27. Termination Model of Exception Handling
• When an exception occurs:
• try block terminates immediately
• Program control transfers to first matching catch block
• try statement – consists of try block and corresponding catch and/or finally
blocks
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
28. Enclosing Code in a try Block
• try block – encloses code that might throw an exception and the code that
should not execute if an exception occurs
• Consists of keyword try followed by a block of code enclosed in curly
braces
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
29. Using the throws Clause
• Appears after method’s parameter list and before the method’s body
• Contains a comma-separated list of exceptions
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
30. Using the throws Clause
• Exceptions can be thrown by statements in method’s body of by methods
called in method’s body
• Exceptions can be of types listed in throws clause or subclasses
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
31. Example: Handling ArithmeticExceptions and
InputMismatchExceptions
• With exception handling
• program catches and handles the exception
• Example
• Allows user to try again if invalid input is entered (zero for
denominator, or non-integer input)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
32. Sequence of Events for throw
Preceding step
try block
throw
statement
unmatched catch
matching catch
unmatched catch
next step
33. Sequence of Events for
No throw
Preceding step
try block
throw
statement
unmatched catch
matching catch
unmatched catch
next step
34. When to Use Exception Handling
• Exception handling designed to process synchronous errors
• Synchronous errors – occur when a statement executes
• Asynchronous errors – occur in parallel with and independent of the
program’s flow of control
• Avoid using exception handling as an alternate form of flow of control.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
35. Java Exception Hierarchy
• Superclass Throwable
• Subclass Exception
• Exceptional situations
• Should be caught by program
• Subclass Error
• Typically not caught by program
• Checked exceptions
• Catch or declare
• Unchecked exceptions
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
36. Java Exception Hierarchy
• All exceptions inherit either directly or indirectly from class Exception
• Exception classes form an inheritance hierarchy that can be extended
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
37. Java Exception Hierarchy
• Class Throwable, superclass of Exception
• Only Throwable objects can be used with the exception-handling
mechanism
• Has two subclasses: Exception and Error
• Class Exception and its subclasses represent exception situations that
can occur in a Java program and that can be caught by the application
• Class Error and its subclasses represent abnormal situations that could
happen in the JVM – it is usually not possible for a program to recover
from Errors
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
39. Checked Exceptions
• Inherit from class Exception but not from RuntimeException
• Compiler enforces catch-or-declare requirement
• Compiler checks each method call and method declaration
• determines whether method throws checked exceptions.
• If so, the compiler ensures checked exception caught or declared in
throws clause.
• If not caught or declared, compiler error occurs.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
40. Unchecked Exceptions
• Inherit from class RuntimeException or class Error
• Compiler does not check code to see if exception caught or declared
• If an unchecked exception occurs and not caught
• Program terminates or runs with unexpected results
• Can typically be prevented by proper coding
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
41. Java Exception Hierarchy
• catch block catches all exceptions of its type and subclasses of its type
• If there are multiple catch blocks that match a particular exception type, only
the first matching catch block executes
• Makes sense to use a catch block of a superclass when all catch blocks for
that class’s subclasses will perform same functionality
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
42. finally Block
• Consists of finally keyword followed by a block of code enclosed in curly
braces
• Optional in a try statement
• If present, is placed after the last catch block
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
43. finally Block
• Executes whether or not an exception is thrown in the corresponding try
block or any of its corresponding catch blocks
• Will not execute if the application exits early from a try block via method
System.exit
• Typically contains resource-release code
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
44. Using finally
• View program
• Re-throw of exception
• Code for throw exception
• Blocks using finally
• Suggestion
• Do not use a try block for every individual statement which may cause
a problem
• Enclose groups of statements
• Follow by multiple catch blocks
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
45. Sequence of Events for finally clause
Preceding step
try block
throw
statement
unmatched catch
matching catch
unmatched catch
next step
finally
46. UNIT-III Event Handling & DEM
• Java uses an Event Delegation Model.
• Every time a user interacts with a component on the GUI, events are
generated.
• Events are component-specific.
• Events are objects that store information like
• the type of event that occurred,
• the source of the event,
• the time of an event to name a few.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
47. Event Delegation Model
• Once the event is generated, then the event is passed to other objects which
handle or react to the event, thus the term event delegation.
• The objects which react to or handle the events are called event listeners.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
48. Three Players
• Event source which generates the event object
• Event listener which receives the event object and handles it
• Event object that describes the event
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
49. Registering Listeners
• By having a class implement a listener interface, it can contain code to
handle an event.
• However, unless an instance of the class is registered with the component ,
the code will never be executed. (Common novice error.)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
50. A Few More Java Events
• FocusEvent – component gains or loses focus
• MouseEvent – mouse is moved, dragged, pressed, released or clicked
• WindowEvent– window is iconified, deiconified, opened or closed
• TextEvent – text is modified
• KeyEvent – key is pressed, depressed or both
• ContainerEvent – components are added or removed from Container
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
51. Corresponding Listeners
• FocusEvent – FocusListener
• MouseEvent – MouseListener, MouseMotionListener
• WindowEvent–WindowStateListener,WindowListener,
WindowFocusListener
• TextEvent – TextListener
• KeyEvent – KeyListener
• ItemEvent- ItemListener
• ContainerEvent – ContainerListener
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
52. Methods for Registering Listeners
• JButton
• addActionListener(ActionListener a)
• addChangeListener(ChangeListener c)
• addItemListener(ItemListener i)
• JList
• addListSelectionListener(ListSelectionListener l)
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
53. Adapter Classes
• In the previous implementation, we implemented four
empty methods.
• We can create a listener class that extends its
corresponding adapter class.
• Adapter classes provide the empty implementation of all
the methods in a listener interface
• We only need to override the method(s) whose behavior
we want to influence.
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
54. UNIT-IV Servlet Request Dispatcher
• Forward a request from one servlet to another (or jsp).
• Have first servlet do some of the work and then pass on to another.
• Can even forward on to a static source like html
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
55. The Request Dispather
• The RequestDispatcher object is used to send a a client request to any
resource on the server
• Such a resource may be dynamic (e.g. a Servlet or a JSP file) or static (e.g.
a HTML document)
• To send a request to a resource x, use:
getServletContext().getRequestDispatcher("x")
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
56. Request Dispatcher Methods
• void forward(ServletRequest request, ServletResponse response)
• Forwards a request from a Servlet to another resource
• void include(ServletRequest request, ServletResponse response)
• Includes the content of a resource in the current response
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)
57. Passing on Data
• 3 different ways to pass parameters for the forwarded Servlet or JSP
• Data that will be used only for this request:
request.setAttribute("key“,value);
• Data will be used for this client (also for future requests):
session.setAttribute("key“,value);
• Data that will be used in the future for every client
context.setAttribute("key", value);
Chanderprabhu Jain College of Higher Studies & School of Law
Plot No. OCF, Sector A-8, Narela, New Delhi – 110040
(Affiliated to Guru Gobind Singh Indraprastha University and Approved by Govt of NCT of Delhi & Bar Council of India)