Advanced Java
Advanced Java
web.xml
- also called as deployment descriptor file/configuration file.
- It contains instructions to container and server about various web
component of the web app
- Servlet configuration : passing info about servlet class to tServlet
class. <servlet> and sub tags
- Servlet Mapping : linking incoming request url of browser with servlet
comp.
3. Destruction Event
a. SC is about to destroy class obj. Also one time executing
block.
b. destroy() is invoked. we place uninitialization logics like
closing streams, closing jdbc.
c. when the webapp is reloaded, undeployed or when the
server is stopped or restarted or crashes.
What is the Lazy loading (or) when servlet object is created in server?
- On First request made by the web client, the web server creates
objects to the servlet class and calls init() method at same time. This
is also called Lazy loading.
- So, First request will always slow (takes time to create object and call
init() then service() method). 2nd Request onwards only service() will
be called.
ServletConfig:
- It is a memory holds data in key=value format, It is used to pass input
data to initmethod mainly.
- in web.xml file under <servlet> tag write code like
<init-param>
<param-name>mydata</param-name>
<param-value>Hello</param-value>
</init-param>
=>Then server created objects with data like.
=>To read data in init() method code is:-- String val =
config.getInitParameter(“key”); Here <param-name> behaves like key
<param-class> behaves like value
ServletContext:
- It is also called as global memory for all Servlets. =>It will store data
in key = value format both are type String only. =>To get
ServletContext object code is:
- In init() method :using config ServletContext sc =
config.getServletContext(); =>in service() method : using request
ServletContext sc =req.getServletContext(); ****)To store data
globally using web.xml
Syntax is:--
<contex-param>
<param-name>key</param-name> <param-value>val</param-value>
</context-param> ***)To read this, syntax is:--
String val=sc.getInitParammeter(“key”)
Two Types :
1. InMemroy Cookies :
a. allocate memory inside browsers memory [ RAM]
b. destroyed onced brwoseer window is closed.
c. suitable for ST
2. Persistent Cookies :
a. Allocate memory in clients machine HDD.
b. will not be deleted
c. suitable for remember me option.
Session Tracking ?
1. Hidden Form Fields :
- No data secrecy . Can have only text data. Not recommneded.
- Syntax :
- <input type=”hidden” name=”hello” value=”world”>
- String s = req.getParameter(“hello”):
2. Http Cookies
- allocate memory at client side no burden on server.
- Persistence cookies maintain expire data
- can add only text contect , can be viewed in browser. can be deleted
in middle fo session.
3. HttpSession with Cookies:
HttpSession object allocates memory in the server and remembers
data across multiple requests during a session.
- can have both text java obj as values
- session creation and invalidation is our control.
- less network traffic
- allocate memory in server
- we must only work with Http Servlet comp
- cookies are deleted form browser then ST FAILS
Syntax :
HttpSession session = new HttpSession();
To create :
session.setAttribute(“name”, “ajay”);
to Remove :
session.removeAttribute(“name”):
to invalidate :
session.invalidate():
4. HttpSession with URL Rewriting
- appednign sessionId to the url that foes tot the browser.
JDBC
What is JDBC ?
- JDBC API will act as mediator between Java Program and Database
software
- JDBC API means classes and interfaces defined in two packages.
1. Java Sql
2. Javax.Sql (Extended)
DriverManager :
● It is a class present in java.sql pkg
● responsible for managing all db objs and to register and
unregister db drivers.
○ DiverManager.registerDriver(driver);
○ DiverManager.unregisterDriver(driver);
● also responsible to establish connection to the db with the help
of driver sw.
○ Connection con = DiverManager.getConnection(..);
Driver :
● without a driver sw we can’t touch db.
● It acts as a bridge bw java app and db.
● responsible for converting ajva calls into db specific calls and vice
versa.
What are types of JDBC Driver ?
1. Type 1 : JDBC ODBC Bridge Driver
2. Type 2 : Native Driver
3. Type 3 : Middleware / Network Protocol Driver
4. Type 4 : All Java Driver /thin driver
6. close connection
What is Statement ?
stmt obj only works for static query ps obj can work for both static and
dynamic queries.
Best choice when we want to work Best choice when we want to work
with multiple queries. with one querie but required to
execute multiple times.
Inserting data values & large object Inserting data values & large object
is difficult. is easy[ CLOB & BLOB ]
Explain ResultSet
- ResultSet will represent data given by our select query
- ResultSet will maintains cursor to point the rows
- Initially ResultSet cursor will be available before first record
- We need to move RS cursor to next position by calling next ( )
method
1) TYPE_FORWARD_ONLY ( by default )
2) TYPE_SCROLL_INSENSITIVE
3) TYPE_SCROLL_SENSITIVE
1) CONCUR_READ_ONLY
2) CONCUR_UPDATABLE
BatchProcessing:-
This concept is used to execute multiple SQLs at a time, instead of sending
them one by one. It saves only execution time by reducing network calls
between Java Application and Database
For this we use JDBC Batch
Example :
Statement stmt = con.createStatement();
con.close();
What are Transactions in JDBC
Single Unit amount of work is called as Transaction
=> In JDBC, transaction will be committed by default for every non select
query execution because by default Transaction Auto Commit is true.
con.setAutoCommit ( true ) ; // this is default behaviour of Connection
obj
con.setAutoCommit ( false ) ;
=> If we don't use Connection Pooling concept then our project will run into
Connections Exhausted Problem (No connections available to
communicate with db)
Features of JSP ?
● provides tag based programming and 9 implicit objs
● we can place jsp in public as well as private areas.
● custom tags
3. Taglib Directive :
a. to include jsp tag libraries.
b. <%@taglib %>
What is JSTL :
library that contains readily available jsp tags.