Professional Documents
Culture Documents
C) Security Manager:
A security manager is a Java virtual machine (VM) object that implements
a security policy. By default, the Java 2 platform software provides a
security manager that disallows all access to local system resources apart
from read and write access to the directory and its subdirectories where
the program is invoked.
You can extend the default security manager to implement customized
verifications and approvals for applets and applications, but the
implementation must include the appropriate access verification code for
every checkXXX method you override. If you do not include this code, no
access verification check happens, and your code breaches the system
security policy.
2. Discuss the following with suitable example programs for each:
A) Data Types in Java:
When declaring a variable, besides its name, you must provide the type of information
that the variable will hold. The role of this type is to tell the compiler how much memory
will be needed to store the value(s) of that variable. Since your program is not the only
one used in the computer, memory has to be effectively managed.
The type of value that a variable will hold is called a data type. As you ay imagine,
different variables can be meant to hold different types of values. Each data type uses a
Java keyword to be characterized. As a modern language, Java provides even more
support for its traditional data types. To do this, a class was created for each data type.
This allows you to get more information from a variable declared with the particular data
type you want to use.
B) Variables in Java:
As we know, an object stores its state in fields.
int cadence = 0;
int speed = 0;
int gear = 1;
The What Is an Object? discussion introduced you to fields, but you probably have still a
few questions, such as: What are the rules and conventions for naming a field? Besides
int, what other data types are there? Do fields have to be initialized when they are
declared? Are fields assigned a default value if they are not explicitly initialized? We'll
explore the answers to such questions in this lesson, but before we do, there are a few
technical distinctions you must first become aware of. In the Java programming
language, the terms "field" and "variable" are both used; this is a common source of
confusion among new developers, since both often seem to refer to the same thing.
The Java programming language defines the following kinds of variables:
Instance Variables (Non-Static Fields) Technically speaking, objects store their
individual states in "non-static fields", that is, fields declared without the static keyword.
Non-static fields are also known as instance variables because their values are unique to
each instance of a class (to each object, in other words); the currentSpeed of one
bicycle is independent from the currentSpeed of another.
Class Variables (Static Fields) A class variable is any field declared with the static
modifier; this tells the compiler that there is exactly one copy of this variable in
existence, regardless of how many times the class has been instantiated. A field defining
the number of gears for a particular kind of bicycle could be marked as static since
conceptually the same number of gears will apply to all instances. The code static int
numGears = 6; would create such a static field. Additionally, the keyword final could be
added to indicate that the number of gears will never change.
Local Variables Similar to how an object stores its state in fields, a method will often
store its temporary state in local variables. The syntax for declaring a local variable is
similar to declaring a field (for example, int count = 0;). There is no special keyword
designating a variable as local; that determination comes entirely from the location in
which the variable is declared which is between the opening and closing braces of a
method. As such, local variables are only visible to the methods in which they are
declared; they are not accessible from the rest of the class.
Parameters You've already seen examples of parameters, both in the Bicycle class and
in the main method of the "Hello World!" application. Recall that the signature for the
main method is public static void main(String[] args). Here, the args variable is the
parameter to this method. The important thing to remember is that parameters are
always classified as "variables" not "fields". This applies to other parameter-accepting
constructs as well (such as constructors and exception handlers) that you'll learn about
later in the tutorial.
b[i][j]=Integer.parseInt(dis.readLine());
} if(c1!=r2)
System.out.println("Multiplication Is Not Possible");
else
{
for(i=0;i{
for(j=0;j{
c[i][j]=0;
for(k=0;k{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i{
for(j=0;j{
System.out.println(c[i][j]+" ");
}
System.out.println(" ");
}
}
}
}
// Line 1
// Line 2
System.out.println("Exit main().");
}
public static void division(int totalSum, int totalNumber) {
System.out.println("Computing Division.");
int average = totalSum/totalNumber;
System.out.println("Average : "+ average);
}
}
An ArithmeticException is thrown at runtime when Line 11 is executed because
integer division by 0 is an illegal operation. The Exit main() message is never
reached in the main method
Output
Computing Division.
java.lang.ArithmeticException: / by zero
state. A thread that is in inactive state can either be resumed, in which case it
enters the ready state again, or it can be stopped in which case it enters the
finished state.
B) Thread Priorities:
In multithreading environment, one thread might require the attention of the CPU
more quickly than other. In such a case that thread is said to be of high priority.
Priority of a thread determines the switching from one thread to another. In other
words, priority determines how a thread should behave with respect to the other
threads.
The word priority should not be confused with the faster running of a thread. A high
priority thread does not run any faster than the low priority thread. A thread can
voluntarily leave the control by explicitly stopping, sleeping or blocking on pending
I/O or it can pre-empted by the system to do so. In the first case the processor
examines all threads and assigns the control to the thread having the highest
priority. In the second case, a low priority thread that is not ready to leave the
control is simply pre-empted by the higher priority thread no matter what it is doing.
This is known as pre-emptive multi-tasking. It is advisable that in case two threads
have the same priority, they must explicitly surrender the control to their peers.
C) Thread Synchronization:
Multithreading produces asynchronous behavior among the programs. This means
that all threads run as independent units without affecting each other. But
sometimes it becomes necessary to synchronize these threads. For example,
synchronization must be provided when two threads share the same variable or
data structure like an array. In such a case there must be way by which they should
not come in each others way.
The remote reference layer, which is the middleware between the stub/skeleton
layer and the underlying transport protocol. This layer handles the creation and
management of remote object references.
The transport protocol layer, which is the binary data protocol that sends remote
object requests over the wire.
These layers interact with each other as shown in Figure 3-1. In this figure, the
server is the application that provides remotely accessible objects, while the client
is any remote application that communicates with these server objects.
In a distributed object system, the distinctions between clients and servers can get
pretty blurry at times. Consider the case where one process registers a remoteenabled object with the RMI naming service, and a number of remote processes are
accessing it. We might be tempted to call the first process the server and the other
processes the clients. But what if one of the clients calls a method on the remote
object, passing a reference to an RMI object that's local to the client. Now the server
has a reference to and is using an object exported from the client, which turns the
tables somewhat. The "server" is really the server for one object and the client of
another object, and the "client" is a client and a server, too. For the sake of
discussion, I'll refer to a process in a distributed application as a server or client if its
role in the overall system is generally limited to one or the other. In peer-to-peer
systems, where there is no clear client or server, I'll refer to elements of the system
in terms of application-specific roles (e.g., chat participant, chat facilitator).
As you can see in Figure 3-1, a client makes a request of a remote object using a
client-side stub; the server object receives this request from a server-side object
skeleton. A client initiates a remote method invocation by calling a method on a
stub object. The stub maintains an internal reference to the remote object it
represents and forwards the method invocation request through the remote
reference layer by marshalling the method arguments into serialized form and
asking the remote reference layer to forward the method request and arguments to
the appropriate remote object. Marshalling involves converting local objects into
portable form so that they can be transmitted to a remote process. Each object is
checked as it is marshaled, to determine whether it implements the
java.rmi.Remote interface. If it does, its remote reference is used as its marshaled
data. If it isn't a Remote object, the argument is serialized into bytes that are sent to
the remote host and reconstituted into a copy of the local object. If the argument is
neither Remote nor Serializable, the stub throws a java.rmi.MarshalException back
to the client.
POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as
appropriate.
Here is the signature of this method:
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException{}
The service () method is called by the container and service method invokes doGe,
doPost, doPut, doDelete, etc. methods as appropriate. So you have nothing to do
with service() method but you override either doGet() or doPost() depending on
what type of request you receive from the client.
The doGet() and doPost() are most frequently used methods within each service
request. Here are the signature of these two methods.
The doGet() Method
A GET request results from a normal request for a URL or from an HTML form that
has no METHOD specified and it should be handled by doGet() method.
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {}
The doPost() Method
A POST request results from an HTML form that specifically lists POST as the
METHOD and it should be handled by doPost() method.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {}
The destroy() method :
The destroy() method is called only once at the end of the life cycle of a servlet.
This method gives your servlet a chance to close database connections, halt
background threads, write cookie lists or hit counts to disk, and perform other such
cleanup activities.
After the destroy() method is called, the servlet object is marked for garbage
collection. The destroy method definition looks like this:
public void destroy() {// Finalization code... }
The following code fragment from the above program, opens the input
and output streams:
FileReader fr = new FileReader(args[0]);
PrintWriter pw = new PrintWriter(System.out, true);
The program reads the input file and displays its contents until it reaches
an end-of-file condition (-1), as shown here:
while ((read = fr.read(c)) != -1)
pw.write(c, 0, read);
Notice the "(char cbuf[])" version of the read method. This is used here
because in most cases reading a single character at a time can be
approximately five times slower than reading chunks of data (array) at a
time.
2. Write the steps in creating and running applets in Java along with
a sample program to demonstrate the same. Also describe various
ways of running Applets.
A Java applet is an applet delivered to the users in the form of Java bytecode. Java
applets can run in a Web browser using a Java Virtual Machine (JVM), or in Sun's
AppletViewer, a stand-alone tool for testing applets. Java applets were introduced in
the first version of the Java language in 1995. Java applets are usually written in the
Java programming language but they can also be written in other languages that
compile to Java bytecode such as Jython.
Java applets run at a speed that is comparable to (but generally slower than) other
compiled languages such as C++, but many times faster than JavaScript. In addition
they can use 3D hardware acceleration that is available from Java. This makes
applets well suited for non trivial, computation intensive visualizations.
import java.applet.Applet;
import java.awt.*;
For compilation, this code is saved on a plain-ASCII file with the same name as the
class and .java extension, i.e. HelloWorld.java. The resulting HelloWorld.class applet
should be placed on the web server and is invoked within an HTML page by using an
<APPLET> or an <OBJECT> tag. For example:
objects to database entities in such a way that applications do not even know
whether or not their objects are being stored in a database.
Java affects the way we distribute and maintain application. Currently a Web
application allows user to download a bunch of flight information as an HTML page.
While viewing the page for a particular flight, suppose someone makes a
reservation for a seat on that flight and this process will not be available to the
viewers as the page is just a copy of data from the database. To view the change
that just occurred, the viewers again have to contact the database to get the latest
data. If we reconstruct the same Web application using Java RMI to retrieve the data
from a single flight object on the server, any number of users can view the data
simultaneously and if there is any reservation or any change taking place,
immediately the changes made to the data will be sent back to all the users and
hence the users can avail the latest data at any time. Thus JDBC can combine with
Java RMI to develop distributed enterprise-scale mission-critical three-tier database
applications.
place of a get method. For example, the following two method signatures create a
Boolean property named atHome:
public void setAtHome(boolean b)
public boolean isAtHome()
The name of a property is determined by the method signatures for the property.
The set or get portion of the method name is removed, and the next letter is
decapitalized.
B) Bound Properties:
Bound properties support the PropertyChangeListener (in the API reference
documentation) class. Sometimes when a Bean property changes, another object
might need to be notified of the change, and react to the change. Whenever a
bound property changes, notification of the change is sent to interested listeners.
The accessor methods for a bound property are defined in the same way as those
for simple properties. However, you also need to provide the event listener
registration methods forPropertyChangeListener classes and fire a
PropertyChangeEvent (in the API reference documentation) event to the
PropertyChangeListener objects by calling their propertyChange methods
The convenience PropertyChangeSupport (in the API reference documentation) class
enables your bean to implement these methods. Your bean can inherit changes
from the PropertyChangeSupportclass, or use it as an inner class.
In order to listen for property changes, an object must be able to add and remove
itself from the listener list on the bean containing the bound property. It must also
be able to respond to the event notification method that signals a property change.
The PropertyChangeEvent class encapsulates property change information, and is
sent from the property change event source to each object in the property change
listener list with the propertyChange method.
C) Constrained Properties:
A bean property is constrained if the bean supports the VetoableChangeListener(in
the API reference documentation) and PropertyChangeEvent(in the API reference
documentation) classes, and if the set method for this property throws a
PropertyVetoException(in the API reference documentation).
Constrained properties are more complicated than bound properties because they
also support property change listeners which happen to be vetoers.
The following operations in the setXXX method for the constrained property must be
implemented in this order:
Save the old value in case the change is vetoed. Notify listeners of the new
proposed value, allowing them to veto the change. If no listener vetoes the change
(no exception is thrown), set the property to the new value. The accessor methods
for a constrained property are defined in the same way as those for simple
properties, with the addition that the setXXX method throws a
PropertyVetoException exception. The syntax is as follows:
public void setPropertyName(PropertyType pt)
throws PropertyVetoException {code}
6. Describe the process of creating a table and updating its contents using
JDBC.
Creating Tables and updating:
There is currently no possibility to create tables using the SQL Server interface you
have used for queries.
In general, you can create (and delete) tables and insert/update/delete table
content using JDBC - there is a link below - and check the effect using the SQL
Server query interface. By selecting all information from a table (execute "SELECT *
FROM T" in the 'pubs' database using the query tool - if T is the table you have just
created or updated) you can see whether your commands were successful: if the
table hasn't been created then the system is going to tell you that the object
doesn't exist; if it exists you will see its content (might be empty immediately after
creation).
programming-language types into XML format and later parsing the XML to extract
the embedded programming-languages types.
Some other middleware technologies, such as IBM MQ Series, transmit only binary
data, which is efficient. However, this requires that developers write the marshaling
code that copies programming-language types into the binary buffers prior to
transmission, and the unmarshaling code to extract the programming-language
types from a binary buffer. In contrast, a CORBA IDL compiler generates the
marshaling and unmarshaling code, so that developers do not need to write (and
maintain) such low-level code.
They can use a standard web browser, such as Firefox, Internet Explorer or
Safari, as their only user interface.
They don't force the user to wait for the web server every time the user clicks a
button. This is what "asynchronous" means. For instance, gmail fetches new
email messages in the background ("asynchronously") without forcing the user
to wait. This makes an AJAX application respond much more like a "real"
application on the user's computer, such as Microsoft Outlook.
The Ajax engine works within the Web browser (through JavaScript and the DOM)
to render the Web application and handle any requests that the customer might
have of the Web server. The beauty of it is that because the Ajax engine is
handling the requests, it can hold most information in the engine itself, while
allowing the interaction with the application and the customer to happen as
asynchronously and independently of any interaction with the server.
Usually they manipulate data in XML format. This allows AJAX applications to
interact easily with server-side code written in a variety of languages, such as
PHP, Perl/CGI, Python and ASP.NET. Using XML isn't absolutely necessary, and in
fact many "AJAX" applications don't -- they use the XMLHTTPRequest object to
send and receive data "on the fly," but they don't actually bother packaging that
data as XML.
Working of Ajax:
Ajax adds a layer to web application communication models. As previously
stated, in traditional web applications communication between the
browser/client and the web server occurred directly between these two
components using HTTP requests. When users/clients request web pages, the
server sends all the HTML and CSS code at once. If the user enters information
into this page and requests more servers, the entire page must be loaded again.
When using Ajax, the page is only loaded once. Any processing or modification
caused by additional user input occurs in real time
In addition to HTML and CSS code, Ajax also downloads JavaScript files. These
three components make up the Ajax engine. So all requests for data from the
server will be sent as JavaScript calls to this engine, which is located as a
communication layer between the browser and the server. The Ajax engine or
layer processes subsequent requests that may change the appearance or
function of the web page. By requesting information from the server
asynchronously, Ajax sends page bits to request services from the server as
needed.
The engine allows these bits or responses to be displayed without reloading the
entire page. This makes web pages and web applications much more responsive
since only the necessary information is communicated between clients and
servers instead of the entire page. This process imitates the responsiveness of
desktop applications. Similarly, Ajax conducts all HTTP requests in the
background, preventing user disruption. Users can continue working while the
page sections are requested and delivered. With Ajax, the user interface is much
more closely connected to application logic.