Professional Documents
Culture Documents
Web Servers and Servlets Pre-Requisites: Prof. Rama Chandra Rao Meka RGMCET Nandyal
Web Servers and Servlets Pre-Requisites: Prof. Rama Chandra Rao Meka RGMCET Nandyal
URL HTTP
• Stands for Uniform Resource Locator. • Stands for Hyper Text Transfer Protocol.
• The address that is typed in a web- • The most widely used protocol for
browser in order to load a page from a transferring data between web-browsers
remote location. and web-servers.
• Request and response to a typed URL is
in terms of the http protocol. • Is a stateless protocol implemented at the
Application Layer.
• Typically consists of the protocol, followed
by a “:” and an IP address of a machine • The default http port is : 80
followed by the file name to load.
HTML Web-server
• Stands for Hyper-text Markup Language. • A web server is a computer on the internet
• Most widely used language for data that hosts websites, serving pages to
transfer across the web. viewers upon request.
• Provides means to create structured • It has a unique IP address.
documents by denoting structural • Needs software to enable hosting of
semantics such as headings, paragraphs, pages, scripting etc.
lists, links etc. • Maps the path component of the URL into
a local file system resource.
1
25-Sep-21
2
25-Sep-21
3
25-Sep-21
4
25-Sep-21
Exceptions
• ServletException – thrown when the • PostParameterServlet.java
servlet encounters a problem.
• UnavailableException – extends
ServletException. Thrown when the
requested server is not available.
5
25-Sep-21
Cookies
• HttpGetServlet.java • The “Cookie” class encapsulates a cookie.
• A cookie is stored on the client (browser)
side.
• It is useful in tracking user activities.
• If some information is necessary to be
passed between the client and server on
every request, it is ideal to send it via a
cookie.
6
25-Sep-21
Cookies… Cookies…
• With cookies, user does not have to enter the • Cookies are stored as “name-value” pairs
data each time he visits the same website. on the client machine.
• Typically a cookie is saved in a file on the client’s • In addition, the expiration date of the
machine, along with the domain and path of the cookie is also saved. The expiration date
webserver. decides when the cookie is to be deleted
• When the user enters a URL, the domain and from the client-machine.
path are checked and if a corresponding cookie • Cookies can be stored on the client
exists, it is sent silently to the web-server. machine by a servlet executing on the
server side.
Cookies… Cookies…
• The servlet can call the “addCookie()” • A few important methods are:
method to store a cookie on the client – String getName()
machine.
– String getValue()
• The addCookie() method belongs to the – void setMaxAge(int secs)
HttpServletResponse interface.
– int getMaxAge()
• The cookie is then sent as part of the
– String getDomain()
response header.
• Constructor: Cookie(String name, String
value)
Cookies.. Cookies…
• A word of caution with unfamiliar sites…. • SetCookiesServlet.java
• Cookies.html
• getCookiesServlet.java
7
25-Sep-21
8
25-Sep-21
home=Cosby&favorite+flavor=flies
HttpSession HttpSession
• Enables a servlet to read and write state • The HttpSession object provides methods
information associated with an HTTP session. such as setAttribute(), getAttribute(),
• A new session can be created using the removeAttribute() and getAttributeNames()
getSession(true) call on a HttpServletRequest to manage bindings between objects and
object. With this call if a session already exists
names.
for the request object, it is returned. Else a new
HttpSession object is created and returned. • All servlets associated with a particular
• Provides a way of getting around the “stateless” client will share the session state.
nature of the Http protocol.