A ServletContext is common for all servlets and jsps ain a webapp. They can communicate with each other and with container through that object.
The important methods of ServletContext:
-getInitParameter(String)
-getInitParameterNames()
-getAttribute(String)
-getAttributeNames()
-setAttribute(String,Object)
-getMejorVersion()
-getResourseAsStream()
-getRequestDispatcher
-log(String)
The get/set Attribute is used for communication.
Suppose one servlet wants to share two objects(obj1,obj2) for all the web application. So it will do like this,
getServletContext.setAttribute(“first”,obj1);//the name “first” will be used for getting obj1
getServletContext.setAttribute(“second”,obj2);
The servlets wants to get those objets will get like this,
getServletContext.getAttribute(“first”);
getServletContext.getAttribute(“second”);
now the servlet gets the object.
In JSP thing is little bit different. JSP has implicit object ServletContext. With that object we can get the objects.
getAttributeNames() returns an Enumeration holding all the data of ServletContext.
removeAttribute(String name) is to remove an attribute.
No comments:
Post a Comment