method inside doGet() and

computerharpyΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

47 εμφανίσεις

Classes and Interfaces

1

How can we create deadlock
condition on our servlet?


Ans: one simple way to call doPost()
method inside doGet() and
doGet()method inside doPost() it will
create deadlock situation for a servlet.

Classes and Interfaces

2

For initializing a servlet can we use
constructor in place of init ().


No, we can not use constructor for initializing a servlet because for
initialization we need an object of servletConfig using this object we
get all the parameter which are defined in deployment descriptor for
initializing a servlet and in servlet class we have only default
constructor according to older version of java so if we want to pass a
Config object we don’t have parameterized constructor and apart
from this servlet is loaded and initialized by container so it is the job
of container to call the method according to servlet specification they
have lifecycle method so init() method is called firstly.


Classes and Interfaces

3

Suppose you have a class which you serialized it and
stored in persistence and later modified that class to
add a new field. What will happen if you deserialize the
object already serialized?


in this case Java Serialization API
will

throw

java.io.InvalidClassException



Classes and Interfaces

4

Can we transfer a Serialized
object vie network?


Yes you can transfer a Serialized object
via network

because java serialized object
remains in form of bytes which can be
transmitter via network. You can also
store serialized object in Disk or database
as Blob.



Classes and Interfaces

5

While serializing you want some of the
members not to serialize? How do you
achieve it?


It is sometime also asked as what is the
use of

transient variable
,




Classes and Interfaces

6

Can you Customize Serialization process
or can you override default Serialization
process in Java?


The answer is yes you can. We all know that

for
serializing an
object

ObjectOutputStream.writeObject

(saveThisobject)
is invoked

and for reading
object

ObjectInputStream.readObject()

is invoked but
there is one more thing which

Java Virtual
Machine

provides you is to define these two method in
your class. If you define these two methods in your
class then

JVM

will invoke these two methods instead of
applying default serialization mechanism



Classes and Interfaces

7

How can we refresh
servlet on client?


On client side we can use Meta http
refresh

Classes and Interfaces

8

How many methods Serializable has? If
no method then what is the purpose of
Serializable interface?


Serializable interface exists in

java.io


package
and forms core of java serialization mechanism.
It doesn't have any method and also
called

Marker Interface in Java
. When your class
implements

java.io.Serializable

interface it
becomes Serializable in Java and gives compiler
an indication that use Java Serialization
mechanism to serialize this object.



Classes and Interfaces

9

“Minimize Accessibility of Classes
and Members” how to do that?


Standard advice for information
hiding/encapsulation


Decouples modules


Allows isolated development and maintenance


Java has an access control mechanism to
accomplish this goal


Item 13

Classes and Interfaces

10

Make Each Class or Member
as Inaccessible as Possible


Standard list of accessibility levels


private


package
-
private (aka package friendly)


protected


public


Huge difference between 2
nd

and 3
rd


package
-
private: part of implementation


protected: part of public API




Classes and Interfaces

11

In Public Classes, Use Accessors,
Not Public Fields


Avoid code such as:

class Point { public double x; public double y; }


No possibility of encapsulation


Also, public mutable fields are
not

thread safe


Use get/set methods instead:

public double getX() { return x; }

public void setX(double x) { this.x = x}


Advice holds for immutable fields as well


Limits possible ways for class to evolve


Item 14:

Classes and Interfaces

12

Example: Questionable Use of
Immutable Public Fields

public final class Time {


private static final int HOURS_PER_DAY = 24;


private static final int MINUTES_PER_HOUR = 60;



public final int hour;
// Not possible to change rep for class


public final int minute;



// But we can check invariants, since fields are final


public Time ( int hour, int minute ) {


if (hour < 0 || hour >= HOURS_PER_DAY)


throw new IllegalArgumentException(“Hour: “ + hour);


if (minute < 0 || minute >= MINUTES_PER_HOUR)


throw new IllegalArgumentException(“Minute: “ + minute);


this.hour = hour;


this.minute = minute;


}


// Remainder omitted

}

Classes and Interfaces

13

Minimize Mutability , why?


Reasons to make a class immutable:


Easier to design


Easier to implement


Easier to use


Less prone to error


More secure


Easier to share


Thread safe


Item 15:

Classes and Interfaces

14

Mutability


Mutable Objects:

When you have a
reference to an instance of an object, the
contents of that instance

can

be altered



Immutable Objects:

When you have a
reference to an instance of an object, the
contents of that instance

cannot

be
altered

Classes and Interfaces

15

Mutability


Final classes


A

final

class

cannot be subclassed. This is done for reasons of
security and efficiency.



Accordingly, many of the Java standard library classes are final, for
example

java.lang.System

and

java.lang.String
. All methods in a
final class are implicitly final.


Example:


public

final

class

MyFinalClass {...}



public

class

ThisIsWrong
extends

MyFinalClass {...}
// forbidden




Restricted subclasses are often referred to as "soft final" classes.

Classes and Interfaces

16

Mutability


Final methods


A

final

method

cannot be

overridden

by subclasses.


This
is used to prevent unexpected behavior from a
subclass altering a method that may be crucial to the
function or consistency of the
class.


Example:


public

class

MyClass

{
public

void

myMethod
() {...}


public

final

void

myFinalMethod
() {...} }


public

class

AnotherClass

extends

MyClass

{
public

void

myMethod
() {...}
// Ok



public

final

void

myFinalMethod
() {...}
// forbidden
}


Classes and Interfaces

17

Mutability


Final variables


A

final

variable

can only be initialized once, either
via an initializer or an assignment statement.


It does not need to be initialized at the point of
declaration: this is called a "blank final" variable.


A blank final instance variable of a class must be
definitely assigned at the end of every constructor of
the class in which it is declared;



Classes and Interfaces

18

Example: Class Complex

public
final

class Complex {


private final
double re;


private final
double im;


public Complex (double re, double im) { this.re = re; this.im = im;}



// Accessors with no corresponding mutators


public double realPart() { return re; }


public double imaginaryPart() { return im; }




// Note standard “producer” pattern


public Complex add (Complex c) {


return new Complex (re + c.re, im + c.im);


}




// similar producers for subtract, multiply, divide


// implementations of equals() and hashCode() use Double class

}

Classes and Interfaces

19

Inheritance is Forever


A commitment to allow inheritance is part
of public API


If you provide a poor interface, you (and
all of the subclasses) are stuck with it.


You cannot change the interface in
subsequent releases.


TEST for inheritance


How? By writing subclasses

Classes and Interfaces

20

Constructors Must Not Invoke
Overridable Methods

// Problem


constructor invokes overridden m()

public class Super {


public Super() { m();}


public void m() {…};

}

public class Sub extends Super {


private final Date date;


public Sub() {date = new Date();}


public void m() { // access date variable}

}

Classes and Interfaces

21

What Is the Problem?


Consider the code


Sub s = new Sub();


The first thing that happens in
Sub()
constructor
is a call to constructor in
Super()


The call to
m()
in
Super()
is overridden


But
date

variable is not yet initialized!


Further, initialization in
Super m()
never
happens!


Yuk!

Classes and Interfaces

22

What are the common mechanisms
used for session tracking?


Cookies

SSL sessions

URL
-

rewriting

Classes and Interfaces

23

What is the difference between Difference
between doGet() and doPost()?


A doGet() method is limited with 2k of data to be sent, and doPost() method
doesn't have this limitation. A request string for doGet() looks like the following:


http://www.allapplabs.com/svt1?p1=v1&p2=v2&...&pN=vN


doPost() method call doesn't need a long text tail after a servlet name in a
request. All parameters are stored in a request itself, not in a request string, and
it's impossible to guess the data transmitted to a servlet only looking at a request
string.

Classes and Interfaces

24

What are the different
kinds of enterprise beans?


Different kind of enterprise beans are
Stateless session bean, Stateful
session

bean, Entity

bean
,

Classes and Interfaces

25

What is Session Bean?


A session bean is a non
-
persistent object that
implements some business logic running on the server.
One way to think of a session object is as a logical
extension of the client program that runs on the server.


Session beans are used to manage the interactions of
entity and other session beans, access resources, and
generally perform tasks on behalf of the client.


Classes and Interfaces

26

What is software
architecture of EJB?


session and Entity EJBs consist of 4 and 5 parts respectively:


1.

A remote interface (a client interacts with it),



2.

A home interface (used for creating objects and for declaring
business methods),


3.

A bean object (an object, which actually performs business logic
and EJB
-
specific operations).



4.

A deployment descriptor (an XML file containing all information
required for maintaining the EJB) or a set of deployment descriptors
(if you are using some container
-
specific features).



5.
A Primary Key class
-

is only Entity bean specific.


Classes and Interfaces

27


Classes and Interfaces

28

Classes and Interfaces

29

Classes and Interfaces

30

Classes and Interfaces

31

Classes and Interfaces

32


Classes and Interfaces

33

Classes and Interfaces

34

Classes and Interfaces

35

Classes and Interfaces

36

Classes and Interfaces

37

Classes and Interfaces

38

Classes and Interfaces

39

What is the role of Remote
Interface in RMI?


Remote interfaces are defined by extending
,an interface called Remote provided in the
java.rmi package. The methods must throw
RemoteException. But application specific
exceptions may also be thrown.

Classes and Interfaces

40

An applet may not create frames (instances of
java.awt.Frame class).

• True


• False


False

While applets must themselves reside
within the browser, they may create other
frames.


Classes and Interfaces

41

An applet is an instance of the Panel class in
package jawa.awt.

• True

• False



True

Applets are small programs, meant not to
run on their own but be embedded inside
other applications (such as a web
browser). All applets are derived from the
class java.applet.Applet, which in turn is
derived from java.awt.Panel.


Classes and Interfaces

42


The class file of an applet specified using the CODE parameter in
an <APPLET> tag must reside in the same directory as the calling
HTML page.

• True


• False


False

The CODE parameter can specify
complete or relative path of the location
of the class file

Classes and Interfaces

43


Applets loaded over the internet have unrestricted
access to the system resources of the computer where
they are being executed.

• True


• False


False

Due to security reasons, applets run under
a restricted environment and cannot
perform many operations, such as reading
or writing to files on the computer where
they are being executed.


Classes and Interfaces

44

An applet may be able to access some methods of
another applet running on the same page.

• True

• False



True

Only public methods can be accessed in
such a fashion.


Classes and Interfaces

45


If

getParameter

method of an instance of java.applet.Applet class
returns null, then an exception of type a NullPointerException is
thrown.

• True


• False


False

A NullPointerException is thrown only
when a null value is used in a case where
an object is required. For instance, trying
to execute the

equals

method on a String
object when the object is null.

Classes and Interfaces

46

One of the popular uses of applets involves making
connections to the host they came from.

• True

• False


True

An example of this feature can be
implementation of a chat functionality.

Classes and Interfaces

47

Applets loaded from the same computer where they are executing
have the same restrictions as applets loaded from the network.

• True


• False


False

Applets loaded and executing locally have
none of the restrictions faced by applets
that get loaded from the network.


Classes and Interfaces

48

The methods

init
,

start
,

stop

and

destroy

are the four
major events in the life of an applet.

• True

• False




True

These methods initialize, start the
execution, stop the execution, and
perform the final cleanup respectively. Not
each of these methods may be overridden
in every applet.


Classes and Interfaces

49

Applets are not allowed to have contsructors
due to security reasons.

• True


• False



False

Applets may have constructors, but
usually don't as they are not guaranteed
to have access to the environment until
their

init
method has been called by the
environment.

Classes and Interfaces

50

Exam


Multiple choice and true/false questions
cover:


Basics of the language


Topics that we studied


Effective Java Items



There will be a cover page to record your
choice; please use it since I will not look
at the internal pages