Java New Servlets
Java New Servlets
Introduction
Need for creating Dynamic Web Pages.
Java Servlets are used to design dynamic web
pages.
Servlets are the Java programs that runs on the
time.
Limited number of requests that can be
processed concurrently.
CGI applications are platform dependent.
Servlets
Java 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
Using Servlets, you can collect input from
users through web page forms, present records
from a database or another source, and create
web pages dynamically.
Servlets can perform session tracking CGI cannot perform session tracking
Servlets can read and set HTTP headers, CGI can’t read and set HTTP headers,
handle cookies handle cookies
Servlet Applications
Search engines
E-commerce applications
Shopping carts
Product catalogs
Intranet application
GenericServlet HttpServlet
Defines a generic, protocol- Defines a HTTP protocol specific
independent servlet. servlet.
Gives a blueprint and makes writing Gives a blueprint for Http servlet and
servlet easier. makes writing them easier.
Interfaces Description
Classes Description
Interfaces Description
Classes Description
In Servlet
Focusing Advertisement
Cookies
The Cookie class provides an easy way for servlet to
read, create, and manipulate HTTP cookies on the
web browser.
getCookies():To retrieve cookies as request.
addCookie(): To send a new cookie to the browser.
response.addCookie(ck);
Creating a Cookie
Example
WriteCookie.java : An HTTP servlet, which is a
Java class that extends HttpServlet.
cookieWrite.html : A web page to invoke the servlet
by sending a request.
web.xml : A deployment descriptor which will map
the request from the web page to the servlet.
Creating a Cookie
Creating a Cookie
Creating a Cookie
Creating a Cookie
Creating a Cookie
Reading a Cookie
Example
ReadCookieServlet.java : An HTTP servlet, which
is a Java class that extends HttpServlet.
cookieRead.html : A web page to invoke the servlet
by sending a request.
web.xml : A deployment descriptor which will map
the request from the web page to the servlet.
Reading a Cookie
Reading a Cookie
Reading a Cookie
Reading a Cookie
Reading a Cookie
Session Handling
HTTP is a “stateless” protocol.
Each time a client retrieves a Web page, the client opens
a separate connection to the Web server and the server
does not automatically maintain contextual information
about the client.
No built-in support for maintaining contextual
information in Server.
There are 4 typical solutions to this problem:
Cookies
URL rewriting
HttpSession
Session Handling
Cookies:
A web server can assign a unique session ID as a
cookie to each web client and for subsequent requests
from the client they can be recognized using the
received cookie.
URL rewriting:
You can append some extra data on the end of each
URL that identifies the session, and the server can
associate that session identifier with data it has stored
about that session.
http://jmaster.in/home.html;sessionid=12345
Session Handling
Hidden form fields:
A web server can send a hidden HTML form field
along with a unique session ID as follows:
<input type="hidden" name="sessionid"
value="12345">
This entry means that, when the form is submitted, the
specified name and value are automatically included in
the request.
Each time when web browser sends request back, then
session_id value can be used to keep the track of
different web browsers.
Session Handling
HttpSession:
Servlet provides HttpSession Interface which provides
a way to identify a user across more than one page
request or visit to a Web site and to store information
about that user.
The servlet container uses this interface to create a
session between an HTTP client and an HTTP server.
The session persists for a specified time period, across
more than one connection or page request from the
user.
Session Tracking Basics
Using sessions in servlets is straightforward and
involves four basic steps.
Accessing the session object associated with the
current request
Call request.getSession() to get an HttpSession object,
which is a simple hash table for storing user-specific
data.
Looking up information associated with a session
Call getAttribute() on the HttpSession object, cast the
return value to the appropriate type, and check whether
the result is null.
Session Tracking Basics
Storing information in a session
Use setAttribute() with a key and a value.
javax.servlet.http.HttpServletResponse
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.
There are two methods defined in the
RequestDispatcher interface.
forward()
include()
RequestDispatcher in Servlet
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.