Ejava Unit 01
Ejava Unit 01
Java – Unit 01 1
1 Explain in brief various technologies provided by Java EE Platform
An enterprise application is a business application. Java EE is set of coordinated technologies and
practices that enable developing, deploying and managing multi tier server centric enterprise
applications.
It’s a collection of standardized components; containers and services for creating and deploying
distributed applications within well defined distributed computing architecture.
Following are the technologies provided by the Java EE platform:
1. Web application technologies:
• Java Servlet:
A servlet is a program that extends the functionality of a web server. servlet receives a
request from a client, dynamically generate the response and then the response containing
an HTML or XML document.
• JavaServer Pages:
JSP technology help us to generate dynamic content for a web client. A JSP page is text based
document that contains two types of text:
Static Data: can be expresses in any text based format such as HTML,WML and XML
JSP elements: informs how the page constructs dynamoc content
• JavaServer pages standard Tag Library[JSTL]:
It encapsulates core functionality of JSP applications. thie technology allows creating a
standard set of tags,instead of mixing tags from numerous vendors in a JSP application.
This standardization allows deploying application on any JSP server that supports JSTL.
• JavaServer Faces-JSF:
It’s a user interface framework for building web applications.
• Facelets:
Facelets refers to JavaServer Faces ViewDefinition framework, which is a page declaration
language developed for use with JavaServer Faces.it allows declaration of UI componenets in
different presentation technologies.
2. Enterprise Application Technologies:
• Enterprise Java Beans-EJB:
An Enterprise JavavBean[EJB] componenet is an enterprise bean having methods to
implement modules of business logic.
they are scalable, transactional, secure and are building blocks of the enterprise applications
running on JavaEE Server.
EJB can be accessed locally and remotely through RMI/HTTP for SOAP and RESTful web
services.
• Java Persistent API-JPA:
It’s a Java standards-based API for Object-Relational Mapping[ORM], a solution for
persistence.
Java persistence consists of:
ü The Java Persistence API
ü The query language
• Java Transaction API
It provides a standard interface for democrating transactions. It provides default auto
commit and rollback transactions.
• Java Message Services[JMS]:
This is an API for messaging standard that allow Java EE application components to
create,send,receive and read messages. It enables distributed coomunications that is
loosely coupled, reliable and asynchronous.
• Java Mail:
Java EE includes Java Mail with a service provider interface that allow application
components to send emails.
3. Web Services technologies:
• SOAP and RESTful:
JAX-WS is the Java API for SOAP web services. JAX-RS is the java API for RESTFUL web services.
• Java API for XML Registries (JAXR) :
JAXR allows accessing business and general-purpose registries over the web.
4. Security Services:
• JACC:
Java Authorization Service Provider Contract for containers (JACC) specification defines a
contract between a java EE application server and an authporization policy provider. JACC
enables services to authenticate and enforce access controls upon users. All java EE
containers support this contract.
• JASPIC:
The java Authentication Services Provider Interface for containers(JASPIC) specification
defines a service provider interface(SPI) by which authenctication providers that implement
message authentication mechanism may be integrated in client or server message processing
containers or runtimes.
2 What are different types of System Architecture?
1. Single Tier Architecture –
This is the type of architecture where the data and the application reside in the same machine. This
is done by organizations which are small and are not
geographically spread.
Advantages:
1) High security as the data and application exist in a
single system.
2) The simplest and least expensive architecture
3) Less equipment to purchase and maintain.
Disadvantage:
1) Communication with other systems is not present.
2) These types of implementation are lower security and the
lack of scalability.
3) Running all the site’s components on a single computer
also limits expansion and optimization possibilities
2. Two-tier Architecture:
In a modern two-tier architecture, the server holds both
the application and the data. The application resides on
the server rather than the client, probably because the
server will have more processing power and disk space
than the PC.
Advantages :
1) Communication is faster
2) Removes data redundancy and helps different department to communicate via data
Disadvantage
1) 2-Tier architecture application performance will be degraded upon increasing the users beyond
the 50-100 limit.
Enterprise Java – Unit 01 3
Advantage:
1) High degree of flexibility in deployment platform and configuration
2) Improved Security – Client is not direct access to database
3) Enhanced scalability – Each tier can scale horizontally
Containers are the interface between a component and the low-level platform-specific functionality that
supports the component. Before it can be executed, a web, enterprise bean, or application client
component must be assembled into a Java EE module and deployed into its container.
The assembly process involves specifying container settings for each component in the Java EE
application and for the Java EE application itself. Container settings customize the underlying support
provided by the Java EE server, including such services as security, transaction management, Java
Naming and Directory Interface (JNDI) API lookups, and remote connectivity. Here are some of the
highlights.
• The Java EE security model lets you configure a web component or enterprise bean so that
system resources are accessed only by authorized users.
• The Java EE transaction model lets you specify relationships among methods that make up a
single transaction so that all methods in one transaction are treated as a single unit.
• JNDI lookup services provide a unified interface to multiple naming and directory services in the
enterprise so that application components can access these services.
• The Java EE remote connectivity model manages low-level communications between clients and
enterprise beans. After an enterprise bean is created, a client invokes methods on it as if it were in
the same virtual machine.
• Because the Java EE architecture provides configurable services, application components within
the same Java EE application can behave differently based on where they are deployed. For
example, an enterprise bean can have security settings that allow it a certain level of access to
database data in one production environment and another level of database access in another
production environment.
• The container also manages nonconfigurable services, such as enterprise bean and servlet
lifecycles, database connection resource pooling, data persistence, and access to the Java EE
platform APIs
The deployment process installs Java EE application components in the Java EE containers as
illustrated above:
• Java EE server: The runtime portion of a Java EE product. A Java EE server provides EJB and
web containers.
• Enterprise JavaBeans (EJB) container: Manages the execution of enterprise beans for Java EE
applications. Enterprise beans and their container run on the Java EE server.
• Web container: Manages the execution of web pages, servlets, and some EJB components for
Java EE applications. Web components and their container run on the Java EE server.
• Application client container: Manages the execution of application client components.
Application clients and their container run on the client.
• Applet container: Manages the execution of applets. Consists of a web browser and Java Plug-
in running on the client together.
5 Write a note on Glass Fish Server.
• GlassFish is a Java application server project created by Sun Microsystems that allows many
developers to generate enterprise technologies that are convenient and scalable, as well as
additional services that can be installed based on preference. It is a free, dual-licensed software
under the GNU General Public License (GPL) and the Common Development and Distribution
License (CDDL). GlassFish was acquired by Oracle in 2010.
• GlassFish is the reference implementation of Java EE and as such supports Enterprise
JavaBeans, JPA, JavaServer Faces, JMS, RMI, JavaServer Pages, servlets, etc. This allows
developers to create enterprise applications that are portable and scalable, and that integrate with
legacy technologies. Optional components can also be installed for additional services.
6. What are advantages of servlet over CGI?
Advantages of Servlets over CGI:
• Platform independence:
Servlets run on top of Java Virtual Machine (JVM) so they are platform independent components.
On the other hand CGIs run on the operating system itself so they are platform dependent.
• Performance
Enterprise Java – Unit 01 7
CGIs run on separate processes, so it takes more time to start a new process for each and every
requests. Servlets are accessed through threads and those threads also being kept in a thread
pool. It increases the performance.
• Security
Servlets are running inside the sand box of JVM, so it is hard to damage the server side modules by
malfunctioning the Servlets. Since CGIs are native applications, using CGIs a hacker can damage
the server side components easily compared to Servlets.
• ErrorHandling
Servlets provides easiest error handling mechanism jointly with web container. CGIs do not have
such a powerful error handling support.
• Extensibility
Servlets are just Java classes so it is easy maintain and extend their functionality. All the object
oriented concepts can be directly applied to Servlets as well. CGIs are mostly written in scripting
languages like Perl so the extensibility is very poor in case of CGIs.
7 Differentiate CGI and Servlet
CGI Servlet
It is a process based. i.e., for every request, a It is thread based. i.e., for every request new
new process will be created and that process is thread will be created and that thread is
responsible to generate required response. responsible to generate required response.
Creation and destruction of new process for Creation and destruction of a new thread for
every request is costly, if the number of every request is not costly, hence if the number
requests increases then the performance of the of requests increases there is no change in
system goes down. Hence CGI technology fails response time. Due to this, servlet technology
to deliver scalable applications. succeeds to deliver scalable applications.
Two processes never share common address All the threads shares the same address space,
space. Hence there is no chance of occurring Hence concurrence problem is very common in
concurrence problems in CGI servlets.
We can write CGI program in variety of We can write servlets only in java.
languages, But most popular language is perl.
Most of the CGI languages are not object Java language itself is object oriented. Hence we
oriented. Hence we miss the benefits of oops. can get all the key benefits of oops.
Most of CGI languages are platform dependent. Java language is platform independent.
8 What is Servlet? Write Short note on Java Servlet Technology.
Servlet: It’s a module written using Java that runs in a server application to answer client requests.
¡ It’s most commonly used with HTTP and the word servlet is often used in the meaning of
HTTP servlet
¡ It’s supported by virtually all web servers and application servers
¡ Can easily share resources
9 What is Servlet? Write a short note on Java Servlet Technology. Describe Servlet Architecture.
Servlet:
¡ It’s a module written using Java that runs in a server application to answer client requests.
¡ It’s most commonly used with HTTP and the word servlet is often used in the meaning of
HTTP servlet
¡ It’s supported by virtually all web servers and application servers
¡ Can easily share resources
• Servlets read the explicit data sent by the clients (browsers). This includes an HTML form on a
Web page or it could also come from an applet or a custom HTTP client program.
• Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media
types and compression schemes the browser understands, and so forth.
• Process the data and generate the results. This process may require talking to a database,
executing an RMI or CORBA call, invoking a Web service, or computing the response directly.
• Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent
in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
• Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or
other clients what type of document is being returned (e.g., HTML), setting cookies and caching
parameters, and other such tasks.
Enterprise Java – Unit 01 9
Servlet Architecture:
The javax.servlet.Servlet package contains the generic interfaces and Classes that are
implemented and extended by all Servlets. The javax.servlet.http package contains the
Classes that are extended when creating HTTP-specific Servlets.
The main root of the Servlet Architecture is the javax.servlet.Servlet. it has following
interfaces:
• Servlet
• ServletRequest
• ServletResponse
• RequestDispatcher
• ServletConfig
• ServletContext
• SingleThreadModel
• Filter
• FilterConfig
10. Describe servlet life cycle.
A servlet life cycle can be defined as the entire process from its creation till the destruction. The lifecycle of a
servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a
servlet, the container performs the following steps:
• If an instance of the servlet does not exist, the web container
• Loads the servlet class.
• Creates an instance of the servlet class.
• Initializes the servlet instance by calling the init method.
• Invokes the service method, passing request and response objects. If it needs to remove the servlet,
the container finalizes the servlet by calling the servlet’s destroy method.
Enterprise Java – Unit 01 11
1. Loading a Servlet:
The first stage of the Servlet lifecycle involves loading and initializing the Servlet by the Servlet
container. The Web container or Servlet Container can load the Servlet at either of the following
two stages. The Servlet container performs two operations in this stage :
• Loading : Loads the Servlet class.
• Instantiation : Creates an instance of the Servlet. To create a new instance of the Servlet, the
container uses the no-argument constructor.
2. Initializing a Servlet:
• After the Servlet is instantiated successfully, the Servlet container initializes the instantiated
Servlet object. The container initializes the Servlet object by invoking
the Servlet.init(ServletConfig) method which accepts ServletConfig object reference as
parameter.
• The Servlet container invokes the Servlet.init(ServletConfig) method only once, immediately
after the Servlet.init(ServletConfig) object is instantiated successfully. This method is used to
initialize the resources, such as JDBC datasource.
• Now, if the Servlet fails to initialize, then it informs the Servlet container by throwing
the ServletException or UnavailableException.
•
3. Handling request:
• After initialization, the Servlet instance is ready to serve the client requests. The Servlet
container performs the following operations when the Servlet instance is located to service a
request :
• It creates the ServletRequest and ServletResponse objects. In this case, if this is a HTTP
request then the Web container creates HttpServletRequest and HttpServletResponse objects
which are subtypes of the ServletRequest and ServletResponse objects respectively.
• After creating the request and response objects it invoke the Servlet.service(ServletRequest,
ServletResponse) method by passing the request and response objects.
• The service() method while processing the request may throw
the ServletException or UnavailableException or IOException.
4. Destroying a Servlet:
• When a Servlet container decides to destroy the Servlet, it performs the following operations,
• It allows all the threads currently running in the service method of the Servlet instance to
complete their jobs and get released.
• After currently running threads have completed their jobs, the Servlet container calls
the destroy() method on the Servlet instance.
• After the destroy() method is executed, the Servlet conatiner releases all the references of
this Servlet instance so that it becomes eligible for garbage collection.
11 Write a note on RequestDispatcher
The RequestDispatcher interface provides the facility of dispatching the request to another resource it
may be html, servlet or jsp. This interface can also be used to include the content of another resource
also. It is one of the way of servlet collaboration.
To obtain the object of RequestDispatcher:-
The getRequestDispatcher() method of ServletRequest interface returns the object of
RequestDispatcher. Syntax:
• public RequestDispatcher getRequestDispatcher(String resource);
Example of using getRequestDispatcher method
• RequestDispatcher rd=request.getRequestDispatcher("servlet2");
• //servlet2 is the url-pattern of the second servlet
There are two methods defined in the RequestDispatcher interface.
• public void forward(ServletRequest request,ServletResponse response)throws
ServletException,java.io.IOException: Forwards a request from a servlet to another resource
(servlet, JSP file, or HTML file) on the server.
• public void include(ServletRequest request,ServletResponse response)throws
ServletException,java.io.IOException: Includes the content of a resource (servlet, JSP page,
or HTML file) in the response.
Enterprise Java – Unit 01 13
index.html
<form action="servlet1" method="post">
Name:<input type="text" name="userName"/><br/>
Password:<input type="password" name="userPass"/><br/>
<input type="submit" value="login"/>
</form>
Login.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Login extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExcep
tion, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
String p=request.getParameter("userPass");
if(p.equals("servlet"){
RequestDispatcher rd=request.getRequestDispatcher("servlet2");
rd.forward(request, response);
}
else{
out.print("Sorry UserName or Password Error!");
RequestDispatcher rd=request.getRequestDispatcher("/index.html");
rd.include(request, response);
}
}
}
WelcomeServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class WelcomeServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExc
eption, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
}
}
12 Differentiate ServletConfig and Servlet Context
ServletConfig ServletContext
• ServletConfig available in javax.servlet.*; • ServletContext available in javax.servlet.*;
package package
• ServletConfig object is one per servlet class • ServletContext object is global to entire web
• Object of ServletConfig will be created application
during initialization process of the servlet • Object of ServletContext will be created at the
• This Config object is public to a particular servlet time of web application deployment
only • Scope: As long as web application is executing,
• Scope: As long as a servlet is executing, ServletContext object will be available, and it
ServletConfig object will be available, it will be will be destroyed once the application is
destroyed once the servlet execution is removed from the server.
completed. • ServletContext object will be available even
• We should give request explicitly, in order to before giving the first request
create ServletConfig object for the first time • In web.xml – <context-param> tag will be appear
• In web.xml – <init-param> tag will be appear under <web-app> tag
under <servlet-class> tag
Enterprise Java – Unit 01 15