0 ratings0% found this document useful (0 votes) 6 views26 pagesJava 4 Mark Imp PDF
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
4 mark questions
1) List the JSP elements.
2) Write short note on cookies.
3) Life cycle of isp
4) List the JDBC driver types.
5) Explain JDBS Connection steps
Cpa t-te st -Den ed Lee ee oe
7) Difference Between Servlet And JSP
8) Explain JCheckbox and JRadioButton components in swing.
optional
1) Explain two swing components: i) JList ii) JComboBox.a
12. JSP Elements Sentetsanause wsion\ 6-88
1
2
3.
A JSP can contain three important ‘ypes of elements
Scripting elements: Scripting elements enah
at
‘ant servlet. These can be.
Declarations: A JSP ete
Statements to be inserted in
le programmers to insert Java code which will
either:
‘ment that provides the embedded Java declaration
tothe Servlet class,
Expressions: A ISP element that
aluated
iit,
Provides the embedded Java expressions to be
&S Patt of the service method of the Servlet class.
Hi. Scriptlets: A JSP clement that provides the embedded Java statements to be
executed as part of the service method of the Servlet class.
iv. Comments: A ISP element that represents a comment.
Directives: Directives are messages to the JSP container i.e. the server program that
executes JSPs. Directives enable the programmer to specify page settings, to include
content from other resources and to specify custom tag libraries for use in a JSP.
Actions: Actions encapsulate functionality in predefined tags that programmers can
embed in a JSP. Actions often are performed based on the information sent to the server as
part of a particular client request. They also can create Java objects for use in JSP
scripdless.Cookies in Servlet
‘Aeookie is a small piece of information that is persisted between the mubiple cient
requests
‘A-cookie has a name, a single value, and optional attributes such as a comment, path
and domain qualifiers, a maximum age, and a varsion number
How Cookie works
By default, each request is considered as a new request. In cookies technique, we add
cookie with response from the serlat So cookie is stored in the cache of the browser.
‘Alor thal f request is sent by the user, cookie 's added with request by default. Thus, we
recognize the user as the old user.
Request «Cookie
tyReuest
2) Response + Cookie
server
Types of Cookie
“There are2 types of cookies in soviet.
1. Non-porsstent cookie
2) Persistent cookie
Non-persistent cookie
Its valid for single session only. It is removed each time when user closes the
browser.
Persistent cookie
It is valid for multiple session It is not removed each time when user closes the
browser. It is removed only if user logout or signoutAdvantage of Cookies
1. Simplest technique of maintaining the state.
2. Cookies are maintained at client side.
Disadvantage of Cookies
1. It will not work if cookie is disabled from the browser.
2. Only textual information can be set in Cookie object.Life Cycle of JSP
Last Updated : 05 Feb, 2025
ERS eee
ee oY
EMcuuetesy
Steps of JSP Life Cycle
Deer eet oar
Compilation of JSP page(Compilation of JSP into test java)
Classloading (test java to test.class)
Instantiation(Object of the generated Servlet is created)
oe uc age
Request processing(_jspService(jis invoked by the container)
method is invoked by the container)
Berne ead
Da een rca een ete a
(test java)
ep checks for syntax correctness.. Classloading
The container dynamically loads the compiled class.
. Instantiation
The container creates an instance of the generated servlet class.
This instance handles multiple requests unless explicitly configured
eo) ASIPHM ited reeled mcs eal iie)}
le This method is called only once when the JSP is first loaded.
le ‘It is used for initializing resources like database connections or
configurations.. Request Processing (_jspService())
This method is called for every request.
It cannot be overridden because it is auto-generated and declared as
final.
It receives HttpServietRequest and HttpServietResponse objects to7. JSP Cleanup (jspDestroy())
e This method is called once before removing the JSP from service.
e Itis used for releasing resources, such as closing database connections
or cleaning up open files.JDBC Driver
1. JDBC Drivers
1. JDBC-ODBC bridge driver
2. Native-API driver
3. Network Protocol driver
4. Thin driver
[JDBC Driver is @ software component that enables java application to interact with the database. There are
'ypes of JDBC drivers:
JDBC-ODBC bridge driver
Native-API driver (partially java driver)
Network Protocol driver (fully java driver)
Thin driver (fully java driver)
aeRs
1) JDBC-ODBC bridge driver
[Tine JDBC-ODBC bridge driver uses ODBC driver fo connect to the database. The JDBC-ODBC bridge driv
|converts JDBC method calls into the ODBC function calls. This is now discouraged because of thin driver.2) Native-API driver
|The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls |
native calls of the database API. It is not written entirely in java.3) Network Protocol driver
The Network Protocol driver uses middleware (application server) that converts JOBC
calls directly or indirectly into the vendor-specific database protocol. It is fully written in
java.4) Thin driver
[The thin driver converts JDBC calls directly into the vendor-spet
{thin driver. Itis fully written in Java language.
ic database protocol. That is why itis knoJava Database Connectivity with 5 Steps
1, 5 Steps to connect to the database in java
Register the driver class
Create the connection object
Create the Statement object
Execute the query
Close the connection object
geena
[There are 5 steps to connect any java application with the database using JDBC. These steps are as follow:
Register the Driver class
Create connection
Create statement
Execute queriesThe JDBC driver must be loaded into memory. In most modem Java versions, this step is
optional because the driver is automatically loaded when the connection is established.
| Example:
ons of Java
gl.cj.jdbe.Driver");
// For older v
c forName ("com.2. Create a Database Connection
tablish a connection to the database using the DriverManag: nnection () method.
This method requires the URL of the database, username, and password.
© Syntax:
Connection conn = priverMan:
getConnection (DB_URL, USER, PASSWORD) ;
© Example:
JRL = "Jdbe:mysql://loca
ering DI
string
2306 /mydatab:3. Create a Statement
A scacement object is used fo execute SQL queries.
© Options:
‘tatement: For simple SQL queries.
© Preparedstatement: For parameterized queries (safer and more efficient).
© Example:
statement4. Execute the SQL Query
Execute the SQL query using the appropriate method for the seatenent or
atement object.
Brenari
© For SELECT queries:
Resultset x T * FROM user:
executeguery (
© For INSERT, UPDATE, or DELETE queries:
rowsAffected = stmt
(1, "John" ") 5
-executeUpdate ("INSERT INTO users (id, name}
5. Process the Results
For SELECT queries, the result is returned in a Resuitset object. Iterate through the
Resultset to access the data,
© Example:
while (rs.next())
¢ id = rs.getIat ("id");
String name = re.getString ("name'
stem.out.printin("ID: * + id name):6. Close the Resources
Always close the Results
memory leaks
Lon objects to free up resources and
© Example:The main differences between AWT and Swing are given in the following table.
Basic
Comparison
SWING
AWT
Architecture | It’ follows the model view | It doesn’t follow the model-view
controller architecture. architecture.
ul It does support a Pluggable look | It doesn't support a pluggable look
and feel for UL and feel
Components It has a lot of components to | It has fewer components as
provide for UI. compared to Swing.
Independent | Itis platform Independent. It is platform-dependent.
Weight Its components are lightweight | Its components are heavyweight.
Speed If components are more used, it | Its speed would be normal if
can be slow,
components are used more.Tai
JSP
SCE Noa
Aa a eee Reel egy
forele (3
Writing code for servlet is harder than.
JSP as it is HTML in java.
ISP is easy to code as it is java in
ne
eee eee cela Tan)
Aone acerca
JSP is the view in the MVC approach
eke e
Serviet is faster than JSP.
RS a UU ae
fees Tannoy
Ree Rem aronK )
aco)
Serviet can accept all protocol
requests.
RS eae eC
In Servlet, we can override the
Rees iu us
In JSP, we cannot override its service()
method.It can handle extensive data It cannot handle extensive data
processing. processing very efficiently.
The facility of writing custom tags is The facility of writing custom tags is
not present. present.JCheckBox
A check box is a control that is used to turn an option on or off. A JCheckBox component
is a two-state button typically used in GUIs containing selectable items. It consists of a
Small box that can either contain a check mark or not. There is a label associated with cach
check box that describes what option the box represents. You change the state of a check
box by clicking on it.
When a check box is selected or deselected, it causes an ItemEvent2 JRadioButton
A radio button is similar to a check box Le. it has two states: On and Off. However, radio
b 7
uttons are used to create & mutually exclusive set
id select any one, none or all
t only one of several options
of options. For example, in the
nevi
Previous program, the user coul I check boxes. In many cases.
we w:
‘ant to require the user to select
revious should automatically be deselected. Such &
p. The JRadioButton class ant ButtonGroup
When a radio Dution
When another option is selected, the P!
ie options is radio button grou
is sein to create a group OF mutually
fed or deselected, it causes an NtemEvent.
as the JCheckBox class ConsTUCTORS.
exclusive radio buttons.
Thee
Constructors of RadioButton are sanedist
The JList class provides a compact, multiple-choice, scrolling list of alternatives w4;.
the user can select from. Single or Multiple or a range of values can be selecieg Te
javax.swing.ListSelectionModel class defines three selection my,
SINGLE SELECTION (only one item can be selected at a
SINGLE_INTERVAL_SELECTION (multiple co
MULTIPLE_INTERVAL_SELECTION (default - any combination of items can te
selected). These modes are defined as static constants of the class
time,
ntiguous items can be selected) any
The selection or deselection of a list item causes the ListSelectionEvent to occur.t
JComboBox stem cvent
‘A.combo box, also known as a choice list or drop-down list, contains a list of items from
which the user can choose. Since it is a drop-down list, it saves _space_on the GU
Additionally, a combo box allows the user to type the value in a text field, Hence, it
combines a text box and a list into a single control.
The JComboBox class creates this control. To make the combo box editable, use the
Editable. Elements can be added to a combo box using the addElement()