Servlet init parameters are for a single servlet only. No body out side that servlet can access that. It is declared inside the <servlet> tag inside Deployment Descriptor, where as context init parameter is for the entire web application. Any servlet or JSP in that web application can access context init parameter. Context parameters are declared in a tag <context-param> directly inside the <web-app> tag. The methods for accessing context init parameter is getServletContext ().getInitParamter (“name”) where as method for accessing servlet init parameter is getServletConfig ().getInitParamter (“name”);
9)What are the different ways for getting a servlet context?
We will get ServletContext by calling getServletConfig ().getServletContext (). This is because a ServletConfig always hold a reference to ServletContext. By calling this.getServletContext () also we will get a ServletContext object.
10)What is the difference between an attribute and a parameter?
The return type of attribute is object, where the return type of parameter is String. The method to retrieve attribute is getAttribute () where as for parameter is getParamter (). We have a method setAttribute to set an attribute. But there is no setters available for setting a parameter.
11)How to make a context thread safe?
Synchronizing the ServletContext is the only solution to make a ServletContext thread safe.
Eg: synchronized (getServletContext ()) {
// do whatever you want with thread safe context.
}
12)What is the difference between setting the session time out in deployment descriptor and setting the time out programmatically?
In DD time out is specified in terms of minutes only. But in programmatically it is specified in seconds. A session time out value of zero or less in DD means that the session will never expire. To specify session will never expire programmatically it must be negative value.
0 comments:
Post a Comment