Sie sind auf Seite 1von 25

Servlet

Copyright © 2015Tech Mahindra. All rights reserved. 1


Objectives
• Session Management

• Cookie
• HttpSession
• HttpRequest Headers

• HttpResponse Headers

Copyright © 2015Tech Mahindra. All rights reserved. 2


Session Management
• HTTP is ‘Stateless’ protocal.

• The client browser makes a connection to the server, sends the request, gets the request
and closes the connection.

• The connection exists for only a single request/response.


• Because the connection don’t’ persists, the container doesn’t recognize that the client
making second request is same client from the previous request.

• The container considers each and every request made is from a new client.

Copyright © 2015Tech Mahindra. All rights reserved. 3


Session Management

 We need session management in applications like online shopping where server needs
to remember the client details during transactions.

 User session can be managed by the following techniques.
• Hidden Fields

• URL rewriting
• Cookie

• HttpSession

Copyright © 2015Tech Mahindra. All rights reserved. 4



Cookies
Cookie is a small piece of data exchanged in between server and browser.

• The server sends the cookie to the client, and the client returns the cookie when the
client makes another request.

• Cookies are stored on client side.

• By default, cookies lives on client browser as long as session.


• Once the client browser closes, cookies disappear.
• But we can specify how long a cookie is alive even after the browser shutdowns.
• Using a single cookie we can store only 4kb of information .

Copyright © 2015Tech Mahindra. All rights reserved. 5


Cookie Class
• Cookie( String , String ) // constructor

• int getMaxAge()
• String getName()
• String getValue()

• void setMaxAge(int )
• boolean getSecure()

• void setSecure(boolean)
• void setDomain(String)
• String getDomain()

Copyright © 2015Tech Mahindra. All rights reserved. 6


Sample Servlet Programs which sets cookies
 ServletA.java

Copyright © 2015Tech Mahindra. All rights reserved. 7


Sample Servlet program to retrieve cookies
 ServletB.java

Copyright © 2015Tech Mahindra. All rights reserved. 8


Run ServletA
 Run the ServletA by pressing Shift+F6

Copyright © 2015Tech Mahindra. All rights reserved. 9


Output of ServletA

Copyright © 2015Tech Mahindra. All rights reserved. 10


Output of ServletB

Copyright © 2015Tech Mahindra. All rights reserved. 11


Session Tracking
• Server uses session tracking mechanism to remember a client.

• HttpSession is useful in session tracking.


• Session tracking internally uses cookie concept .
• Using cookie we can store limited client data on client browser.
• Cookies are stored on client side.

• Container creates HttpSession object and saves on server side.


• User session data can be saved on server side using session object( as attributes ).
• Server creates a session object for each user and generates unique token to identify
session object on server side.

• After generating unique token , server sends token back to the client as a cookie with
 ‘set-cookie’ response header.
• Client browser sends token with ‘cookie’ request header to the server when making
other request.
Copyright © 2015Tech Mahindra. All rights reserved. 12
HttpSession Interface
• Object getAttribute(“attribute name”)

• long getCreationTime()
• String getId()
• int getLastAccessedTime()
• int getMaxInactiveInterval()

• ServletContext getServletContext()
• void invalidate()
• boolean isNew()

• void removeAttribute(“attribute name”)


• void setAttribute(“attribute name”, Object)
• void setMaxInactiveInterval(int)
Copyright © 2015Tech Mahindra. All rights reserved. 13
Sample Servlet for creating HttpSession and
 ServletX.java setting attributes

Copyright © 2015Tech Mahindra. All rights reserved. 14


Sample Servlet for retrieving user data from
 ServletY.java session

Copyright © 2015Tech Mahindra. All rights reserved. 15


Run ServletX
 Run the ServletX by pressing Shift+F6.

Copyright © 2015Tech Mahindra. All rights reserved. 16


Output of ServletX

Copyright © 2015Tech Mahindra. All rights reserved. 17


Output of ServletY

Copyright © 2015Tech Mahindra. All rights reserved. 18


HTTP GET Request
 GET/SampleWebApplication/ServletA?username=msat&password=techm HTTP/1.1

 Host:www.localhost.com
 User-Agent:Mozilla/5.0 Netscape/7.1
 Accept:text/xml,application/xml,text/html

 Accept-Language:en-us;
 Accept-Encoding:gzip,deflate;

 Accept-Charset:ISO-8859,utf-8
 Keep-Alive:300
 Connection:keep-alive

Copyright © 2015Tech Mahindra. All rights reserved. 19



HTTP POST Request
POST/SampleWebApplication/ServletA HTTP/1.1

 Host:www.localhost.com

 User-Agent:Mozilla/5.0 Netscape/7.1
 Accept:text/xml,application/xml,text/html
 Accept-Language:en-us;

 Accept-Encoding:gzip,deflate;
 Accept-Charset:ISO-8859,utf-8
 Keep-Alive:300
 Connection:keep-alive

 username=msat&password=techm
Copyright © 2015Tech Mahindra. All rights reserved. 20
HTTP Response
 HTTP/1.1 200 OK
 Set-Cookie:JSESSIONID=OAAB6324332SDFSASDS;
 Content-Type:text/html
 Content-Length:399
 Date:Mon, 15 NOV 2010 08:08:40 GMT
 Server:Apache;
 Connection:close

 <html>
 <body>
 ServletA
 </body>
 </html>

Copyright © 2015Tech Mahindra. All rights reserved. 21


doXXX() methods
• doGet( )

• doPost( )
• doDelete( )
• doPut( )

• doTrace( )
• doOptions( )

Copyright © 2015Tech Mahindra. All rights reserved. 22


Version History
Version History
Version Created/
Date Changes made Reviewed by
No Changed by

1 11-May-15 Ushakiran Chivaluri Conversion to 2015 template Prakash Goteti

Copyright © 2015Tech Mahindra. All rights reserved. 23


Thank you
Visit us at www.techmahindra.com

Disclaimer
Tech Mahindra Limited, herein referred to as TechM provide a wide array of presentations and reports, with the contributions of
various professionals. These presentations and reports are for informational purposes and private circulation only and do not
constitute an offer to buy or sell any securities mentioned therein. They do not purport to be a complete description of the markets
conditions or developments referred to in the material. While utmost care has been taken in preparing the above, we claim no
responsibility for their accuracy. We shall not be liable for any direct or indirect losses arising from the use thereof and the viewers are
requested to use the information contained herein at their own risk. These presentations and reports should not be reproduced, re-
circulated, published in any media, website or otherwise, in any form or manner, in part or as a whole, without the express consent in
writing of TechM or its subsidiaries. Any unauthorized use, disclosure or public dissemination of information contained herein is
prohibited. Unless specifically noted, TechM is not responsible for the content of these presentations and/or the opinions of the
presenters. Individual situations and local practices and standards may vary, so viewers and others utilizing information contained
within a presentation are free to adopt differing standards and approaches as they see fit. You may not repackage or sell the
presentation. Products and names mentioned in materials or presentations are the property of their respective owners and the
mention of them does not constitute an endorsement by TechM. Information contained in a presentation hosted or promoted by
TechM is provided “as is” without warranty of any kind, either expressed or implied, including any warranty of merchantability or
fitness for a particular purpose. TechM assumes no liability or responsibility for the contents of a presentation or the opinions
expressed by the presenters. All expressions of opinion are subject to change without notice.

Copyright © 2015Tech Mahindra. All rights reserved. 24


Copyright © 2015Tech Mahindra. All rights reserved. 25

Das könnte Ihnen auch gefallen