Inroduction To Servlet Inroduction To Servlet: Made By:-Pankaj Malik Mba (It)
Inroduction To Servlet Inroduction To Servlet: Made By:-Pankaj Malik Mba (It)
e b S er ve r
est 2, W ng
erRe
q u retrievi
1 . Us
e s t e dstatic
The requ ge
web pa Server’s
Internet
File
. Repl y System
3
Web
Server
HTTP Protocol
• This kind of Client/Server communication
should follow a Protocol for its smooth
functioning
• Hyper Text Transfer Protocol or HTTP is the
protocol used by the Web
• According to the HTTP protocol, the client
should send a Request to the server in a special
format
• Based on the Request, the server will send back a
Response, which again is in a special format
HTTP
• The Request
following is the request generated by an
Internet Browser when the URL was
http://www.yahoo.com
abcbooks.co.in
http://abcbooks.co.in/eshop/ 202.68.33.49
PurchaseItem?iname=Floppy+Disc&qty=12
4. Compose HTML
output
Web Server
HTTP Request
User requests for
a web page
Request for static
pages
Request for
dynamic pages
Query
database for Login Bill Purchase
Item
data
Sample Session
• Client opens a connection to the server
• Client makes a request to the server:
▫ GET /index.html HTTP/1.0
• The Server responds to the request
▫ HTTP/1.0 200 OK
▫ [more header stuff]
▫ CRLF
▫ <HTML>
▫ [html code from index.html]
▫ </HTML>
• The connection is closed by the server (or client)
GET method of HTTP
• Simplest and Most frequently used method
HTTP Request
GenericServlet
HttpServlet
Servlet Life Cycle
Client Interaction
service ( )
Servlet
init ( ) destroy ( )
• HttpServlet:
▫ From package javax.servlet.http
▫ Provides an HTTP specific implementation of the
Servlet interface
▫ This will most likely be the class that all of our
servlets will extend
Methods from HttpServlet class
• Methods:
• public String getHeader (String name)
▫ Returns the value of the specified request
header as a String.
▫ If the request did not include a header of the
specified name, this method returns null.
▫ The header name is case insensitive
Javax.servlet.http.HttpServletRequest
{ response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body><head><title>Hello
World!</title></head><body><h1>Hello
World!</h1></body></html>");
}
}
Step 2 - web.xml file entries
<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>myPack.HelloServlet</servlet-
class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>hello</url-pattern>
</servlet-mapping>
</web-app>
Step 3 –Give the request to the Web Server with following URL
• Methods:
• public String getHeader (String name)
▫ Returns the value of the specified request
header as a String.
▫ If the request did not include a header of the
specified name, this method returns null.
▫ The header name is case insensitive
Javax.servlet.http.HttpServlet
Request
• HttpServletRequest interface
▫ Object passed to doGet and doPost
▫ Extends ServletRequest
• Methods
▫ String getParameter( String name )
Returns value of parameter name (part of GET or POST)
▫ Enumeration getParameterNames()
Returns names of parameters (POST)
▫ String[] getParameterValues( String name
)
Returns array of strings containing values of a parameter
▫ Cookie[] getCookies()
Returns array of Cookie objects, can be used to identify client
HttpServletResponse Interface
• HttpServletResponse
▫ Object passed to doGet and doPost
▫ Extends ServletResponse
• Methods
▫ void addCookie( Cookie cookie )
Add Cookie to header of response to client
▫ ServletOutputStream getOutputStream()
Gets byte-based output stream, send binary data to client
▫ PrintWriter getWriter()
Gets character-based output stream, send text to client
▫ void setContentType( String type )
Specify MIME type of the response
Helps display data
Managing Sessions
• HTTP is a stateless protocol and therefore
cannot store the information about the user
activities across web pages.
• Session management is the process of keeping
track of the activities of a user across web pages.
• Session tracking can also be used to keep track
of the user’s perferences.
Session Tracking
• Web sites
▫ Many have custom web pages functionality
Custom home pages - http://my.yahoo.com/
Shopping carts
Marketing
▫ HTTP protocol does not support persistent
information
Cannot distinguish clients
• Distinguishing clients
▫ Hidden variables
▫ Cookies
▫ Session Tracking
• Cookies
Cookies
▫ Small files that store information on client's
computer
▫ Servlet can check previous cookies for information
• Header
▫ In every HTTP client-server interaction
▫ Contains information about request (GET or POST)
and cookies stored on client machine
▫ Response header includes cookies servers wants to
store
• Age
▫ Cookies have a lifespan
▫ Can set maximum age
Cookies can expire and are deleted
Session Tracking with HttpSession
• HttpSession is an alternative to cookies. It
keeps the session data available until browsing
ends
• Methods
▫ getSession( createNew )
Class HttpServletRequest
Returns client's previous HttpSession object
▫ putvalue( name, value )
Adds a name/value pair to object
▫ getValueNames()
Returns array of Strings with names
▫ getValue( name )
Returns value of name as an Object
Cast to proper type
Need of Session Tracking
• The server should be able to identify that a series
of requests are coming from the same client
Client Server
• URLRewriting - encodeURL()
• Cookies
• HttpSession
URL rewriting
• By rewriting URLs all links and redirections which are
created by a Servlet have to be encoded to include the
session ID.
• It also does not allow the use of static pages. All HTML
pages which are sent within a session have to be created
dynamically.
URL rewriting
• The URL is constructed using an HTTP GET
• Read or write to it
• getId()
▫ Returns a string containing the unique identifier assigned to this
session. Useful when using URL rewriting to identify the session.
• setAttribute()
▫ Binds an object to this session, using the name specified. (Note: this
replaces the putValue() method of JSDK 2.1.)
Servlet Collaboration
• In a web application scenario a servlet receives an HTTP
request from a client, processes application logic
partially and hands over the request to another servlet.
RequestDispatcher
rd=getServletContext().getRequestDispatcher
(String path)
rd.forward(req,res)
Note:
• Should not write anything to the response.
• Use reset() method on response.
Configuring Context parameter
<web-app>
<servlet>…</servlet>
<context-param>
<param-name>user</param-name>
<param-value>scott</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>tiger</param-value>
</context-param>
</web-app>
Servlets & Applets
Servlet Applet
Subclass of GenericServlet Subclass of Applet
Runs in a server Runs in a browser
Must be multithreaded or Generally single thread per
thread safe applet
No direct user interface Uses AWT for user interface
If downloaded to server, If downloaded to no access
browser, controlled access to to files and access back only
files and network network to serving host
If local to server, full access n/a
to files and network