0% found this document useful (0 votes)
37 views57 pages

J2ee Odp

This document provides an overview of key concepts related to web applications and Java servlets. It discusses client-server architecture, web terminology like servlets and JSPs, the servlet lifecycle, and how servlets handle HTTP requests and responses. It also covers topics like session management, request parameters, and the differences between GET and POST requests.

Uploaded by

HEMANT TIWARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views57 pages

J2ee Odp

This document provides an overview of key concepts related to web applications and Java servlets. It discusses client-server architecture, web terminology like servlets and JSPs, the servlet lifecycle, and how servlets handle HTTP requests and responses. It also covers topics like session management, request parameters, and the differences between GET and POST requests.

Uploaded by

HEMANT TIWARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 57

Advance Java

Content

● Client Server Architecture


● Web Terminologies
● Servlet
● Servlet Hierarchy
● Servlet Life Cycle
● Servlet context and config
● Redirecting
● Session Management
● Jsp
What is Web?

Web consists of billions of clients and server


connected through wires and wireless networks
What is Web Application?

A Web Application is a collection of static


files(webpages) such as HTML pages, images,
graphics etc. A Web application is a web site
with dynamic functionality on the server. Google,
Facebook, Twitter are examples of web
applications.
Webpage & Website

Webpage is a collection of html,css &


javascript files.
Website is the collection of Webpages.
Types of Website

Static Website Dynamic Website


1) Static web pages will remain 1) Dynamic websites are those
same for the time until and websites where data
unless someone changes it generates dynamically.
manually.
2) Does use Databases
2) Doesn't use databases.
3) Languages used: front-end +
3) Languages used : HTML, php or python or asp.net or
CSS, JavaScript. j2ee.
Two Phases of Website

Front – End Back – End


1)Front – end is the 1)Back – End is the
what users see in phase which provides
website. data to front – end.
2)HTML, CSS & J.S 2)It contains databases
and as well
languages which runs
on server example :
php, j2ee etc.
Protocol

Protocol is the set of guidelines and rules by


which one computer can communicate with
another computer over internet.
Examples:
1)Http
2)Ftp
3)Smtp
4)Https
HTTP (Hypertext Transfer Protocol)

HTTP is a protocol that clients and servers use on the web


to communicate.
● It is similar to other internet protocols such as
SMTP(Simple Mail Transfer Protocol) and FTP(File
Transfer Protocol) but there is one fundamental difference.
● HTTP is a stateless protocol i.e HTTP supports only one
request per connection. This means that with HTTP the
clients connect to the server to send one request and then
disconnects. This mechanism allows more users to
connect to a given server over a period of time.
● The client sends an HTTP request and the server answers
with an HTML page to the client, using HTTP.
HTTP Methods

HTTP request can be Methods


made using a variety of
methods, but the ones
1)GET
you will use most often 2)POST
are Get and Post. The
3)PUT
method name tells the
server the kind of 4)…..
request that is being
made, and how the rest
of the message will be
formatted.
Servlet

Servlet Technology is used to create web


applications. Servlet technology uses Java
language to create web applications.

Web applications are applications that resides


at web server and build dynamic web pages. A
dynamic page could be anything like a page
that randomly chooses picture to display or
even a page that displays the current time.
CGI (Common Gateway Interface)

Before Servlets, CGI(Common Gateway Interface)


programming was used to create web applications. Here's
how a CGI program works :
1)User clicks a link that has URL to a dynamic page instead
of a static page.
2)The URL decides which CGI program to execute.
3)Web Servers run the CGI program in seperate OS shell.
The shell includes OS enviroment and the process to
execute code of the CGI program.
4)The CGI response is sent back to the Web Server, which
wraps the response in an HTTP response and send it back
to the web browser.
Drawbacks of CGI programs

● High resposne time because its create processes to


handle request.
● CGI is not scalable.
● CGI programs are not always secure or object-oriented.
● It is Platform dependent.
Because of these disadvantages, developers started
looking for better CGI solutions. And then Sun
Microsystems developed Servlet as a solution over
traditional CGI technology.
Advantges of Servlet

1)Better performance:
Because it creates a thread for each request
not process (like CGI).
2)Platform independent:
Because it uses java language and java is
robust language.
3)Trusted
4)Library:
Full functionality of java library is available.
Servlet Api

Servlet technology is an Api and the latest version is


4.0.
Servlet API provides Classes and Interface to develop
web based applications.

Servlet API contains two java packages are used to


developed the servlet programs, they are:

1)javax.servlet
2)javax.servlet.http
javax.servlet

javax.servlet package contains list of interfaces


and classes that are used by the servlet or web
container. These classes and interface are not
specific to any protocol.
Interfaces Classes
1)Servlet 1)GenericServlet
2)ServletRequest 2)ServletInputStream
3)ServletResponse 3)ServletOutputStream
4)RequestDispatcher 4)ServletRequestWrapper
5)ServletConfig 5)ServletResponseWrapper
6)ServletContext 6)ServletException
javax.servlet.http

javax.servlet.http package contains list of


classes and interfaces to define http servlet
programs. This package are used to interact
with browser using http protocol. It is only
responsible for http requests.

Interfaces Classes
1)HttpServlet
1)HttpServletRequest
2)Cookie
2)HttpServletResponse 3)HttpServletRequestWrapper
3)HttpSession 4)HttpServletResponseWrapp
er
4)HttpSessionListener
Servlet Hierarchy
Environment Setup

To work with j2ee, we need to have :


1)Java server
like :
1. tomcat
2. getty
3. glassfish
2)Servlet-api.jar
Java Server

Java Server is the Software where we can


serve/deploy java web applications.
Tomcat installation
http://bit.do/jTomcat
Reasons to use tomcat:
1)It’s Incredibly Lightweight
2)It’s Open-Source
3)It’s Highly Flexible
Servlet Api Setup

To work with java web applications, we need to


setup servlet-api.jar to make servlets work.
We cannot run servlet applications if you didn’t
set servlet-api.jar.
Set servlet-api.jar in classpath
http://bit.do/jServlet
Hello World Example

Now we are going to create a servlet which can


handle http request/response from client & in
our case it will generate hello world
Question

As we know Servlet does not have any main().

● But someone has to instantiate the servlet.


● But someone has to get the request and
response.
● But someone has to Manage.
1) Life
2)Death
3)Resources
Web Container

Web container is the application present in


server which is responsible for managing
servlet execution in java web application.
It handles :
1)Servlet life cycle
2)Security
3)Multi threading support
Working of Servlet

Lets understand the working of servlets in


detail.
Servlet Life Cycle

It can be defined as the entire process of servlet from


creation to destroy.
The web container maintains the life cycle of a Servlet
instance or object.
Cycle
1)Loading (Servlet class is loaded)
2)Installation (Servlet instance is created)
3)Initialization (init method is invoked)
4)Service providing (service method is invoked)
5)Destroying (destroy method is invoked)
1. 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.
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.
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.
Difference Btw Http and Generic Servlet

Generic HttpServlet
1)Generic Servlet is 1)Specially designed
protocol Independent. for Http protocol.
(http,smtp,ftp)
2)Supports all http
2)Service method methods.
handles all request
3)Can handle session
3)Can handle only & cookie data
simple requests
4)can’t handle session
data
HttpServletRequest Methods

getParameter(String name) – String

Returns the value of the request parameter.


(if no parameter found then return null)

GetPrameterValues( ) - String[ ]

Returns all request prameter values in an


array.
(returns null if no parameter found)
GetParameterMap( ) - Returns java.util.Map
(returns null if not any parameter found)

getParametetNames( ) : Returns an
Enumeration of String.
(returns null if not any parameter found)
Example to handle Query Data

Lets create one form and send data to servlet


from client using get and post.
Difference Between Get & Post

Get Post
1) Data is sent in Url. 1) Data is sent in request body.
2) Get request is not secured 2) Post request is secured
because data is exposed in because data is not exposed
URL. in URL.
3) Get request can be 3) Post request cannot be
bookmarked and is more bookmarked.
efficient.
4) Get request can send only 4) Large amount of data can be
limited amount of data sent.
around 2048 characters
Response Type

Content Type is also known as MIME Type.


MIME stand for Multipurpose internet Mail
Extension. It is a HTTP header that provides
the description about what are you sending to
the browser (like send image, text, video etc.).
Syntax :
response.setcontentType("text/html");

Read more : http://bit.do/jMIME


Annotation vs Deployment Descriptor

Web.xml file is known as Deployment


Descriptor which is used to configure servlets
and other Java web application resources.
before annotations (introduced in 3.0 servlet-
api) deployment descriptor file is used.
Web Container looks into Deployment
Descriptor file or WebServlet annotation which
is used to set the configuration of Servlets and
other resources like url-pattern,name etc.
Syntax

<web-app>
<servlet>
<servlet-name>alias name</servlet-name>
<servlet-class>fully qualified class name</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>alias name</servlet-name>
<url-pattern>/url pattern</url-pattern>
</servlet-mapping>
</web-app>
Welcome File list

<web-app>
....
<welcome-file-list>
<welcome-file>home.html</welcome-file>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
</web-app>
Servlet Exception Handling

Handle Http Error Codes


<error-page>
<error-code>http error code</error-code>
<location>/Servlet or Html location</location>
</error-page>
To Handle Java Exception

<error-page>
<exception-type>Exception Class Path
</exception-type>
<location>/Servlet or HTML</location>
</error-page>
Exception Points to Remember

1)Servlet which handles the exception will be


defined in the same usual way as any other
servlet.
2)A single Servlet error handler page can be
used to handle any no. of error-code or
exception-type
ServletConfig

An object of ServletConfig is created by the


web container for each servlet using its
initialization phase. This object can be used to
get configuration information from web.xml file.
An object of ServletConfig is available to the
Servlet during its execution, once the servlet
execution is completed, automatically
ServletConfig interface object will be removed
by the container.
Advantage of ServletConfig

If the configuration information is modified from


the web.xml file, we don't need to change the
Servlet. So it is easier to manage the web
application if any specific content is modified
from time to time.
Example

In web.xml
Methods of ServletConfig interface

1)public String getInitParameter(String name):

Returns the parameter value for the specified parameter


name.

2)public Enumeration getInitParameterNames():

Returns an enumeration of all the initialization parameter


names.

3)public String getServletName():

Returns the name of the Servlet.


How to Get ServletConfig Object into
Servelt

ServletConfig conf = getServletConfig();

In the above statement, we are directly calling


getServletConfig() method as it is available in
Servlet interface, inherited into GenericServlet
and defined and further inherited into
HttpServlet and later inherited into our own
servlet class.
To Retrieve data from ServletConfig
param in web.xml

In order to retrieve the data of the ServletConfig


we have two methods, which are present in
ServletConfig interface.

1)public String getInitParameter("param name");


2)public Enumeration getInitParameterNames();
Notes on ServletConfig

● ServletConfig object exist one per servlet


program.
● An object of ServletConfig created by the
container during its initialization phase.
● An object of ServletConfig is available to the
servlet during its execution, once the servlet
execution is completed, automatically
ServletConfig interface object will be removed
by the container.
Login authentication Example

Lets setup mysql and database connection then


lets create login authentication.
ServletContext Interface

ServletContext is similar to ServletConfig I.e An


object of ServletContext is created by the web
container at time of deploying the project. This
object can be used to get configuration
information from web.xml file and
ServletContext object created for web
application not for particular servlet like
ServletConfig.
Advantage of ServletContext

Easy to maintain if any information is shared to


all the servlet, it is better to make it available for
all the servlet. We provide this information from
the web.xml file, so if the information is
changed, we don't need to modify the servlet.
Thus it removes maintenance problem.
Commonly used methods of
ServletContext interface
● public String getInitParameter(String name): Returns the
parameter value for the specified parameter name.
● public Enumeration getInitParameterNames(): Returns the
names of the context's initialization parameters.
● public void setAttribute(String name,Object object): sets the
given object in the application scope.
● public Object getAttribute(String name): Returns the attribute for
the specified name.
● public Enumeration getInitParameterNames(): Returns the
names of the context's initialization parameters as an Enumeration
of String objects.
● public void removeAttribute(String name): Removes the
attribute with the given name from the servlet context.
How to create ServletContext Object?

getServletContext() method of ServletConfig interface


returns the object of ServletContext.

//We can get the ServletContext object from ServletConfig


object
ServletContext
application=getServletConfig().getServletContext();

//Another convenient way to get the ServletContext object


ServletContext application=getServletContext();
Web.xml syntax
<web-app>
......

<context-param>
<param-name>parametername</param-
name>
<param-value>parametervalue</param-value>

</context-param>
......
</web-app>
Difference Between ServletContext and
ServletConfig

http://bit.do/contextAndConfig

You might also like