0% found this document useful (0 votes)
15 views21 pages

Final - MOST IMP QUESTION FOR GTU - 2020

The document contains questions related to Java applets, AWT, Swing, Servlets, JSP and JDBC. It asks to explain the life cycle of an applet, differentiate between local and remote applets, explain the <applet> tag and its attributes, use the <param> tag in an applet example, and differentiate between applets and applications. It also contains questions on AWT hierarchy, layout managers, frames, difference between AWT and Swing, event handling in Java, JDBC architecture, and differences between servlets, JSPs and other Java technologies.

Uploaded by

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

Final - MOST IMP QUESTION FOR GTU - 2020

The document contains questions related to Java applets, AWT, Swing, Servlets, JSP and JDBC. It asks to explain the life cycle of an applet, differentiate between local and remote applets, explain the <applet> tag and its attributes, use the <param> tag in an applet example, and differentiate between applets and applications. It also contains questions on AWT hierarchy, layout managers, frames, difference between AWT and Swing, event handling in Java, JDBC architecture, and differences between servlets, JSPs and other Java technologies.

Uploaded by

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

AJAVA (3360701)

GTU IMP QUESTION


1) Explain Life cycle of APPLET
2) Difference between Local and Remote APPLET.
3) Explain and list <applet>tag and its attributes.
4) Explain <PARAM> tag in applet using example.
5) Differentiate APPLET and Application.
6) Discuss the advantage and disadvantage of Applet.
7) Explain AWT Hierarchy.
8) List Layout Manager in AWT and explain any two with Example.
9) Define Frame and two ways to create Frame.
10) Give the difference between AWT and SWING.
11) Explain event delegation model in java.
12) List out Event Class and Event Listener.
13) Explain JDBC Architecture.
14) List and Explain Type of JDBC Driver and explain any one with Example.
15) Write and explain steps to connect java application to Database.
16) Explain/List JDBC API components in brief.
17) Write Advantage and disadvantage of JDBC.
18) Give difference between Servlet and Applet.
19) Explain life cycle of Servlet.
20) Explain the difference between doGet () Request and doPost ().
21) Give difference between Servlet and JSP.
22) Explain/list JSP Expressions scripting element tag with an example.
23) Explain life cycle of JSP.
24) Explain JSP declaration tag with example.

By: Shweta Patel (Lecturer in CE VPMP) 1


AJAVA (3360701)

1) Explain Life cycle of APPLET


Ans:
 The applet states are:
1. Born or Initialization state
2. Running state
3. Idle state
4. Dead or destroyed state

Life cycle of applet

1) Initialization state(Born)
 Applet enters the initialization state when it is first loaded,this is achieved by calling the init()
method of Applet class.
 Initialization occurs only once in the applet’s life cycle.
 To provide any of behaviour mentioned above,we must overrride init() method:
public void init()
{
............(Action)
............
}
2) Running state
 Applet enters the running state when the system calls start() method of Applet class.
 This occurs automatically after applet is initialized.
 Unlike init() method,the start() method may be called more than once.
 We may override the start() method to create a thread to control the applet.
public void start( )
{
............(Action)
............
}

3) Idle or stopped state


 An applet becomes idle when it is stoppped from running.

By: Shweta Patel (Lecturer in CE VPMP) 2


AJAVA (3360701)

 We can also stop applet by calling the stop() method explicitly.


 To do this override stop() method:
public void stop()
{
..................(Action)
..................
}
4) Dead state
 An applet is said to be dead when it is removed from memory.
 This occurs automatically by invoking the destroy() mehtod when we quit the browser.
public void destroy()
{
...............
................
}
5) Display state
 Applets moves to the display state ,whenever it has to perform some output operations on the
screen.
 The paint() method is called to complete this task.
 Display state is not considered as a part of the applet’s life cycle.
 The paint() method is defined in the applet class.it is inherited from the Component class ,a
super class of Applet.
public void paint()
{
.............(Display statements)
.............
}
2) Difference Between Local and Remote APPLET
Ans:
Local Applet Remote Applet
It is stored locally on a standalone It is stored remotely on server
computer. computer.
It does not require an internet It required an internet connection.
connection.
It can be accessed only in a local It can be accessed by all computers
computer. connected with server
Code Base attribute is optional for Code Base attribute is Mandatory for
Local Applet. Remote Applet.

3) Explain and list <applet>tag and its attributes.


Ans:
< APPLET
[CODEBASE= codebase_URL]
CODE=AppletfileName.class
[ALT = Alternate_text]
[Name = applet_instance_name]
WIDTH = Pixels
HEIGHT = Pixels

By: Shweta Patel (Lecturer in CE VPMP) 3


AJAVA (3360701)

[ALIGN= Alignment]
[VSPACE = Pixels]
[HSPACE = Pixles]
[<PARAM NAME=name1 VALUE= value1 >]
[<PARAM NAME=name2 VALUE= value2 >]
..............
</APPLET>

NO Attribute Meaning
CODE=AppletfileName.class Specifies the name of the applet class to be
1 (necessary) loaded.
CODEBASE= codebase_URL Specifies URL of the directory in which the
2 (Optional) applet resides.(must used in remote applet)
WIDTH = Pixels Specify width and height of the space on the
3 HEIGHT = Pixels(necessary) HTML page that will be reserved for applet.
Name = applet_instance_name A name for the other applet on the page may
4 (Optional) refer to this applet.
[ ALIGN= Alignment] Alignment of applet on page.
5
(Optional)
[ VSPACE = Pixels ] It specifies amount of vertical blank space the
6
(Optional) browser should leave surrounding the applet.
[ HSPACE = Pixels] It specifies amount of horizontal blank space the
7
(Optional) browser should leave surrounding the applet.
[ALT = Alternate text ] Non _java browser will display this text where
8
(Optional) the applet would normally go.

4) Explain <PARAM> tag in applet using example


Ans:
 <PARAM ...> tag is used to supply user defined parameters to applet.
 Passing parameter to an appplet code using <PARAM> tag is similar to passing
parameters to the main() method using command line argument.
 getParameter() method is used to get parameter from <PARAM> tag.
 Each <PARAM...> tag has a name attribue and value attribute .
Syntax:
<APPLET ......>
<PARAMname=color value=“red”>
<PARAMname=text value=“I like java!”>
</APPLET>
Example:
import java.applet.*;
import java.awt.*;
/*
<applet code=ParamTest.class width=500 height=300>
<param Name="subject" value="Hello java">
</applet>

By: Shweta Patel (Lecturer in CE VPMP) 4


AJAVA (3360701)

*/
public class ParamTest extends Applet
{
public void paint(Graphics g)
{
String s=getParameter("subject");
g.drawString("Name:="+s,50,50);
}
}
Output:

5) Differentiate APPLET and Application.


Ans:
APPLET APPLICATION
An Applet is an application Application is a program that runs on
designed to be transmitted over the your computer under the operating system
Internet and executed by a Java- of that Computer.
compatible Web browser.
Applet is dynamic program which is Application is static program which run
run on browser. using java interpreter.
Applet do not use the main () method
Application uses main () method for
for initiating the execution of the
execution of the code.
code.
Applets cannot be run Application runs independently using
independently. javac compiler.
Applets cannot read or write files on Application can read write files on the
the web user’s disk. web user’s disk.
Application program can use methods of
Applets can’t use libraries from
c, c++ libraries.
other languages such as c, c++.

6) Discuss the advantage and disadvantage of Applet.


Ans:
Advantage
 Applet automatically integrated with HTML.

By: Shweta Patel (Lecturer in CE VPMP) 5


AJAVA (3360701)

 Applet provides client-side functionality.


 Applet can be accessed from various platform and various java-enabled web-
browsers.
 Applet can provide dynamic, graphics capabilities.
 Implemented in JAVA and easy-to-learn object oriented Programming.
 Provide safe security.
Disadvantage
 Applet has limited access to the local and remote file systems.
 Applets don’t access client-side resources, like-Files, Operating system
 Applet cannot work with native methods.
 It requires the Java plug-in.
7) Explain AWT Hierarchy.
Ans:

1) Component
 Component class is derived from object class.
 It is the super class for all AWT components class such as Label, TextComponent,
RadioButton, and CheckBox etc.
2) Container
 Container class is derived from component class.
 The Container is a component in AWT that can contain another component like buttons,
textfields, labels etc. The class that extends Container class is known as container such as
Frame, Dialog and Panel.
3) Windows
 Windows class is derived from container class.
 The window is the containers that have no borders and menu bars. You must use frame,
dialog or another window for creating a window.
4) Panel
 Panel class is derived from container class.
 The Panel is the container that doesn't contain title bar and menu bars. It can have other
components like button, textfield etc.
5) Frame

By: Shweta Patel (Lecturer in CE VPMP) 6


AJAVA (3360701)

 Frame class is derived from container class.


 The Frame is the container that contain title bar and can have menu bars. It can have other
components like button, textfield etc.
8) List Layout Manager in AWT and explain any two with Example.
Ans:
 Layout means arrangement of a component in applet, panel, and frame using layout
manager.
 We can arrange component in AWT windows.
 There are five Layout managers in java.
1) FlowLayout Manager
2) GridLayout Manager
3) BorderLayout Manager
4) CardLayout Manager
5) GridBagLayout Manager
1) FlowLayout Manager
 It is the default layout for applet and panel.
 It place controls in the order in which they are added linearly from left to right and
from top to bottom in horizontal and vertical rows.
 We can align component to left, right, center.
 By default components are cantered in applet.
Constructor for FlowLayout:
a) FlowLayout ():-FlowLayout with center alignment.
b) FlowLayout (int align):-FlowLayout with specified alignment
(LEFT,RIGHT,CENTER)
c) FlowLayout (int align, int h, int v):-FlowLayout with specified alignment and
horizontal and vertical gap.
Example:
import java.applet.*; import java.awt.*;
/* <applet code=Flowtest.class width=150 height=400>
</applet> */
public class Flowtest extends Applet
{
Button bt1=new Button("ok");
Button bt2=new Button("cancel");
Button bt3=new Button("1");
Button bt4=new Button("2");
Button bt5=new Button("3");
Button bt6=new Button("4");
FlowLayout f3=new FlowLayout(FlowLayout.CENTER);
public void init()
{
setLayout(f3);
add(bt1);
add(bt2);

By: Shweta Patel (Lecturer in CE VPMP) 7


AJAVA (3360701)

add(bt3);
add(bt4);
add(bt5);
add(bt6);
}}
Output:

2) BorderLayout Manager
 It is the default layout for Frame and all windows.
 In Border Layout components are added to the edges of container.
 Border Layout provides five areas to place components like North, South, East, West,
and Center.
Constructor for BorderLayout:
BorderLayout ():-BorderLayout with empty direction.
Example: BorderLayout b1=new BorderLayout ();

Example:
import java.applet.*;
import java.awt.*;
/*<applet code=BorderDemo.class width=100 height=100>
</applet>*/
public class BorderDemo extends Applet
{
Button bt1=new Button("1");
Button bt2=new Button("2");
Button bt3=new Button("3");
Button bt4=new Button("4");
Button bt5=new Button("5");

BorderLayout b1=new BorderLayout ( );

public void init()


{
setLayout(b1);
add("North",bt1);

By: Shweta Patel (Lecturer in CE VPMP) 8


AJAVA (3360701)

add("South",bt2);
add("Center",bt3);
add("East",bt4);
add("West",bt5);
}}
Output:

9) Define Frame and two ways to create Frame.


Ans:
Definition:
 A frame is a top-level window with a title and border.
 The frame class is used to provide the main window of an application.
 The Default layout of Frame is Border Layout.
 There are two way to create Frame.
1) By extending Frame class
2) By creating object of Frame class.
1) By extending Frame class
Example:
Import java.applet.*;
Import java.awt.*;
Public class FrameDemo extends Frame
{
FrameDemo ()
{
this.setVisible (true);
this.setSize (300,300);
this.setTitle (“My Frame”);
}
Public static void main (String args [ ])
{
FrameDemo d1=new FrameDemo ( );
}
}
Output:

By: Shweta Patel (Lecturer in CE VPMP) 9


AJAVA (3360701)

2) By creating object of Frame class.


Example:
Import java.applet.*;
Import java.awt.*;
/*<applet code=FrameDemo.class width=500 height=600>
</applet>*/
Public class FrameDemo extends Applet
{
Public void init ( )
{
Frame f1=new Frame ( );
f1.setVisible (true);
f1.setSize (300,300);
f1.setTitle (“My Frame”);
}
}
Output:

10) Give the difference between AWT and SWING.


Ans:
AWT SWING
AWT stands for Abstract windows toolkit. Swing is also called as JFC’s (Java
Foundation classes).
AWT components are called Heavyweight Swing components are called light weight
component. component
AWT components require java.awt package. Swing components require javax.swing
package.
AWT components are platform dependent. Swing components are made in purely java
and they are platform independent.
This feature is not supported in AWT.  We can have different look and feel in Swing.

By: Shweta Patel (Lecturer in CE VPMP) 10


AJAVA (3360701)

11) Explain event delegation model in java.


Ans:
 The changes of state of object are known as Event.
 For example, Button click event.
 Event Handling is the mechanism that controls the event and decides what should
happen if an event occurs.
 This mechanism has the code which is known as event handler, which is executed when
an event occurs.

 There are three participants in Event Handling Delegation Model :-


1. Event Source: The class which generates the event.
2. Event Listener: The classes which receive notification of events.
3. Event Object: The Class which describes the events.
 We have to follow Three steps in Event Delegation Model :-
1. Implement the desired Listener interface with event class and package.
Example:
import java.awt.*;
public class Test extends Applet implements ActionListener
2. Register method of Listener with Component.
Example:
b1.addActionListener (this);
3. Implement method of Listener interface in the class.
Example:
public void actionPerformed (ActionEvent e)
{
//Code;
}

By: Shweta Patel (Lecturer in CE VPMP) 11


AJAVA (3360701)

12) List out Event Class and Event Listener.


Ans:
EventClass EventListener
ActionEvent ActionListener
KeyEvent KeyListener
MouseEvent MouseListener
WindowEvent WindowListener
ItemEvent ItemListener

13) Explain JDBC Architecture.


Ans:

The JDBC API supports both two-tier and three- tier architecture for database access.
Two-tier Architecture
 Two-tier is based on client-server application.
 Two-tier Architecture provides direct communication between Java applications and
database.
 It requires a JDBC driver that can help to communicate with the particular database.
 Query or request is sent by the user to the database and results are received back by the
user.
 The database may be present on the same machine or any remote machine connected via a
network.
 This approach is called client-server architecture or configuration.

Advantages of Two-Tier Architecture:


 Provides high performance
 Easy to maintain and modify as compare to 3-tier architecture
 Communication is faster
 Provides less complexity
 Least expensive
 Multiple user can access data from anywhere
Disadvantages of Two-Tier Architecture:
 Performance is reduced when client increases
 Faster when there is less number of users use it.

By: Shweta Patel (Lecturer in CE VPMP) 12


AJAVA (3360701)

Three-tier Architecture
Three –tier architecture are contain three Layers.
1) Client Layer(Presentation Layer)
2) Business Layer(Application Layer\Middle Layer)
3) Database Layer.
 In this, there is no direct communication.
 Requests are sent to the middle tier i.e. HTML browser sends a request to java application
which is then further sent to the database.
 Database processes the request and sends the result back to the middle tier which then
communicates with the user.
 Increases the performance and simplifies the application deployment.

Advantages of Three-Tier Architecture:


 It provides better security
 Performance is high compare to 2-tier
 Provide high degree of flexibility
 Faster in communication as compare to 2-tier architecture
 Provides improve data integrity
 Easy to maintain and modify
Disadvantages of Three-Tier Architecture:
 Increases complexity.
14) List Type of JDBC Driver and explain any two with Example.
Ans:
 JDBC driver is a software component that enables java application to interact with the
database.
 There are 4 types of JDBC driver:
1) Type-1: JDBC-ODBC bridge driver
2) Type-2: Java Native API driver
3) Type-3: Java Net-protocol driver
4) Type-4: Pure java driver / Thin driver
Type-1: JDBC-ODBC Bridge Driver
 This is also known as Type-1 bridge driver.

By: Shweta Patel (Lecturer in CE VPMP) 13


AJAVA (3360701)

 In, type-1 driver a JDBC Bridge is used to access ODBC driver installed on each client
machine.
 Type-1 driver translate all JDBC calls into ODBC calls and sent them to ODBC driver.
 The ODBC driver depends on native library of the OS.
 This driver is platform dependent.
 Java 8 doesn’t support JDBC-ODBC driver.

Advantages:
 Easy to use and install.
 Can be easily connected to any database.
Disadvantages:
 Not suitable for applet programming.
 Install on each client machine.
 Performance decrease because of JDBC call to ODBC call.

Type-3: Java Network (net) - Protocol Driver

 This driver is also known as Type-3 or pure java driver for database middleware.
 This driver uses the 3-tier model.
 It is fully written in java.
 It converts JDBC call to directly or indirectly into Vendor specific database protocol.
 These types of drivers are
very flexible.

Advantages:

By: Shweta Patel (Lecturer in CE VPMP) 14


AJAVA (3360701)

 A single driver can handle any database provided by middle tier.


 It can be used in any web application and no need to install any software on client
machine.
Disadvantages:
 It increases complexity because at middleware we develop the database coding.
 Client machine must support network protocol.

15) Write and explain steps to connect java application to Database.


Ans:
There are following five steps to create database connection with java application:
Step-1: Register the Driver
Step-2: Create Connection
Step-3: Create SQL Statement
Step-4: Execute SQL Query
Step-5: Close the Connection

STEP-1: REGISTER THE DRIVER


 The Class.forName () method is use to register the driver class dynamically.
 For example: Class.forName (“com.mysql.jdbc.Driver”);
STEP-2: CREATE CONNECTION
 The DriverManager class provides the getConnection () method to establish (create)
connection object. It requires to pass database URL, username and password.
 For example:
Connection cn=new getConnection (“jdbc: mysql: //localhost:3306/Diamond”,”root”,”
”);
STEP-3: CREATE SQL STATEMENT
 The connection interface provide the createStatement () method to create Sql statement.
 For example: Statement st=cn.createStatement;
STEP-4: EXECUTE SQL QUERY
 The statement interface provide executeQuery () method to execute SQL Statement.
 For example:
ResultSet rs=st.executeQuery (“SELECT * FROM student”);
While (rs.next ())
{
System.out.print (rs.getInt (“id”) + “\t”);
System.out.print (rs.getString (“Name”) + “\t”);
System.out.println (“”);
}

STEP-5: CLOSE THE CONNECTION


 The connection interface provide close () method to close the database connection.
 For example: cn.close();
16) Explain/List JDBC API components in brief.
Ans:

By: Shweta Patel (Lecturer in CE VPMP) 15


AJAVA (3360701)

 The JDBC API provides the facility for accessing the relational database from the java
programming language.
 The JDBC API provides the following interfaces and classes:
Driver Manager:
 Driver Manager is very important part of JDBC architecture.
 The main responsibility of JDBC driver manager is to load all the drivers found in the
system.
 JDBC driver manager is a class in JDBC API.
 The object of this class connects java application to a JDBC driver.
 The JDBC driver also helps to select the most appropriate driver from the previous
loaded drivers when a new open database is connected.
Driver:
 This interface handles the communication with the database server.
 You will interact directly with driver object very rarely.
 Using driver manager object we can manage object of this type.
 It also implements the details associated with the working drivers object.
Connection:
 This interface with all methods for contacting the database.
 The connection object represents communication context
 i.e.: all communication with the database is through connection object only.
Statement:
 This interface is created to submit the sql statement to the database.
 Same derived interface except parameters in addition to execute stored procedures or
functions.
ResultSet:
 This interface stores data retrieve from database after executing some SQL statement.
 It acts as an iterate to allow to move data to the next data.
SQLException:
 This class handles errors that occur in database application.
17) Write Advantage and disadvantage of JDBC
Ans:
Advantages of JDBC:
 Using JDBC, we can read any type of database if proper drivers are installed.
 JDBC provides full access to metadata facility.
 No content conversation is required in JDBC.
 JDBC provides existing enterprise data.
 We can create XML structure of data from database automatically.
 JDBC supports query and store procedure.
 JDBC require zero configurations for network computers.
 JDBC is simplified enterprise development.
 JDBC support modules.
Disadvantages of JDBC:
 Correct drivers are needed for deployment of each type of database.
 Can’t update or insert multiple tables with sequence.

By: Shweta Patel (Lecturer in CE VPMP) 16


AJAVA (3360701)

 When multiple connections are created or closed it affects the performance of database.
 Creating a connection over and in this environment is too expensive.
18) Give difference between Servlet and Applet.
Ans:
Servlet Applet
A java Servlet executes on the Web A java Applet executes on the Web
Server in response to requests from a Browser.
Web Browser.
Servlet have no GUI. Applets are rich and powerful GUI
Servlet is server side technology. Applet is client side technology.
Servlet are more powerful than Java Applets are limited to certain
Applet. operations on the Browser.
Servlet can be used for server side data Applets can be used for client side
validation. data validation.

19) Explain life cycle of Servlet.


Ans:
The life cycle contains the following steps: -
Load Servlet class.
1) Create Instance of Servlet.
2) Call the Servlet init () method.
3) Call the Servlet service () method.
4) Call the Servlet destroy () method

1) Load Servlet class.


 The Servlet class is loaded when the first request for the Servlet is received by the web
container.
2) Create Servlet instance.
 After the Servlet class is loaded, web container creates the instance of it.
 The Servlet instance is created only once in the Servlet life cycle.
3) Call the Servlet init ( ) method.
 Once instance of Servlet is created, the web container calls the init ( ) method only once
after creating the Servlet instance.

By: Shweta Patel (Lecturer in CE VPMP) 17


AJAVA (3360701)

Syntax:
public void init (ServletConfig config) throws ServletException
{
//initialize code…
}
4) Call the Servlet Service ( ) method.
 The container calls the service () method each time the request for Servlet is received.
Syntax:
Public void service (ServletRequest request, ServletResponce response) throws
ServletException, IOException
{
//code…
}

5) Call the Servlet Destroy ( ) method


The web container calls the destroy () method before removing Servlet instance giving a chance
for cleanup activity.
20) Explain the difference between doGet () Request and doPost ().
Ans:
doGet() doPost()
Data is sent in header to the server. Data is sent in request body.
Get request can send only limited Large amount of data can be
amount of data. sending in Post method.
Get request is not secured because data Post request is secured because data
is exposed in URL. is not exposed in URL.
Get request is more efficient. Post request is less efficient.
Get request can be bookmarked. Post request is not bookmarked.

21) Give difference between Servlet and JSP.


Ans:

JSP Servlet
JSP is a file which consists of HTML tag Servlet is a java class.
and scripting code.
Files are saved using .jsp extension Files are saved using .java extension
It is server side scripting language which is Servlet is special program of JAVA that
used to develop dynamic web pages. is already compiled and used to create
dynamic web contents.
JSP run slower as compared to Servlet Servlet runs faster as compared to JSP
because we need to compile the code because Servlet is already compiled.
written in JSP to convert it in Servlet code.
Coding is easy in JSP as compared to Coding is difficult in Servlet as compared
Servlet. to JSP.
JSP are supported to HTTP protocol only. Servlet are supported to HTTP, FTP and
SMTP protocols.

By: Shweta Patel (Lecturer in CE VPMP) 18


AJAVA (3360701)

22) List scripting Elements of JSP and Explain Expressions scripting element tag with an
example.
Ans:
The scripting elements provides the ability to insert java code inside the JSP
There are mainly three types of scripting elements:
1. Scriptlet tag
2. Expression tag
3. Declaration tag
1) Scriptlet tag
 It is used to execute java source code in JSP.
 A Scriptlet can contain any number of JAVA language statements, variable or method
declarations, or expressions that are valid in the page scripting language.
Syntax of Scriptlet: <% code fragment %>
 You can write Scriptlet tag in XML as follows: <jsp:scriptlet> Code fragment </jsp:
Scriptlet>
Example: Write a program to print Hello world in JSP
<html>
<head>
<title>JSP Hello World</title> </head> <body>
<% out.println("Hello World<br>"); %> </body> </html>
Output: Hello World
2) Expression tag
 The code placed within JSP expression tag is written to the output stream of the
response. So you need not write out.print () to write data.
 It is mainly used to print the values of variable or method.
 Syntax: <%= statement %>
Example of JSP expression tag
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
Output: welcome to Jsp
3) Declaration tag
 The JSP declaration tag is used to declare fields and methods.
 We can declare static member, instance member, and method inside declaration tag.
 The code placed in this tag must be end with semicolon (;).
 Syntax: <%! Field or method declaration %>
Example of JSP expression tag
<html>
<body>
<%! Int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>
Output: Value of the variable is:50

By: Shweta Patel (Lecturer in CE VPMP) 19


AJAVA (3360701)

23) Explain life cycle of JSP.


Ans:
JSP page looks like a HTML page but is a Servlet.
It is not executed directly.
The JSP page follows these phases:
1) Translation of JSP Page
2) Compilation of JSP Page
3) Class loading (class file is loaded by the classloader)
4) Instantiation (Object of the Generated Servlet is created).
5) Initialization (jspInit () method is invoked by the container).
6) Request processing (_jspService () method is invoked by the container).
7) Destroy (jspDestroy () method is invoked by the container).

Translation:
 JSP container checks the JSP page code and converts it to generate the Servlet source
code.
 After JSP page is translated into Servlet the entire request for that JSP page are served
by its related Servlet class.
Compilation of JSP Page
 In this Java source code for the related Servlet is compiled by the JSP container. The
container converts source code into byte code.
Class loading:
 In this Servlet class file is loaded by the class loader.
Instantiation:
 Object of the Generated Servlet is created.
Initialization:

By: Shweta Patel (Lecturer in CE VPMP) 20


AJAVA (3360701)

 In this instantiated object is initialized. For this jspInit () method is invoked by the
container.
 JspInit ( ) method declared in JSP page and it is implemented by JSP container.
 This method called once in JSP life cycle to initialize it with config params configured
in deployment descriptor.
Request processing:
 In this stage of JSP life cycle only those objects of a JSP equivalent Servlet that
initialized successfully are used by the web container to handle client request.
 _jspService () method is invoked by the container for each client request by passing
request and response object.
Destroy:
 If a Servlet container decides to destroy a Servlet instance of a JSP page Destroy ()
method is invoked by the container.
 It clean up the environment .It is called only once in JSP life cycle.
 Note: jspInit (), _jspService () and jspDestroy () are the life cycle methods of JSP.
24) Explain JSP declaration tag with example.
Ans: See in Question -22

By: Shweta Patel (Lecturer in CE VPMP) 21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy