J2EE_Basics
Version Java EE 8 (J2EE 1.8) maintained under Oracle/Jakarta EE 8 (maintained by eclipse foundation)
(Tomcat 9 supports J2EE 1.8 version)
What is J2EE ?
Consists of specifications only (i.e interfaces and abstract classes).
Which specifications (i.e Rules or contracts)
Specifications of services required for any enterprise application.
Who uses this specifications?
Used by J2EE vendors to implement J2EE compliant web servers.
What is enterprise application ?
An enterprise application (EA) is a large software system platform designed to operate in a corporate environment .
It includes online shopping and payment processing, interactive product catalogs, computerized billing systems, security, content management, IT service management, business intelligence, human resource management, manufacturing, process automation, enterprise resource planning ....
What does this specifications include?
These specifications include
Servlet API
JSP(Java server page) API
Security
Connection pooling
EJB (Enterprise Java Bean) --> New Competitor Spring
JNDI(Naming service -- Java naming & directory i/f
JPA(java persistence API)
JMS(java messaging service)
Java Mail
Java Server Faces --> (Fading Technology)
Java Transaction API
Webservices support(SOAP/REST) etc...
Who is the Vendor of J2EE specs
Earlier it used to be under Sun microsystem, Oracle purchased Sun microsystem and now distributes these specification under Eclipse License as Oracle submitted Java EE to the Eclipse Foundation.
J2EE compliant web server
Apache Tomcat (web server : servlet/web container)
Difference between web server and Application server
A web server,
normally contains only one Web container[i.e (server side JVM)/(Web Engine)]
Can only manage dynamic web component for eg. Servlet, JSP, Servlet Filter.
Some of the services implemented by web server may include servlet API,JSP API,Security,Connection pooling,JNDI(naming service) does not implement all of them.
Whereas, an application server (here J2EE complaint application server) can be formulated as,
web container + EJB (enterprise java bean)container + ALL J2EE services implementation.
J2EE server Vendors & Products
Apache
tomcat(web server)/Tomee (app server)
Oracle/Sun
Glassfish
Red Hat
JBoss (wild fly)
Oracle
weblogic(by BEA Systems)
IBM
Websphere
WHY J2EE
Can support different types of clients
thin client(web client + Internet connection)
thick client --- application client(eg : TCP client/RESTful client)
smart clients -- mobile client
J2EE server independence
Create & deploy server side application i.e., any J2EE compliant server it is guaranteed to produce SAME results without making any changes or re-deploying on any other J2EE server[i.e by making WAR file(bundled output)]
Ready made implementation of primary services(eg., security, conn, pooling, email etc) which gives independence from the worry of managing the life cycle of the servlet etc, so that J2EE developer doesn't have to worry about primary services. Rather they can concentrate on actual business logic.
Web browser sends the request (URL) eg : http://www.abc.com:8080/day1.1 /day1.1 --> root/context path/web app name
Host -->Web server -->Web Container(server side JVM) -->Web application -->HTML/JSP/Servlet....
Note: One web container can deploy multiple web applications
What is dynamic web application server side application?
Its an program deployed(installed) on web-server/app-server, meant for servicing typically web clients(thin clients) using application layer protocol HTTP/HTTPS.
What is Web container?
Server side JVM residing within web server. It is a run-time environment for dynamic web components(such as Servlet & JSP,Filter).
Responsibilities of Web container
Creating Http Request & Http response objects
Controlling life-cycle of dynamic web components (i.e manages life cycle of servlet,JSP,Filters)
Giving ready-made support for services such as Naming,security,Connection pooling.
Handling concurrent request from multiple clients.
Managing session tracking.
What is web.xml
Deployment descriptor one per web application created by developer(in current case : by IDE)
This descriptor is read by Web Container, in the begginning of web application deployment.
What are the contents of web.xml?
Deployment instructions for welcome page, servlet deployment tags, session configuration, sec config......
Servlets
Why servlets?
To add dynamic nature to the web application
What is a servlet ?
Java class (with NO main method)
Represents dynamic web component whose life cycle will be managed by WC(web container : server side JVM)
life cycle methods such as init, service, destroy are called by WC using call back mechanism. These method implementation are provided by developer but when to invoke this method is decided by web container.
A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.
Life-cycle methods used by Web container to manage Servlet
Web container calls the init() method only once after servlet object has been created by the web container.
Similarly, the destroy() method is also called only once after the end of the servlet life cycle.
Similarly, web container calls the service() method once per request.
Responsibilities of Servlet
Request processing(involving conversion(string to Java data types)/validation).
Business Logic.
Dynamic response generation.
Managing Data Access Logic.
Page navigation.
Servlet API details
Servlet API Info
Deployment of the servlet
Via annotation (before J2EE 1.5 only xml based configuration was possible)
All annotations for Servlet is present in
javax.servlet.annotation
When
@WebServlet
annotation is used web container internally creates a hash map like structure with,
/validate
pages.LoginServlet
(url pattern)
Fully Qualified Servlet Class Name
Note: Does not need a constructor. Why?
A bit about init() and destroy()
Both methods are called only once by the webcontainer
init()/init(ServletConfig config)
Called only once on first request to the web server.
This behavior can be changed through load-on-startup tag in
web.xml
by default lazy initialization (loadOnStartup=-1)
destroy()
Called only once when
application is undeployed
server is reloaded(One case when this happens is when source code is changed. Not html changes but java code changes.)
server is shutdown
load on startup
With load-on-startup optional element we can change the default behaviour of sevlet instance initialization to eager(i.e a positive value, 0 is normally not used).
Higher the value, lower the priority of Servlet initialization.
Web Container(WC) loads servlet class at server side Web Container's Method area. Single instance of servlet class is created using default constructor and init() is invoked. Now servlet is completely initialized for servicing client requests.
Hmm, then what?
Now once servlet has been initialised by the WC, service() method of
HttpServlet
class is invoked by WC.service() method then checks for method in request header, based on which it decides which do**** method of the programmer provided servlet class needs to be called.
One the request has been served by the servlet, it sends the processed response to the Web Container, which in turn sends it to the Web server.
Web server basically wraps the response received from the WC in to a reponse packet with response status code, header and body. (Default response code is 200 (OK))
This response packet is send back to client. This reponse is rendered on to the client browser based on content type i.e html.
Legacy Approach: Using XML tags to configure a Web Servlet
Explanation remains same only how we confugure the servlet changes. This one set of xml tags will be repeated for as many servlet classes that needs to deployed. Leads to bloating.
Hence, Annotation rocks...!
Note: One servlet can be mapped to multiple url pattern. But multiple servlet cannot be mapped to same url pattern.
At the time of web app deployment, WC tries to populate map of url patterns , from XML tags (from web.xml). Later, it will check for @WebServlet annotation
How to read request parameters sent from the client, in a servlet?
Last updated