mi0041 – java & web design set-2
Post on 13-Jul-2015
2.431 Views
Preview:
TRANSCRIPT
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 1/14
MBA IS- 4th SEM MI0041 Java & Web Design
Assignment Set- 2
Q.1 Give one example each for the following and explain the uses
1. Web Design Tools, 2. HTML Editors, 3. Adobe Photoshop, 4. Adobe Flash, & 5. Firebug Browser
1. Web Design Tools: Design for the Web is one of the most significant areas that are emerging in the
development of websites. Web design refers to the task of developing a Web page or the user interface
of the Web page. The design of a Web page acts as a medium between people and information. The
major intention of Web design is in the development of attractive Web pages that reside on Web
servers. Web design tools concentrates more on the presentation of contents using hypertext, images,
videos, and so on.
There are various tools available today that help us develop and design Web pages. We can term these
as design tools. Design tools help us to design attractive Web pages easily and quickly. Some of the
popular Web design tools are as follows:
HTML Editors.
Adobe Photoshop.
Adobe Flash.Firebug Browser.
2. HTML Editors: HTML editor refers to a software application that we can use to create Web pages. It
provides us with various tools that are required to design a Website. Even though the HTML files can be
written using any text editor such as notepad or textpad, specialised HTML editors can provide
convenience and additional functionalities. It not only offers support to work with HTML tags but also
provides support to work with technologies such as Cascading Style Sheets (CSS), Extensible Mark-up
Language (XML) and JavaScript. They even help us to handle the interactions that happen with remote
Web server through the File Transfer Protocol (FTP) and Web-based Distributed Authoring and
Versioning (WebDAV). HTML Editor is useful as we need not have to type or remember all the HTML
tags. We can say that it is just like the word processor where we type the content and apply the requiredformats. We can see the HTML source as well as view how the Webpage would look like when viewed
on the Web browser, by switching to the display format.
3. Adobe Photoshop: Adobe Photoshop is a software application package useful in editing images and is
considered as an important tool for Web designing. It is an excellent tool to create attractive Websites.
Photoshop can read and write raster and vector image formats. For example, gif, jpeg and png. An image
in raster format is described in a table of pixels, where each pixel has a specific color.
Adobe Photoshop is considered as an image editing tool for Web design as it useful in the following
ways:
1. It helps you to tune your art work to the design of your a Web page on your Website.
2. It can be used to add graphics to your videos that you might have uploaded on your Website.
3.
It helps to create different types of web banners and buttons that would enhance the look of
your Website.
4. It also helps us to turn an image or photograph look like a work of art, that is, to look like a
painting.
5. It can be used to create a rollover effect to the images or button on your Web pages easily
without writing any code by just adding different behaviours to different images or buttons.
Here, by rollover effect we mean that we can make a particular area of an image to change
when you click or move the mouse over an image.
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 2/14
4. Adobe Flash: Adobe Flash is a multimedia graphics program that is used to create interactive Web
pages, videos and animations. It can be embedded inside any HTML program to create interactive Web
pages. Other than being used for creating interactive Web pages it is also widely used in games and
advertisements.
Adobe Flash helps you to create interactive videos on the Web. It uses vector graphics. Here, vector
graphics refers to graphics that can be scaled to any size of our choice without any loss in quality or
clarity. We need not have any kind of programming knowledge to use this software as it is very easy to
learn. It captures user input through keyboard, mouse, microphone and camera.
We will now look into some of the uses of Adobe Flash. Some of the uses of Adobe Flash are as
follows:
1. It helps us to load Flash movies at a faster pace when compared to animated GIF's. This is
because animated GIF's includes numerous images saved in one image. Thus when a Web
browser loads such an image, the browser may need to load all the frames of the image before
display. Therefore, it takes longer time for a GIF image to load.
2. It helps to create simple animations and buttons quickly and easily.
3. It supports interactivity. Thus, it helps us to control animations using Flash software. For
example, we can begin animating an image after the user clicks on a button.4. It is possible to embed the animations created using Flash into a Web page.
5. It is mostly used in the creation of vector based animations and helps in reducing the file sizes.
Reduction in the file size helps to quickly download and play movies on the Web pages.
5. Firebug Browser: Firebug is also an excellent Web development tool, which is an add-on for Mozilla
Firefox. It provides us with various tools to inspect, edit, and debug Web pages. This add-on provides us
a tabbed interface that facilitates us to edit as well as debug HTML, CSS and JavaScript codes in any Web
page. It also offers DOM (Document Object Model) inspection and appraisal related to the performance
of Websites.
Some of the uses of Firebug Browser are as follows:1. It is very easy to learn the tool as it is user friendly. It also provides you the facility to open the
Firebug window within your Web browser, as you saw in Figure 10.1, or as a separate window.
2. It lets us examine and edit the HTML tags. Whenever there is any change in the HTML code the
Firebug highlights the modifications made in yellow.
3. It even lets you make modifications to the code and see the output of the change instantly.
Q.2 Write a Java program to demonstrate the use of control statements (all three statemnts) in Java.
Answer:
We know that a program is a collection of statements that are carried out to accomplish a task which
was decided in advance. And, in a program, normally the statements are executed in sequential order,
which is called sequential execution or sequential control flow. Thus, you can define Control statements
as the statements which are used for controlling the execution flow in a program. In Java, we can classify
the control statements into three categories namely, looping statements, conditional statements, and
special control statements, as depicted in the figure below:
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 3/14
Java Control Statements
1. Looping statements: Looping statements are those statements, which enables you to execute group
of statements frequently. They are also called as iteration statements. You will notice that the JAVA
programming language has three types of looping statements/constructs, namely, for, while, and do-
while. Let us briefly describe them.
For loop: For loop is the most flexible looping statement, used for the continuous execution of a piece of code until a specific condition is satisfied. We use the following syntax of for loop.
While loop: While loop is a looping statement which frequently executes a block of statements as long
as a condition remains true, look at the syntax of while loop.
Do-while loop: Do-while loop is a looping statement, which is used like while loop (which frequently
executes a block of statements as long as a condition remains true). The difference between the do-
while and while loops is that, in the do-while loop, the statements within the loop are guaranteed to be
executed at least once regardless of the condition evaluation, do-while loop below:
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 4/14
In do-while loop, you have to initialise the control variable before the loop. Then, the statements are
executed. After the statements are executed, the given condition in the while statement is tested. If the
result of this test is true, again the statements within the body of the loop are executed. These steps are
followed as long as the condition is true. When the condition becomes false, the loop terminates and
the control goes to the statement immediately after the while statement
2. Conditional statements: Java supports three conditional statements, that is, if, if-else, switch.
If statement: If is a conditional statement, which gives permission to a program, for executing various
blocks of code, depending on the evaluation of a test.
The syntax of if statement is as follows:
As per the above syntax, if the given condition is true, the statements within the parentheses (statement
1, statement 2) are executed; otherwise, the statements are skipped and the statements after the loopare executed.
If-else statement: If-else is also a conditional statement, which is an addition to the if statement. The
general form of the if-else statement is:
Switch statement: Switch statement is also a conditional statement, which is like else-if statement withseveral else statements whose conditions are tested in sequential order.
Let us have a look at the syntax of switch statement:
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 5/14
In the above syntax, firstly the value of a given variable (expression) is tested against a list of case values.
If the program finds a match, the statements associated with that case are executed. If no match is
found, the statement in the default block is executed.
.
Flowchart of Switch statement
3. Special control statements: Break statement
Break is a special control statement, which is used for getting out from a loop before its (loops)
completion, on a pre-defined condition, or you can say that it ends the loop that immediately surrounds
it. We can use this statement with while loop, do-while loop, for loop, and switch statement.
Continue statement: Continue is also a special control statement, which is used to stop the control flow
in the program and return the control to the loop without the execution of the statements which are
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 6/14
written after the continue statement; and which tells the system to jump out from the remaining part of
the current loop. This statement is useful when we want to execute just some of the statements in a
loop. The continue statement will skip the remainder of the loop statements and evaluate the condition
of the loop as before.
Return statement: Return is the last special control statement, which is used for finally returning from a
method, that is, it tells the Java interpreter to stop the execution of the current method. If you want
your method declaration to return a value, you have to write an expression after the return statement;
and that expressions value will become the desired return value. You must have noticed that in some
programs, some methods are declared void; it means that they do not return any value.
Q.3 What are the difference types of inheritance in Java? Explain the relationship between interfaces
and inheritances.
Answer: We can define inheritance as the process of inheriting features of one object by another. If we
use inheritance, we can easily manage the information about the objects. A super class and its
subclasses have an in-a relationship. We can define such relationship using Java inheritance. It means
that you can use an object of a subclass (sometimes it is called as child class), wherever you can use an
object of the super class (sometimes it is called as parent class). In Java, we use class inheritance for
creating or building new classes from existing classes. You can analyze the same from the figure
Inheritance in Java
Inheritance types in Java:
Simple inheritance: It refers to the process of deriving/inheriting a child class simply from its parent
class. You must note that there is only one child class and its parent class, in this kind of inheritance. You
can also call it as single inheritance or one level inheritance.
example of a parent class vehicle, and a child class bus; which is shown in figure
Figure: Example of Simple Inheritance
Multilevel inheritance: It refers to the process of deriving a child class from another child class. Here,
note that the inherited class is known as child class for its parent class and this child class act as the
parent class for its child class. There may be many levels in multilevel inheritance. For example, a parent
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 7/14
class vehicle has a child class bus. This child class, bus becomes a parent class for another child class A/C
Bus. You can see this in the figure
Figure: Example of Multilevel Inheritance
Hierarchical inheritance: This type of inheritance is used for assisting the hierarchical program design. In
this type of inheritance, many features of one level (one parent class) can be inherited by many others
(child classes) below that level.
Let us analyze this by looking at the example in the figure
Figure: Example of Hierarchical Inheritance
The relationship between interfaces and inheritances:
in Java, we can achieve multiple inheritance with the help of interfaces. We can do so by exercising oneor more than one interface within a class, which results in the creation of classes which are built upon
other classes. These classes will not be constrained by the problems created by multiple inheritance. The
main problem with multiple inheritances is that it is difficult to resolve a method that is inherited from
more than one parent.
We can define an interface as a logical collection of methods. Any class implementing an interface must
define such methods. Note that such interfaces can also define constants (public static final). Only
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 8/14
abstract methods and final fields are defined in interfaces, not the code for implementing these
methods. In interfaces, data fields only comprise of constants.
look at some similarities between an interface and a class:
Both an interface and a class can have any number of methods.
We write an interface and a class in a file which has a .java extension, with the same name as the file
name.
Nevertheless, an interface and a class are different in several ways, such as:
Interface cannot be instantiated, but a class can be.
An interface does not have any constructor, but a class can have a constructor.
All the methods of an interface are abstract while a class may or may not have abstract methods.
We cannot extend an interface by a class; but can implement it by a class.
One major difference is that a class explains the properties and behaviours of an object, but an interface
explains behaviours which will be implemented by a class.
We will now see how to define interfaces, extend interfaces, implementing interfaces.
Q.4 How do you handle exceptions in JavaAnswer: A Java exception is nothing but an object that defines an exceptional condition, or an error that
has occurred in a piece of code. In Java, we can handle this exception by creating an object that
represents the exception, and throwing that object into the method that triggered the error. The
method may handle the exception by itself or pass on the exception to another method to handle it. In
either of the ways, the exception is caught and processed at some point.
There are two ways by which exceptions can occur. They are as follows:
Java run-time system: These exceptions relate to the fundamental errors that violate the rules of the
Java programming language or the constraints of the Java execution environment.
Code generated: The code that we write can manually generate exceptions. These are used to report
some error condition to the caller of a method.
Exception types: All the different types of exceptions are subclasses of the built-in
class Throwable. Therefore, Throwable is at the top most level of the exception class hierarchy. In the
next level of hierarchy are two important types of exception classes, namely, Exception and Error. Let us
now discuss these exception classes:
Exception: This class specifies the exceptional conditions that your programs should catch. The subclass
of this class will contain the custom exception types created by the program. Another important
subclass of this exception class is RuntimeException. This subclass contains the exceptions that are
automatically defined for programs and includes exceptions such as divide by zero and invalid array
indexing.
Error: This class defines exceptions that are not expected to be caught under normal circumstances byyour program. Java run-time system uses exceptions of the type Error to indicate errors that are related
to the run-time environment. An example for such an error is, stack overflow.
The exception class hierarchy is depicted in the bellow figure
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 9/14
Using try and catch: The program statements that you want to monitor for exceptions are specified
within the try block. Immediately after a try block, a catch block must be defined. This catch clause
specifies the exception type that you want to catch. Let us look into a simple program that shows howthe try and catch blocks are used.
The execution of the program has been explained in the previous units. We will now look at the output
generated by this program.
Note that the call to println() inside the try block is not executed. Once an exception is thrown, the
program control is transferred from the try block to the catch block. The execution is never returned to
the try block from the catchblock. Once the catch statement is executed, the control is transferred to
the next line in the program following the entire try/catch mechanism.
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 10/14
A try and catch statement together form a unit. The scope of the catch statement is restricted to those
statements specified by the immediately preceding try statement. The statements that are protected
by try must be written within curly braces.
The purpose of using a catch is to resolve the exceptional condition and continue with the program as if
the error never occurred.
finally is a block of code that is executed:
After the execution of the try/catch block.
Before the execution of the code following the try/catch block.
The finally block of code is executed at any cost. If there is an error in the program, the catch block is
executed, then the finally block is executed. If there are no errors, the try block is executed, then
the finally block is executed.
Let us look into an example program to analyse how finally block is used with the try and catch block.
The output of the above program is as follows:
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 11/14
In the above program, we can observe that a divide by zero error has occurred. The print statement
after this error within the try block was not printed as the exception caused the program control to be
transferred to the catch block. However, we can notice from the output of the program that no matter
which block has control and for how long, the finally block will be executed after either the try block or
catch block has completed.
Q.5 Write a small snippet in which the program uses POST method to accept the keywords to
accomplish the task. Also suggest the alternative method to perform this task and its benefits
Answer: The working of servlets begins when the client or the users Web browser passes a request to
the server. The browser uses the HTTP protocol GET method or POST method to pass a request to the
server. For example, we can invoke a servlet when we click on the submit button of a form, or by clicking
on a hyperlink that is present in a Web page. Once the servlet processes the request, the client or the
Web browser gets the output in the form of a HTML page. We have noted that the request made by the
client or the Web browser consists of few components.
They are as follows: Protocol: This refers to protocols such as HTTP that is used for interaction between the server and the
client. Request type: This refers to the GET or POST method to send the request from the client to the
server.
Uniform Resource Locater (URL): This refers to the URL of the document that needs to be retrieved or
the JSP/servlet that will process the request.
Query string: This refers to the additional information such as the user name and password that is
usually present in the form data that the client fills in the browser.
GET method or doGet(): It is used to get additional information from the user or the client along with
the URL to describe what the client needs as output from the server. For example, the additional
information could be your user name and password. This additional sequence of characters that are
added to the URL is known as the query string. This process can happen when a client sends a request to
the server. However, the disadvantage of this method is that the query string is visible on the Web
browser and thus poses a security problem.
For example, if you want to access your account using your user name and password, you will see the
following URL displayed on the address bar of your Web browser, when you use a GET method:
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 12/14
http://www.smu.com/login.html?username="Greg"&paswd="12@greg@45"
However, we can use GET method to retrieve non confidential information. We should also note that
the length of the query string is limited to 250 characters.
POST method or doPost(): It is used to overcome the security issue posed by the GET method because
the POST method sends data as packets using a separate socket connection. Therefore, the entire
transaction is invisible to the clients. However, even this method has a disadvantage, that is, this
method is slower when compared to GET method. This is because the data is sent to the server as
separate packets.
For example, when you access your account using your user name and password, you see the following
URL displayed on the address bar of your Web browser, when you use a POST method:
The life cycle of a servlet comprises three stages. They are as follows:
1. Initialisation/init().
2. Service/service().
3. Destruction/destroy().
Figure 14.2 depicts the life cycle of a servlet.
Initialisation/init(): This phase begins the life of a servlet. It represents the creation and initialisation of
resources that the servlet may require to respond to a clients request. The method used to initiate a
servlet is init(). This method is invoked when the client or Web browser requests for a Web page from
the server. We have to note that this method gets invoked only once, that is when the servlet is first
loaded into the memory. Subsequent client requests to the same servlet do not run this method.
Service/service(): This is the second phase of a servlet life cycle. This phase represents all the
interactions along with the request till the servlet is destroyed. The method used for this purpose is
service(). This method processes the requests sent by the client and then sends a response back to the
client. The service() method is called for every client request.
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 13/14
Destruction/destroy(): This is the final phase of the servlet life cycle. It represents the removal of the
servlet from the container. The method used in the destruction of the servlet is destroy(). When a Web
application is shut down, the servlets destroy() method is called. This method closes all the resources
that were created in the initialisation phase.
Q.6 Explain Java web technologies with its types?
Answer: Java technologies:
1. Java Data Base Connectivity (JDBC): Java Data Base Connectivity is a Java application that is available
in all the latest versions of JDK. This is an application developed by a private organisation called JavaSoft.
This is an Application Program Interface (API) and it provides a common interface for a number of
databases and enables you to build higher level tools and interface applications.
The JDBC API enables you to define Java classes to establish connections between databases, generate
Structured Query Language (SQL) statements and so on. The SQL is the standard programming language
used to create, maintain and search for data in large databases.
JDBC has become the most popular standard for data access. JDBC helps the developers working with
Java to develop a data access application without having knowledge of complex API's of differentdatabase available in the market. They can link different type of databases and form a single database
with the help of JDBC easily.
Regardless of the platform that you use, JDBC helps you to establish an interface between the database
and Java application. Usually, this interface is established using the JDBC drivers. These drivers are a set
of classes and interfaces that helps your Java application to interact with the databases.
Basically, there are two types of JDBC architecture: the JDBC 2-tier and JDBC 3-tier architectures.
JDBC 2-tier architecture: In this architecture the Java application communicates directly with the
database through JDBC. The database may be located on the same system or on another computer in
the network. Therefore, it is called two tire or two layered architecture. The JDBC and the Java
application together form one tier that will directly communicate with the second tier, i.e., the database
and access the required information.JDBC 3-tier architecture: In this architecture along with the two layers that we learnt in 2-tier
architecture, there is one more layer in between these two layers. This middle tier or layer acts a link
between the JDBC and database. First, the request from the Java application will be sent to the middle
layer, the middle layer then sends the request to the database. The database again sends back the
requested information to the middle tire and then to the Java application via JDBC. The purpose of the
middle layer is to handle any application or business specific rules for applications that access the
database.
2. Struts: struts is developed using the technologies, the JSP and servlets. It proves to be more efficient
and effective in developing Web applications.
The Apache struts offer two major versions of struts framework, the struts 1 and struts 1.x frameworks.
The "struts 1" is still the most popular framework for Web based applications. Even though "struts 1.x" is
a more advance framework than "struts 1", "struts 1" is still the most preferred choice of most of the
Web developers.
Advantages of struts:
It enables you to make global changes in the Web documents by editing a single file and these changes
can be made without recompiling or modifying the Java code.
5/12/2018 MI0041 – Java & Web Design set-2 - slidepdf.com
http://slidepdf.com/reader/full/mi0041-java-web-design-set-2 14/14
It provides you a collection of JSP tags that will help you to easily output the properties of JavaBeans
components. A JavaBean component provides standard methods to fetch and update information in an
object.
It provides a group of JSP tags to create HTML forms that are associated with the JavaBeans
components.
It helps you to validate the contents of form fields. This is possible since the struts can check if the form
values are in proper format as per the requirement. If there is any error or mistake in the format, the
form can be automatically redisplayed with the error message.
3. Java Sever Faces (JSF): JSF is a new standard framework for Web application development using Java.
You can consider JSF framework as a toolbox that contains a large set of components which you can use
quickly and easily as per you requirements. You can use and reuse the JSF components in the same Web
page, a number of times. The JSF is available as a part of JDK
Benefits of using JSF:
It provides standard and reusable components which you can use to create user interfaces for Web
applications. It provides many tags for accessing and manipulating the components. The form data is
automatically saved and repopulated when it is displayed at client side.Many Graphic User Interface (GUI) components are available in JSF that makes Web based applications
based on JSF framework more simple and effective.
two types of components in JSF:
Standard UI components: This type contains most of the basic set of UI components such as text fields,
check boxes, list boxes, radio buttons and so on (refer Unit 3 for component description). Some of the UI
components that you can find in JSF are:
UIForm: This represents a Web form that contains various other components to make it a component
i.e. number of components are grouped together to form a single component.
UICommand: This represents UI components like buttons, hyperlinks, menu items and so on.
UIInput: This represents input UI components such as text input fields, numeric input fields and so on.Custom UI components: Usually, while designing UIs you need some different and stylish components to
make the component appear more attractive for example, fancy calendar, stylish table borders and so
on. These are not standard type of JSF components but, designed as per the requirement. Therefore,
they are called custom UI components. Thus, with the help of this the JSF enables you to create and use
your own set of reusable components.
features of JSF:
It is a standard Web user interface framework for Java based applications.
It is built with the servlet API as its base architecture.
It is a component-based framework and hence highly flexible.
User Interface (UI) components can be stored on the server.
Components from other technologies can be used effectively.
top related