2. Java Enterprise Edition
The Java EE is complete platform for developing multi-tiered distributed applications. It consists of:
• The Java Virtual Machine (JVM).
• The Java SE APIs (SE APIs).
• The Java EE Application Server.
• The Java EE APIs (EE APIs).
Specifications of standard API
Defined by the Java Community Process (JCP)
Java Specification Requests (JSRs): proposed and final specifications
Different implementation of same API
Example: JPA is implemented by Eclipselink, OpenJPA, Hibernate
Certifications
Architecture pattern
Large scale
Multi-tiers (Presentation Logic, Business Logic, Persistence)
Scalable
Reliable
Secure
3. Java EE architecture
Client
Database
EJB Container
Web Container
Application
Client
Application
Client
Web Browser
Web Browser
Enterprise
Bean
Servlet JSP Page
Java EE Server
Enterprise
Bean
Enterprise
Bean
JSP Page
Servlet
4. EJB – Enterprise Java Beans
1. What is a bean?
• Beans are business logic components that implement a standard interface through which the bean is hooked into
the bean container (= runtime object for bean).
• A Java class implementing one of the standard bean interfaces is a bean.
• Beans can be accessed remotely, usually from a client tier.
Bean
Bean
container
5. EJB
Why EJB?
Common concerns in different applications lead to re-implementing the same functionality for business logic components.
Examples of common functionality:
- Persistence
- Transactions
- Security
- Runtime and lifecycle management (create, start, stop and delete component)
EJB is a framework that provides the following services to applications:
- Persistence
- Transaction processing
- Concurrency control (each client accesses its own bean instance)
- Events using JMS (Java Messaging Service)
- Naming and directory services via JNDI (Java Naming and Directory Interface)
- Security using JAAS (Java Authentication and Authorization Service)
- Deployment of software components to a server host
- Remote procedure calls via RMI (RMI over IIOP)
- Exposing business functionality via web services
Business
logic
Front end
(protocol, GUI)
Backend
(DB)
6. Servlet
Java Servlet is a java object file developed as a component and
runs on the server.
Servlets are programs that run on a Web or application server
and act as a middle layer between a request coming from a Web
browser or other HTTP client and databases or applications on
the HTTP server
Servlets is a component can be invoked from HTML.
Note:
Servlets cannot run independently as a main application
like the java applet, the servlet does not have main method.
7. Servlet do not display a graphical interface to the user.
A servlet’s work is done at the server and only the results
of the servlet’s processing are returned to the client in the
form of HTML.
A Servlet is a Java technology-based Web component,
managed by a container that generates dynamic content.
Like other Java technology-based components, Servlets
are platform-independent Java classes that are compiled
to platform-neutral byte code that can be loaded
dynamically into and run by a Java technology-enabled
Web server.
Servlet
8. Advantages of Servlet
Platform Independence:
Servlets are written entirely in java so these are platform
independent.
Servlets can run on any Servlet enabled web server.
Performance
Due to interpreted nature of java, programs written in java are slow.
But the java Servlets runs very fast. These are due to the way Servlets run on web
server.
For any program initialization takes significant amount of time. But in case of
Servlets initialization takes place first time it receives a request and remains in
memory till times out or server shut downs.
9. Extensibility
Java Servlets are developed in java which is robust, well- designed and
object oriented language which can be extended or polymorphed into new
objects.
So the java Servlets take all these advantages and can be extended from
existing class to provide the ideal solutions.
4. Safety Java provides very good safety features like memory
management, exception handling etc. Servlets inherits all these
features and emerged as a very powerful web server extension.
5. Secure Servlets are server side components, so it inherits the
security provided by the web server. Servlets are also benefited with
Java Security Manager.
10. JSP
JSP is one of the most powerful, easy-to-use, and
fundamental tools in a Web-site developer's toolbox.
JSP combines HTML and XML with Java servlet (server
application extension) and JavaBeans technologies to create
a highly productive environment for developing and deploying
reliable, interactive, high-performance platform- independent
Web sites.
JSP facilitates the creation of dynamic content on the server.
11. It is part of the Java platform's integrated
solution for server-side programming, which
provides a portable alternative to other server-
side technologies, such as CGI.
JSP integrates numerous Java application
technologies, such as Java servlet, JavaBeans,
JDBC, and Enterprise JavaBeans.
It also separates information presentation from
application logic and fosters a reusable
component model of programming.
12. Advantages of JSP
Servlet use println statements for printing an HTML
document which is usually very difficult to use. JSP has no
such tedious task to maintain.
JSP needs no compilation, CLASSPATH setting and
packaging.
In a JSP page visual content and logic are seperated,
which is not possible in a servlet.
There is automatic deployment of a JSP; recompilation is
done automatically when changes are made to JSP pages.
Usually with JSP, Java Beans and custom tags web
application is simplified.
13. Fundamental JSP Tags
JSP tags are an important syntax element of Java Server
Pages which start with "<%" and end with "%>" just like
HTML tags. JSP tags normally have a "start tag", a "tag
body" and an "end tag". JSP tags can either be predefined
tags or loaded from an external tag library.
Fundamental tags used in Java Server Pages are classified
into the following categories:-
Declaration tag
Expression tag
Directive tag
Scriptlet tag
Comments
14. HTTP- Hyper Text Transfer Protocol
• Protocol: A Protocol is a standard procedure for defining and
regulating communication. For example TCP,UDP, HTTP etc.
• HTTP is the foundation of data communication for the World Wide
Web.
• The HTTP is the Web’s application-layer protocol for transferring
various forms of data between server and client like plaintext,
hypertext, image, videos and sounds .
15. How HTTP Works?
• The Hypertext Transfer Protocol (HTTP) is designed to enable
communications between clients and servers.
• HTTP works as a request-response protocol between a client and
server.
• Example: A client (browser) sends an HTTP request to the server; then
the server returns a response to the client. The response contains
status information about the request and may also contain the
requested content.
HTTP Methods
•GET
•POST
•PUT
•HEAD
•DELETE
•PATCH
•OPTIONS
The two most common HTTP methods are: GET
and POST.
21. Http Get Request
GET is used to request data from a specified resource.
GET is one of the most common HTTP methods.
Note that the query string (name/value pairs) is sent in the URL of a GET request:
/test/demo_form.php?name1=value1&name2=value2
Some other notes on GET requests:
•GET requests can be cached
•GET requests remain in the browser history
•GET requests can be bookmarked
•GET requests should never be used when dealing with sensitive data
•GET requests have length restrictions
•GET requests are only used to request data (not modify)
22. Http Post Request
The POST Method
POST is used to send data to a server to create/update a resource.
The data sent to the server with POST is stored in the request body of the HTTP request:
POST /test/demo_form.php HTTP/1.1
Host: w3schools.com
name1=value1&name2=value2
POST is one of the most common HTTP methods.
Some other notes on POST requests:
•POST requests are never cached
•POST requests do not remain in the browser history
•POST requests cannot be bookmarked
•POST requests have no restrictions on data length
23. HTML- Hyper Text Markup Language
What is HTML?
• HTML is a language for describing web pages.
• HTML is not a programming language, it is a markup language
• A markup language is a set of markup tags
• HTML uses markup tags to describe web pages
HTML Tags
• HTML markup tags are usually called HTML tags
• HTML tags are keywords surrounded by angle brackets like
<html>
• HTML tags normally come in pairs like <b> and </b>
• The first tag in a pair is the start tag, the second tag is the end tag
• Start and end tags are also called opening tags and closing tags .
24. URL
URL stands for Uniform Resource Locator is the global address of documents and other resources on the World
Wide Web. Its main purpose is to identify the location of a document and other resources available on the
internet, and specify the mechanism for accessing it through a web browser.
A URL (Uniform Resource Locator) is a unique identifier used to locate a resource on the internet. It is also
referred to as a web address.
URLs consist of multiple parts including a protocol and domain name that tell a web browser how and where
to retrieve a resource
The URL contains the name of the protocol needed to access a resource, as well as a resource name
25. Containers
The application server provides the runtime platform in the form of containers. A container is a component of the
application server that manages a specific type of components and provide them with all the needed services and
Java EE API implementation. The two types of containers are:
-Business Container
The business container is responsible for managing business components and providing them with runtime
services like: RMI communication, database manipulation, transaction services, etc.
-Web Container
The web container is responsible for managing web components and providing them with runtime services
like: HTTP communication, database manipulation, etc.
26. Servlet Life Cycle & Life Cycles Methods
• Init () Method
• Service () Method
• Destroy() method
Init () Method:-
During initialization stage of the Servlet life cycle, the web container initializes the servlet
instance by calling the init() method.
The container passes an object implementing the ServletConfig interface via the init()
method.
This configuration object allows the servlet to access name- value initialization parameters
from the web application.
27. • Service () Method:-
◦ After initialization, the servlet can service client requests. each request is serviced in
its own separate thread.
◦ The Web container calls the service() method of the servlet for every
request.
◦ The service() method determines the kind of request being made and dispatches it to
an appropriate method to handle the request.
◦ The developer of the servlet must provide an implementation for these methods. If a
request for a method that is not implemented by the servlet is made, the method of the
parent class is called, typically resulting in an error being returned to the requester.
• Destroy() method:-
◦ Finally, the Web container calls the destroy() method that takes the servlet out of
service. The destroy() method, like init(), is called only once in the lifecycle of a servlet.
31. Difference Between Get And Post
GET POST
BACK button/Reload Harmless Data will be re-submitted (the browser should alert the
user that the data are about to be re-submitted)
Bookmarked Can be bookmarked Cannot be bookmarked
Cached Can be cached Not cached
Encoding type application/x-www-form-urlencoded application/x-www-form-urlencoded or
multipart/form-data. Use multipart encoding for binary
data
History Parameters remain in browser history Parameters are not saved in browser history
Restrictions on data length Yes, when sending data, the GET method adds the data
to the URL; and the length of a URL is limited
(maximum URL length is 2048 characters)
No restrictions
Restrictions on data type Only ASCII characters allowed No restrictions. Binary data is also allowed
Security GET is less secure compared to POST because data sent
is part of the URL
Never use GET when sending passwords or other
sensitive information!
POST is a little safer than GET because the parameters
are not stored in browser history or in web server logs
Visibility Data is visible to everyone in the URL Data is not displayed in the URL
32. Servlet Code To Download the JAR
package com.download.jar;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DownloadJarServlet")
public class DownloadJar extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// 1. Helping the browser to understand that it is
// a jar file not an html content.
// Inorder to pass this information in response
// we set the content type as "application/jar"
// by calling setContentType() method in response object
response.setContentType("application/jar");
// 2. Getting the ServletContext object by calling
// getServletContext() method from the current servlet object
ServletContext ctx = getServletContext();
// 3. From ServletContext object we are calling getResourceAsStream()
// which returns the resource located at the named path as an InputStream object.
// The data in the InputStream can be of any type or length.
// This method returns null if no resource exists at the specified path.
// here we are passing the jar name present in the server at the relative path
InputStream is = ctx.getResourceAsStream("/mail.jar");
int read = 0;
byte[] noOfBytes = new byte[1024];
// 4. Getting the outputstream from the response object to write
// contents of jar to browser
OutputStream os = response.getOutputStream();
// 5. Reading the contents of jar in bytes using the inputstream created above
// and writing it to the browser through outputstream created above.
while((read = is.read(noOfBytes)) != -1 ){
os.write(noOfBytes, 0 , read);
}
os.flush();
os.close();
}
}
33. Dispatching Vs. Redirecting
When a servlet does a redirect, it’s like asking the client to call someone else instead. In this case, the client is
the browser, not the user. The browser makes the new call on the user’s behalf, after the originally-requested
servlet says, “Sorry, call this guy instead...”
The user sees the new URL in the browser.
When a servlet does a request dispatch, it’s like asking a co-worker to take over working with a client.
The co-worker ends up responding to the client, but the client doesn’t care as long as someone responds.
The user never knows someone else took over, because the URL in the browser bar doesn’t change.
34. THANKS!
Dr Pankaj Gupta
Head – ACCESS Health Digital
digital.health@accessh.org
Twitter: @pankajguptadr, @accesshdigital
LinkedIn: drpankajgupta, accesshdigital