WEB
PROGRAMMING
Slides © Marty Hall, [Link] book © Sun Microsystems Press
Overview
• SERVLET
• JSP(JAVA SERVER PAGE)
• APPLET
3 JDBC [Link]
SERVLET
• Servlet technology is used to create web
application (resides at server side and generates
dynamic web page).
• Servlet technology is robust and scalable because
of java language.
• Before Servlet, CGI (Common Gateway Interface)
scripting language was popular as a server-side
programming language.
• But there was many disadvantages of this
technology. We have discussed these
disadvantages below.
• There are many interfaces and classes in the
servlet API such as Servlet, GenericServlet,
4
HttpServlet, ServletRequest, ServletResponse
JDBC
etc.
[Link]
What is a Servlet?
• Servlet can be described in many ways, depending
on the context.
• Servlet is a technology i.e. used to create web
application.
• Servlet is an API that provides many interfaces and
classes including documentations.
• Servlet is an interface that must be implemented
for creating any servlet.
• Servlet is a class that extend the capabilities of the
servers and respond to the incoming request.
• It can respond to any type of requests.
• Servlet is a web component that is deployed on the
server to create dynamic web page.
5 JDBC [Link]
6 JDBC [Link]
What is web application?
• A web application is an application accessible from
the web.
• A web application is composed of web components
like Servlet, JSP, Filter etc. and other components
such as HTML.
• The web components typically execute in Web
Server and respond to HTTP request.
• CGI(Common Gateway Interface)
• CGI technology enables the web server to call an
external program and pass HTTP request
information to the external program to process the
request.
• For each request, it starts a new process.
7 JDBC [Link]
CGI
8 JDBC [Link]
Disadvantages of CGI
• There are many problems in CGI technology:
• If number of clients increases, it takes more time
for sending response.
• For each request, it starts a process and Web
server is limited to start processes.
• It uses platform dependent language e.g. C, C++,
perl.
9 JDBC [Link]
Advantage of Servlet
10 JDBC [Link]
Advantage of Servlet
• There are many advantages of servlet over CGI.
• The web container creates threads for handling the
multiple requests to the servlet.
• Threads have a lot of benefits over the Processes
such as they share a common memory area,
lightweight, cost of communication between the
threads are low.
• The basic benefits of servlet are as follows:
• better performance: because it creates a thread for each
request not process.
• Portability: because it uses java language.
• Robust: Servlets are managed by JVM so we don't need to
worry about memory leak, garbage collection etc.
• Secure: because it uses java language..
11 JDBC [Link]
Servlet Terminology
12 JDBC [Link]
HTTP (Hyper Text Transfer Protocol)
• Http is the protocol that allows web servers and
browsers to exchange data over the web.
• It is a request response protocol.
• Http uses reliable TCP connections by default on
TCP port 80.
• It is stateless means each request is considered as
the new request.
• In other words, server doesn't recognize the user
by default.
13 JDBC [Link]
HTTP (Hyper Text Transfer Protocol)
• .
14 JDBC [Link]
Http Request Methods
• Every request has a header that tells the status of
the client.
• There are many request methods. Get and Post
requests are mostly used.
• The http request methods are:
• GET
• POST
• HEAD
• PUT
• DELETE
• OPTIONS
• TRACE
15 JDBC [Link]
Http Request Methods
16 JDBC [Link]
What is the difference between
Get and Post?
17 JDBC [Link]
Anatomy of Get Request
18 JDBC [Link]
Some other features of GET
requests are:
• It remains in the browser history
• It can be bookmarked
• It can be cached
• It have length restrictions
• It should never be used when dealing with
sensitive data
• It should only be used for retrieving the data
19 JDBC [Link]
Anatomy of POST Request
20 JDBC [Link]
Some other features of POST
requests are:
• This requests cannot be bookmarked
• This requests have no restrictions on length of data
• This requests are never cached
• This requests do not remains in the browser history
21 JDBC [Link]
Server
• It is a running program or software that provides
services.
• There are two types of servers:
• Web Server
• Application Server
22 JDBC [Link]
Web Server
• Web server contains only web or servlet container.
It can be used for servlet, jsp, struts, jsf etc. It can't
be used for EJB.
• Example of Web Servers are: Apache Tomcat and
Resin.
23 JDBC [Link]
Application Server
• Application server contains Web and EJB
containers. It can be used for servlet, jsp, struts,
jsf, ejb etc.
• Example of Application Servers are:
• JBoss Open-source server from JBoss
community.
• Glassfish provided by Sun Microsystem. Now
acquired by Oracle.
• Weblogic provided by Oracle. It is more secured.
• Websphere provided by IBM.
24 JDBC [Link]
Application Server
25 JDBC [Link]
Servlet API
• The [Link] and [Link] packages
represent interfaces and classes for servlet api.
• The [Link] package contains many
interfaces and classes that are used by the servlet
or web container.
• These are not specific to any protocol.
• The [Link] package contains
interfaces and classes that are responsible for http
requests only.
• Let's see what are the interfaces of [Link]
package.
26 JDBC [Link]
Interfaces in [Link]
package
• There are many interfaces in [Link] package. They are as
follows:
• Servlet
• ServletRequest
• ServletResponse
• RequestDispatcher
• ServletConfig
• ServletContext
• SingleThreadModel
• Filter
• FilterConfig
• FilterChain
• ServletRequestListener
• ServletRequestAttributeListener
• ServletContextListener
• ServletContextAttributeListener
27 JDBC [Link]
Classes in [Link] package
• There are many classes in [Link] package.
They are as follows:
• GenericServlet
• ServletInputStream
• ServletOutputStream
• ServletRequestWrapper
• ServletResponseWrapper
• ServletRequestEvent
• ServletContextEvent
• ServletRequestAttributeEvent
• ServletContextAttributeEvent
• ServletException
• UnavailableException
28 JDBC [Link]
Interfaces in [Link] package
• There are many interfaces in [Link]
package. They are as follows:
• HttpServletRequest
• HttpServletResponse
• HttpSession
• HttpSessionListener
• HttpSessionAttributeListener
• HttpSessionBindingListener
• HttpSessionActivationListener
• HttpSessionContext (deprecated now)
29 JDBC [Link]
Classes in [Link] package
• There are many classes in [Link]
package. They are as follows:
• HttpServlet
• Cookie
• HttpServletRequestWrapper
• HttpServletResponseWrapper
• HttpSessionEvent
• HttpSessionBindingEvent
• HttpUtils (deprecated now)
30 JDBC [Link]
Servlet Interface
• Servlet interface provides common behavior to all
the servlets.
• 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.
31 JDBC [Link]
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.
32 JDBC [Link]
Generic Servlet class
• GenericServlet class implements Servlet,
ServletConfig and Serializable interfaces.
• It provides the implementaion of all the methods of
these interfaces except the service method.
• GenericServlet class can handle any type of
request so it is protocol-independent.
• You may create a generic servlet by inheriting the
GenericServlet class and providing the
implementation of the service method.
33 JDBC [Link]
Methods of Generic Servlet
class
34 JDBC [Link]
35 JDBC [Link]
• The HttpServlet class extends the
GenericServlet class and implements
Serializable interface.
• It provides http specific methods such as
doGet, doPost, doHead, doTrace etc.
36 JDBC [Link]
37 JDBC [Link]
Life Cycle of a Servlet (Servlet
Life Cycle)
• The web container maintains the life cycle of a
servlet instance.
• Let's see the life cycle of the servlet:
• Servlet class is loaded.
• Servlet instance is created.
• init method is invoked.
• service method is invoked.
• destroy method is invoked.
38 JDBC [Link]
Life Cycle of a Servlet
39 JDBC [Link]
Life Cycle of a Servlet
• there are three states of a servlet: new, ready and
end.
• The servlet is in new state if servlet instance is
created.
• After invoking the init() method, Servlet comes in
the ready state.
• In the ready state, servlet performs all the tasks.
When the web container invokes the destroy()
method, it shifts to the end state.
40 JDBC [Link]
• 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.
41 JDBC [Link]
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 [Link] interface.
Syntax of the init method is given below:
public void init(ServletConfig config) throws ServletException
42 JDBC [Link]
• 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:
43 JDBC [Link]
• 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:
44 JDBC [Link]
Steps to create a servlet example
• The servlet example can be created by three ways:
• By implementing Servlet interface,
• By inheriting GenericServlet class, (or)
• By inheriting HttpServlet class
• The mostly used approach is by extending
HttpServlet because it provides http request
specific method such as doGet(), doPost(),
doHead() etc.
45 JDBC [Link]
In this example we are going to create a servlet that extends the
HttpServlet class. In this example, we are inheriting the HttpServlet class
and providing the implementation of the doGet() method. Notice that get
request is the default request.
46 JDBC [Link]
RequestDispatcher in Servlet
• 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.
47 JDBC [Link]
How to get the object of
RequestDispatcher
• The getRequestDispatcher() method of
ServletRequest interface returns the object of
RequestDispatcher. Syntax:
• Syntax of getRequestDispatcher method
48 JDBC [Link]
Example of RequestDispatcher
interface
• In this example, we are validating the password entered by
the user.
• If password is servlet, it will forward the request to the
WelcomeServlet, otherwise will show an error message:
sorry username or password error!.
• In this program, we are cheking for hardcoded information.
But you can check it to the database also that we will see in
the development chapter.
• In this example, we have created following files:
• [Link] file: for getting input from the user.
• [Link] file: a servlet class for processing the
response. If password is servet, it will forward the request
to the welcome servlet.
• [Link] file: a servlet class for displaying the
welcome message.
49
• JDBC
[Link] file: a deployment descriptor file that contains the
[Link]
50 JDBC [Link]
51 JDBC [Link]
52 JDBC [Link]
53 JDBC [Link]
SendRedirect in servlet
• The sendRedirect() method of
HttpServletResponse interface can be used to
redirect response to another resource, it may be
servlet, jsp or html file.
• It accepts relative as well as absolute URL.
• It works at client side because it uses the url bar of
the browser to make another request.
• So, it can work inside and outside the server.
54 JDBC [Link]
55 JDBC [Link]
In this example, we are redirecting the request to the google server.
Notice that sendRedirect method works at client side, that is why we can
our request to anywhere. We can send our request within and outside the
server.
56 JDBC [Link]
Creating custom google search
using sendRedirect
57 JDBC [Link]
58 JDBC [Link]
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.
59 JDBC [Link]
60 JDBC [Link]
• Non-persistent cookie
• It is valid for single session only. It is removed each time
when user closes the browser.
• Persistent cookie
• It is valid for multiple session . It is not removed each
time when user closes the browser. It is removed only if
user logout or signout.
• Advantage of Cookies
• Simplest technique of maintaining the state.
• Cookies are maintained at client side.
• Disadvantage of Cookies
• It will not work if cookie is disabled from the browser.
• Only textual information can be set in Cookie object.
61 JDBC [Link]
Cookie class
• [Link] class provides the
functionality of using cookies. It provides a lot of
useful methods for cookies.
• Constructor of Cookie class
62 JDBC [Link]
Other methods required for using
Cookies
[Link] adding cookie or getting the value from the cookie, we need some
methods provided by other interfaces. They are: public void
addCookie(Cookie ck):method of HttpServletResponse interface is used
to add cookie in response object.
[Link] Cookie[] getCookies():method of HttpServletRequest interface
is used to return all the cookies from the browser.
63 JDBC [Link]
How to create Cookie?
64 JDBC [Link]
How to delete Cookie?
65 JDBC [Link]
How to get Cookies?
66 JDBC [Link]
Simple example of Servlet
Cookies
• In this example, we are storing the name of the
user in the cookie object and accessing it in
another servlet.
• As we know well that session corresponds to the
particular user.
• So if you access it from too many browsers with
different values, you will get the different value.
67 JDBC [Link]
68 JDBC [Link]
69 JDBC [Link]
70 JDBC [Link]
71 JDBC [Link]
Example of Registration form in
servlet
Here, you will learn that how to
create simple registration form in
servlet. We are using oracle10g
database. So you need to create a
table first as given below:
72 JDBC [Link]
Example of Registration form in
servlet
• In this example, we have created the three pages.
• [Link]
• [Link]
• [Link]
73 JDBC [Link]
[Link]
74 JDBC [Link]
75 JDBC [Link]
JSP(Java Server
Page)
76 JDBC [Link]
JSP
• JSP technology is used to create web application
just like Servlet technology.
• It can be thought of as an extension to servlet
because it provides more functionality than servlet
such as expression language, jstl etc.
• A JSP page consists of HTML tags and JSP tags.
The jsp pages are easier to maintain than servlet
because we can separate designing and
development. It provides some additional features
such as Expression Language, Custom Tag etc.
77 JDBC [Link]
Advantage of JSP over Servlet
There are many advantages of JSP over servlet. They are as follows:
1) Extension to Servlet
JSP technology is the extension to servlet technology. We can use all the features of servlet
in JSP. In addition to, we can use implicit objects, predefined tags, expression language and
Custom tags in JSP, that makes JSP development easy.
2) Easy to maintain
JSP can be easily managed because we can easily separate our business logic with
presentation logic. In servlet technology, we mix our business logic with the presentation
logic.
3) Fast Development: No need to recompile and redeploy
If JSP page is modified, we don't need to recompile and redeploy the project. The servlet
code needs to be updated and recompiled if we have to change the look and feel of the
application.
4) Less code than Servlet
In JSP, we can use a lot of tags such as action tags, jstl, custom tags etc. that reduces the
code. Moreover, we can use EL, implicit objects etc.
78 JDBC [Link]
Life cycle of a JSP Page
79 JDBC [Link]
• As depicted in the above diagram, JSP page is
translated into servlet by the help of JSP translator.
• The JSP translator is a part of webserver that is
responsible to translate the JSP page into servlet.
• After that Servlet page is compiled by the compiler
and gets converted into the class file.
• Moreover, all the processes that happens in servlet
is performed on JSP later like initialization,
committing response to the browser and destroy.
80 JDBC [Link]
Creating a simple JSP Page
• To create the first jsp page, write some html code
as given below, and save it by .jsp extension.
• We have save this file as [Link].
• Put it in a folder and paste the folder in the web-
apps directory in apache tomcat to run the jsp
page.
81 JDBC [Link]
JSP API
• The JSP API consists of two packages:
• [Link]
• [Link]
82 JDBC [Link]
[Link] package
• The [Link] package has two interfaces
and classes.
• The two interfaces are as follows:
JspPage
HttpJspPage
The classes are as follows:
JspWriter
PageContext
JspFactory
JspEngineInfo
JspException
JspError
83 JDBC [Link]
84 JDBC [Link]
Example of JSP scriptlet tag that
prints the user name
85 JDBC [Link]
86 JDBC [Link]
JSP expression tag
• The code placed within JSP expression tag is
written to the output stream of the response.
• So you need not write [Link]() to write data.
• It is mainly used to print the values of variable or
method.
87 JDBC [Link]
JSP expression tag
88 JDBC [Link]
89 JDBC [Link]
90 JDBC [Link]
JSP Declaration Tag
• The JSP declaration tag is used to declare fields
and methods.
• The code written inside the jsp declaration tag is
placed outside the service() method of auto
generated servlet.
• So it doesn't get memory at each request.
91 JDBC [Link]
Syntax of JSP declaration tag
• The syntax of the declaration tag is as follows:
92 JDBC [Link]
Example of JSP declaration tag
that declares field
• In this example of JSP declaration tag, we are
declaring the field and printing the value of the
declared field using the jsp expression tag.
93 JDBC [Link]
Example of JSP declaration tag
that declares method
• In this example of JSP declaration tag, we are
defining the method which returns the cube of
given number and calling this method from the jsp
expression tag. But we can also use jsp scriptlet
tag to call the declared method.
94 JDBC [Link]
JAVA APPLET
95 JDBC [Link]
Applet
• Applet is a special type of program that is embedded in the
webpage to generate the dynamic content.
• It runs inside the browser and works at client side.
• Advantage of Applet
• There are many advantages of applet. They are as follows:
• It works at client side so less response time.
• Secured
• It can be executed by browsers running under many
platforms, including Linux, Windows, Mac Os etc.
• Drawback of Applet
• Plugin is required at client browser to execute applet.
96 JDBC [Link]
Hierarchy of Applets
97 JDBC [Link]
Lifecycle of Java Applet
98 JDBC [Link]
Lifecycle methods for Applet:
• The [Link] class 4 life cycle methods and
[Link] class provides 1 life cycle methods for an applet.
• [Link] class
• For creating any applet [Link] class must be inherited. It
provides 4 life cycle methods of applet.
• public void init(): is used to initialized the Applet. It is invoked only
once.
• public void start(): is invoked after the init() method or browser is
maximized. It is used to start the Applet.
• public void stop(): is used to stop the Applet. It is invoked when Applet
is stop or browser is minimized.
• public void destroy(): is used to destroy the Applet. It is invoked only
once.
99 JDBC [Link]
How to run an Applet?
• There are two ways to run an applet
• By html file.
• By appletViewer tool (for testing purpose).
100 JDBC [Link]
101 JDBC [Link]
102 JDBC [Link]
103 JDBC [Link]
104 JDBC [Link]
105 JDBC [Link]
106 JDBC [Link]
107 JDBC [Link]
108 JDBC [Link]
109 JDBC [Link]
110 JDBC [Link]
111 JDBC [Link]
112 JDBC [Link]
113 JDBC [Link]
114 JDBC [Link]
115 JDBC [Link]
116 JDBC [Link]
117 JDBC [Link]
118 JDBC [Link]
119 JDBC [Link]
120 JDBC [Link]
121 JDBC [Link]
122 JDBC [Link]
123 JDBC [Link]
124 JDBC [Link]
125 JDBC [Link]
126 JDBC [Link]
127 JDBC [Link]
128 JDBC [Link]
129 JDBC [Link]
130 JDBC [Link]
131 JDBC [Link]
132 JDBC [Link]
133 JDBC [Link]
134 JDBC [Link]
135 JDBC [Link]
136 JDBC [Link]
137 JDBC [Link]
138 JDBC [Link]
139 JDBC [Link]
140 JDBC [Link]