Servlet Context, in simple terms, as a shared memory segment for a web application.
when an object has been assigned to this shared memory, will be available to whole web application, until and unless it has been removed.
For every web application, there will be one of ServletContext.
Servlet context is defined in javax.servlet package.
Methods defined in this packages are used at server side to communicate with the servlet container.
They are as follows:
setAttribute()-- binds the object with the specified name, you give, in the current servlet context.
getAttribute()--returns the object assigned by the name provided.
removeAttribute()-- removes the particular object from servlet context.
getAttributeNames()--returns all the objects stored in the current servlet context.
Now, how to get a reference of servlet context,
ServletContext context=getServletContext(); //created context reference object
Now using context object set your object in servletcontext,
String name="JIIT";
context.setAttribute("USER",name);
So,now name object got set in the shared memory of web-application as USER and can be accessible throughout the web-application.
No comments:
Post a Comment