34
Slide 1 of 30 Servlets And ServletContext

Session 2 servlet context and session tracking - Giáo trình Bách Khoa Aptech

Embed Size (px)

Citation preview

Page 1: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 1 of 30

Servlets And ServletContext

Page 2: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 2 of 30

Objectives Initialising Servlets ServletContext RequestDispatcher Error Handling in Servlets Introduction to Session Tracking Session Tracking Techniques Session Tracking Using Cookies Session Tracking Using HttpSession Session Event Handling

Page 3: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 3 of 30

Need for Initialising Servlets

In Web Application, the database connection information, such as SQL user name and password are not send to the servlet every time a client makes a request. Hence, this information need to be passed tho the servlet before beginning the servlet life cycle. To pass the parameter from the client side to the servlets for executing the first time and retrieve the data required as specified by the user the servlet needs to be initialised.

Page 4: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 4 of 30

Interface “ServletConfig” To pass as an argument during initialisation. The method– getServletName()– getInitParameter()– getServletContext()

Page 5: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 5 of 30

Interface “ServletContext” Retrieve and Send information about the server

in which it is running. Method of Servlet Context– getServerInfo()– getAttribute()– setAttribute()

Page 6: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 6 of 30

Interface “ServletContextListener”

Implementations of this interface receive notifications about changes to the servlet context of the web application they are part of. To recieve notification events, the implementation class must be configured in the deployment descriptor for the web application.

• contextInitialized()• contextDestroyed()

Page 7: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 7 of 30

ServletContextEvent

This is the event class for notifications about changes to the servlet context of a web application.

getServletContext()

Page 8: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 8 of 30

ServletContextAttributeListenerImplementations of this interface recieve notifications of

changes to the attribute list on the servlet context of a web application. To recieve notification events, the implementation class must be configured in the deployment descriptor for the web application.

• attributeAdded()• attributeRemoved()• attributeReplaced()

Page 9: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 9 of 30

ServletContextAttributeEvent

This is the event class for notifications about changes to the attributes of the servlet context of a web application. – getName()– getValue()

Page 10: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 10 of 30

RequestDispatcher

Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name.

This interface is intended to wrap servlets, but a servlet container can create RequestDispatcher objects to wrap any type of resource.

Page 11: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 11 of 30

RequestDispatcher

Void forward(ServletRequest request, ServletResponse response) Forwards a request from a servlet to another resource (servlet,

JSP file, or HTML file) on the server.

Page 12: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 12 of 30

RequestDispatcher

include(ServletRequest request, ServletResponse response) Includes the content of a resource (servlet, JSP page, HTML file) in the

response.

Page 13: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 13 of 30

Error Handling in Servlet

Page 14: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 14 of 30

Status Code

Page 15: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 15 of 30

Reporting Error(HttpResponse) sendError(): Sends an error response to the

client using the specified status. setStatus(): Sets the status code for this

response.

Page 16: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 16 of 30

Logging Errors Servlet can store the actions and error throught

the log() method of GenericServlet class

Page 17: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 17 of 30

Session Is the period of connection between client and server Is a group of activities that are performed by a user

while accessing a particular web site HTTP is a Stateless protocol Http Session are virtual connection between client and

server

17

Page 18: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 18 of 30

Session Tracking Allows the server to keep a track of successive requests

made by same client Session Tracking Techniques

– Hidden form field– URL rewriting– Cookies– HttpSession interface

18

Page 19: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 19 of 30

Hidden Form Field Simplest technique to maintain the state of an end

user. Embedded in an HTML form. Not visible when you view an HTML file in a browser

window.– <input type=“hidden” name=“productId” value=“P01”>

19

Page 20: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 20 of 30

URL Rewriting Maintains the state of end user by modifying the URL. Adds some extra data at the end of the URL Is used when the information to be transferred is not

critical.– <a href=“http://localhost:8080/Books?category=java”> Java

Books </a>– <form action=“http://localhost:8080//UpdateProfile?uid=123”

method=“get”> ---------- </form>

20

Page 21: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 21 of 30

Cookie Is a small piece of information sent by the web

server to the client to keep track of users. Cookie has values in the form of key-value pairs Types of Cookie– Temporary Cookie– Persistent Cookie

A web browser is expected to support 20 Cookies per host

Size of each cookie can be a maximum of 4 KB.

21

Page 22: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 22 of 30

Cookie

Page 23: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 23 of 30

Cookie– Cookie cookie1 = new Cookie(“CustId”,”123”);– public void addCookie(cookie1);– Cookie [] cookies = request.getCookies();– public String getValue();– public void setValue(String newValue)– public String getName() – public void setMaxAge(int expiry) – public int getMaxAge()

23

Page 24: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 24 of 30

Working Cookie Using HttpResponse Using HttpRequest

Page 25: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 25 of 30

HttpSession– 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

25

Page 26: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 26 of 30

HttpSession– request.getSession(boolean create)– public String getId() – public Object getAttribute(String name) – public void setAttribute(String name, Object value) – public void removeAttribute(String name) – public long getCreationTime() – public long getLastAccessedTime() – public int getMaxInactiveInterval() – public void setMaxInactiveInterval(int interval) – public boolean isNew() – public void invalidate()

26

Page 27: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 27 of 30

HttpSessionAttributeListenerThis listener interface can be implemented in order to get

notifications of changes to the attribute lists of sessions within this web application. – attributeAdded()– attributeRemoved()– attributeReplaced()

Page 28: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 28 of 30

HttpSessionBindingListenerCauses an object to be notified when it is bound to or

unbound from a session. The object is notified by an HttpSessionBindingEvent object. – valuedBound()– valueUnbound()

Page 29: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 29 of 30

HttpSessionBindingEvent Events of this type are either sent to an object that

implements HttpSessionBindingListener when it is bound or unbound from a session, or to a HttpSessionAttributeListener that has been configured in the deployment descriptor when any attribute is bound, unbound or replaced in a session. – getName()– getSession()– getValue()

Page 30: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 30 of 30

HttpSessionListener Implementations of this interface may are notified of

changes to the list of active sessions in a web application. To recieve notification events, the implementation class must be configured in the deployment descriptor for the web application – sessionCreated()– sessionDestroyed()

Page 31: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 31 of 30

HttpSessionListener

Page 32: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 32 of 30

HtppSessionActivationListener

Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. – sessionDidActive()– sessionWillPassivate()

Page 33: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 33 of 30

HttpSessionEvent This is the class representing event notifications

for changes to sessions within a web application. – getSession()

Page 34: Session 2   servlet context and session tracking - Giáo trình Bách Khoa Aptech

Slide 34 of 30

Summary

34