18. What is the use of HTTPServletRequest Interface?
This interface contains methods to access request information by the HTTP servlets. It is used by servlets to access the parameters sent by client as part of HTTP request from the browser. It is used to access client information like port number, client protocol and form field values.
web.xml is a configuration file which captures the application configuration and deployment details of a web application.
21. How ServletConfig object is loaded and initialized?
- Step 1: When a request to a servlet comes for the first time from client or when the application is deployed the web container will initialize the servlet.
- Step 2: The web container will parse the web.xml file and read the configurations of the servlet to be initialized.
- Step 3: The web container then will create a ServletConfig object and load the configuration details of the servlet in the object.
22. How to use ServletConfig?
ServletConfig is passed as an argument to the init() method of the servlet by the web container during servlet initialization. Hence to use ServletConfig we need to override the method:
public void init(ServletConfig config) throws ServletException
23. What are servlet initialization parameters?
Initialization parameters are used by developers to set some parameter values for the servlet to use. This will be stored as key/value pairs.
Example: Assume in a application there is a rule that by default the employee citizenship is Indian. In EmployeeServlet the developer can set the initialization parameter as “Citizenship = Indian” where the “Citizenship” is the key and “Indian” is the value.
These parameters can be configured in web.xml. These parameters will be loaded to the ServletConfig object during the servlet initialization phase.
24. How do we set init parameter in web.xml?
Init parameters are declared inside <init-param> tag. The parameter name is declared <param-name> and value inside <param-value> respectively. Init parameter is declared separately for each servlet. Each init parameter needs to declared inside a separate <init- param> tag. For example,
25. How to read initialization parameter?
Init parameter can be read using the getInitParameter() method of ServletConfig object.
String var = config.getInitParameter(String parameterName)
Where config is the ServletConfig object and parameterName is the key name of the parameter set in web.xml.
26. What is a servlet context?
It refers to the context in which the servlet runs. It is a gateway between the web container and the servlets. There will be only one instance of servlet context per web application shared by all the servlets in the application. It is used for storing values which can be shared across all the servlets in the web application.
27. Where is servlet context used?
In development projects servlet context are used in the following instances,
- To store master data values which is used by presentation tier, example: states, country, account status etc.
- To store menu links which is used across many JSP/servlets in an application.
28. What is the difference between context parameter and attribute?
- Context parameter are values which is set in the web.xml and can be loaded during the application deployment.
- Context attribute are values which are dynamically set by the application(say servlet) for sharing it across the application.
29. How can we read context parameters?
Context parameters can be read using getInitParameter() method of the ServletContext interface.
String var = context.getInitParameter(String paramName);
Where paramName is the key with which the value is stored in web.xml.
30. What are context attributes?
Context attributes are the attribute values set by any servlet which can be accessed by any other servlet in the same context. It is used for sharing values among other servlets.
31. How can we set context attributes?
Attribute value can be set using the setAttribute() method of the ServletContext interface.
context.setAttribute(String attributeName, Object value);
Where attributeName - is the name of the attribute with which the value can be stored and referred to.
Value – the value stored in the context.
32. How can we read context attributes?
Attribute value can be read using the getAttribute() method of the ServletContext interface.
Object var = context.getAttribute( attributeName);
Where attributeName - is the name of the attribute which needs to be retrieved from the context.
33. What is Response Buffering?
By default, any response written to the output stream is immediately sent to the client as HTTP response. Buffering is the process by which the content are written into a buffer before being sent to the client, thus providing the servlet a better control on the response being sent.
Buffering also makes effective use of network bandwidth since response is sent to the client after a specified buffer size is reached.
34. How servlet works without buffering?
Assume that a client requests for payroll details and PayrollServlet responds back with 200 bytes of data. As soon as the first kb data response is written in the output stream it is pushed to the client. So before the servlet is able to process the entire request the client starts receiving the response.