0% found this document useful (0 votes)
53 views

What Are Java EE Containers

Java EE containers are runtime environments that host and manage Java applications. The main types are EJB containers for enterprise JavaBeans and web containers for servlets and JSPs. Containers provide services like lifecycle management, security, and persistence to support scalable enterprise applications.

Uploaded by

Pawan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

What Are Java EE Containers

Java EE containers are runtime environments that host and manage Java applications. The main types are EJB containers for enterprise JavaBeans and web containers for servlets and JSPs. Containers provide services like lifecycle management, security, and persistence to support scalable enterprise applications.

Uploaded by

Pawan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

What are Java EE containers?

Ans
Definition (1 mark): Java EE containers are runtime environments within the Java EE
architecture that host and manage the execution of enterprise-level Java applications. There
are two main types of containers: the EJB container for Enterprise JavaBeans components
and the web container for servlets and JSP (JavaServer Pages).
EJB Container (Enterprise JavaBeans Container):
Manages Enterprise JavaBeans components, providing services like lifecycle
management, transaction management, security, and persistence.
Essential for building scalable and distributed enterprise applications.
Web Container (Servlet Container):
Manages the execution of web-based components such as servlets and
JavaServer Pages (JSP).
Offers services like lifecycle management, multithreading, and session
management for web components.
Servlets handle HTTP requests and responses, while JSP allows the embedding
of Java code within HTML pages.
Application Client Container:
Manages the execution of Java EE application client components.
Provides services for application clients to interact with enterprise resources.
Enables the development of Java EE applications with client-side components
that run on the client machine.
Applet Container:
Handles the execution of Java applets, which are small, client-side Java
programs embedded in web pages.
Provides a secure and controlled environment for running Java applets within
a web browser.
Applets were popular in the early days of web development for creating
interactive content.

These four containers play distinct roles within the Java EE architecture, catering to
different types of components and facilitating the development of diverse enterprise
applications. If you have any more questions or if there's anything specific you'd like
to explore further, feel free to let me know!

Write a short note on Servlet interface and explain its methods.

Ans Servlet interface provides commonbehaviorto all the servlets.Servlet interface


defines methods that all servlets must implement.
Servlet interface needs to be implemented for creating any servlet (either directly or
indirectly). It provides 3 life cycle methods that are used to initialize the servlet, to
service the requests, and to destroy the servlet and 2 non-life cycle methods.

Methods of Servlet interface


There are 5 methods in Servlet interface. The init, service and destroy are the life
cycle methods of servlet. These are invoked by the web container.

Method Description

public void init(ServletConfig config) initializes the servlet. It is the life cycle
method of servlet and invoked by the
web container only once.

public void service(ServletRequest provides response for the incoming


request,ServletResponse response) request. It is invoked at each request
by the web container.

public void destroy() is invoked only once and indicates that


servlet is being destroyed.

public ServletConfig getServletConfig() returns the object of ServletConfig.

public String getServletInfo() returns information about servlet such


as writer, copyright, version etc.

Explain the life cycle of a servlet.

Ans1) Servlet class is loaded

The classloader is responsible to load the servlet class. The servlet class is loaded
when the first request for the servlet is received by the web container.

2) Servlet instance is created


The web container creates the instance of a servlet after loading the servlet class. The
servlet instance is created only once in the servlet life cycle.
3) init method is invoked
The web container calls the init method only once after creating the servlet instance. The init
method is used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet
interface. Syntax of the init method is given below:
public void init(ServletConfig config) throws ServletException

4) service method is invoked


The web container calls the service method each time when request for the servlet is
received. If servlet is not initialized, it follows the first three steps as described above
then calls the service method. If servlet is initialized, it calls the service method.
Notice that servlet is initialized only once. The syntax of the service method of the
Servlet interface is given below:
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException

5) destroy method is invoked


The web container calls the destroy method before removing the servlet instance
from the service. It gives the servlet an opportunity to clean up any resource for
example memory, thread etc. The syntax of the destroy method of the Servlet
interface is given below:
public void destroy()
Explain JDBC architecture in detail
Ans
JDBC Architecture :
The JDBC API supports both two-tier and three-tier processing models for database access but in
general, JDBC Architecture consists of two layers
JDBC API: This provides the application-to-JDBC Manager connection.
JDBC Driver API: This supports the JDBC Manager-to-Driver Connection.
The JDBC API uses a driver manager and database-specific drivers to provide transparent
connectivity to heterogeneous databases. The JDBC driver manager ensures that the correct driver
is used to access each data source. The driver manager is capable of supporting multiple concurrent
drivers connected to multiple heterogeneous databases. Following is the architectural diagram,
which shows the location of the driver manager with respect to the JDBC drivers and the Java
application

Explain different types of drivers


Ans
JDBC Driver is a software component that enables java application to interact with the
database. There are 4 types of JDBC drivers:

JDBC-ODBC bridge driver


Native-API driver (partially java driver)
Network Protocol driver (fully java driver)
Thin driver (fully java driver)

JDBC-ODBC bridge driver


The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC
bridge driver converts JDBC method calls into the ODBC function calls. This is now
discouraged because of thin driver.
In Java 8, the JDBC-ODBC Bridge has been removed.

Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle recommends
that you use JDBC drivers provided by the vendor of your database instead of the
JDBC-ODBC Bridge.

Advantages:
easy to use.
can be easily connected to any database.

Disadvantages:
Performance degraded because JDBC method call is converted into the ODBC function calls.
The ODBC driver needs to be installed on the client machine.

2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts JDBC
method calls into native calls of the database API. It is not written entirely in java.
Advantage:
performance upgraded than JDBC-ODBC bridge driver.

Disadvantage:
The Native driver needs to be installed on the each client machine.
The Vendor client library needs to be installed on client machine.

3) Network Protocol driver


The Network Protocol driver uses middleware (application server) that converts JDBC
calls directly or indirectly into the vendor-specific database protocol. It is fully written
in java.
Advantage:
No client side library is required because of application server that can perform many tasks
like auditing, load balancing, logging etc.

Disadvantages:
Network support is required on client machine.
Requires database-specific coding to be done in the middle tier.
Maintenance of Network Protocol driver becomes costly because it requires database-
specific coding to be done in the middle tier.

4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is why it
is known as thin driver. It is fully written in Java language.
Advantage:
Better performance than all other drivers.
No software is required at client side or server side.

Disadvantage:
Drivers depend on the Database.

6)Explain two tier architecture with its advantages and disadvantages.


Ans 2 Tier Architecture is like a two-story building in computer
systems. The first floor (client) asks for information, and the second
floor (server) provides it. They talk directly, without any middle floor.
It’s simple but can get messy when many people (users) are involved.
What are the advantages and
disadvantages of 2 Tier Architecture
The followings are the advantages and disadvantages of 2 Tier
Architecture:

Advantages Disadvantages

Simpler to manage and control Less scalable

Faster communication process Harder to maintain

Reduced system complexity Limited user interface

Cost-effective development and


Not suitable for complex systems
maintenance

Improved system performance Lacks robust security features

Advantages of 2 Tier Architecture


1. Simpler to manage and control – Two-tier architecture is easier to
handle and oversee because it has fewer layers, making it less
complicated and more straightforward.
2. Faster communication process – The communication process is
quicker due to the direct link between the client and server, leading
to faster data exchange.
3. Reduced system complexity – With only client and server layers,
the system becomes less complex, reducing the potential for errors
and making it easier to understand.
4. Cost-effective development and maintenance – It’s more cost-
effective in terms of development and maintenance because fewer
layers mean fewer resources and time are needed for updates or
fixes.
5. Improved system performance – The system performance is
enhanced because the direct client-server interaction reduces the
time taken for data processing and retrieval.

Disadvantages of 2 Tier Architecture


1. Less scalable – In a 2 Tier architecture, scaling up to meet
increasing demand is often a challenge. This means it can struggle
to handle more users or data over time.
2. Harder to maintain – Maintaining this architecture can be tough
as changes need to be made in both the client and server tiers,
making it time-consuming.
3. Limited user interface – The user interface is limited in this setup,
which can restrict the user experience and prevent the system from
offering a wide range of features.
4. Not suitable for complex systems – It’s not ideal for complex
systems. As the business grows and system complexity increases, a
2 Tier architecture might not be able to cope.
5. Lacks robust security features – The architecture lacks robust
security features. As a result, it may be more vulnerable to threats
and attacks, which can compromise data and system integrity.
7 Compare class and interface and explain the term API
Ans
Class Interface

The keyword used to create a class is The keyword used to create an interface is
“class” “interface”

A class can be instantiated i.e., objects An Interface cannot be instantiated i.e. objects
of a class can be created. cannot be created.

Classes do not support multiple


The interface supports multiple inheritance.
inheritance.

It can be inherited from another class. It cannot inherit a class.

It can be inherited by a class by using the


It can be inherited by another class
keyword ‘implements’ and it can be inherited
using the keyword ‘extends’.
by an interface using the keyword ‘extends’.

It can contain constructors. It cannot contain constructors.

It cannot contain abstract methods. It contains abstract methods only.

Variables and methods in a class can


be declared using any access All variables and methods in an interface are
specifier(public, private, default, declared as public.
protected).

Variables in a class can be static,


All variables are static and final.
final, or neither.

1)Write a short note on RequestDispatcher Interface.


Ans
 The Request Dispatcher interface dispatching the request to another resource
it may be html, servlet .
 This interface also be used to include the content of another resource.
 It is one of the way of servlet collaboration.
 There are two methods defined in the RequestDispatcher interface.
1. forward(ServletRequest request, ServletResponse response):
This method forwards the request and response objects to another resource for
processing. The resource can be a servlet, JSP page, or any other resource that can
process the request and generate a response.
The forward() method is typically used when the requested resource cannot
handle the request and needs to be forwarded to another resource for processing.
2. include(ServletRequest request, ServletResponse response):
This method includes the response generated by another resource in the response
sent to the client.
The resource can be a servlet, JSP page, or any other resource that can generate a
response. The include() method is typically used when the requested resource
needs to include the output generated by another resource in its own response.
2)List and explain methods of cookies class and explain use of cookies?
Ans Cookies in Servlet
A cookie is a small piece of information that is persisted between the multiple
client 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 version number.
Useful Methods of Cookie class
There are given some commonly used methods of the Cookie class.
Other methods required for using Cookies

For adding cookie or getting the value from the cookie, we need some methods
provided by other interfaces. They are:
1. public void addCookie(Cookie ck):method of HttpServletResponse interface is
used to add cookie in response object.
2. public Cookie[] getCookies():method of HttpServletRequest interface is used
to return all the cookies from the browser.

3Write a short note on HttpSession.


Ans
4 What is Glassfish Server? Explain its Features

Difference between URL and Uri

URL URI

URI provides a technique


URL is used to describe the identity of an item. for defining the identity of
an item.
URL URI

URI is used to distinguish


URL links a web page, a component of a web page or a
one resource from other
program on a web page with the help of accessing
regardless of the method
methods like protocols.
used.

URL provides the details about what type of protocol is URI doesn’t contains the
to be used. protocol specification.

URL is a type of URI. URI is the superset of URL.

It comprises of scheme,
It comprises of protocol, domain, path, hash, and so on. authority, path, query and
many more.

Ex-https://www.geeksforgeeks.org/ Ex- urn:isbn:0-294-56559-3

7)Explain about the file uploading feature of a servlet.


Ans
A Servlet can be used with an HTML form tag to allow users to upload files to
the server. An uploaded file could be a text file or image file or any document.
Creating a File Upload Form
The following HTM code below creates an uploader form. Following are the
important points to be noted down −
• The form method attribute should be set to POSTmethod and GET
method can not be used
• The form enctype attribute should be set to multipart/form-data.
• The form action attribute should be set to a servlet file which would
handle file uploading at backend server. Following example is
using UploadServlet servlet to upload file.
• To upload a single file you should use a single <input .../> tag with attribute
type="file". To allow multiple files uploading, include more than one
input tags with different values for the name attribute. The browser
associates a Browse button with each of them.
Here, we will learn how to upload file to the server. For uploading a file to the server,
method must be post and enctype must be multipart/form-data in html file. For
Example:

index.html
1. <html>
2. <body>
3. <form action="go" method="post" enctype="multipart/form-data">
4. Select File:<input type="file" name="fname"/><br/>
5. <input type="submit" value="upload"/>
6. </form>
7. </body>
8. </html>

Example of uploading file to the server in servlet


Now, for uploading a file to the server, there can be various ways. But, I am going to use
MultipartRequest class provided by oreilly. For using this class you must have cos.jar file.
If you will download this example, we will the cos.jar file alongwith code.

UploadServlet.java
1. import java.io.*;
2. import javax.servlet.ServletException;
3. import javax.servlet.http.*;
4. import com.oreilly.servlet.MultipartRequest;
5.
6. public class UploadServlet extends HttpServlet {
7.
8. public void doPost(HttpServletRequest request, HttpServletResponse response)
9. throws ServletException, IOException {
10.
11. response.setContentType("text/html");
12. PrintWriter out = response.getWriter();
13.
14. MultipartRequest m=new MultipartRequest(request,"d:/new");
15. out.print("successfully uploaded");
16. }
17. }

There are two arguments passed in MultipartRequest class constructor, first one is
HttpServletRequest object and second one is String object (for location). Here I am
supposing that you have new folder in D driver.

web.xml file

This configuration file provides information about the servlet.

1. <web-app>
2.
3. <servlet>
4. <servlet-name>UploadServlet</servlet-name>
5. <servlet-class>UploadServlet</servlet-class>
6. </servlet>
7.
8. <servlet-mapping>
9. <servlet-name>UploadServlet</servlet-name>
10. <url-pattern>/go</url-pattern>
11. </servlet-mapping>
12.
13. </web-app>

You might also like