Pages

Friday 3 January 2020

Parameters

Servlet Instance Creation
  • By default, the servlet instance will be created when the first client will send the request to the servlet.
  • Only one instance will be created for one servlet and will be used to process all the requests by using multiple threads.
  • If you want to create the instance while starting the server or container then you can use the following:
In web.xml
<servlet>
<load-on-startup>X</load-on-startup>
</servlet>
In Annotation
@WebServlet(loadOnStartup=X)

Note: X will be int type value. It must be +ve integer.
It indicates in which order the servlet instance will be created.

Parameters
  • Parameter is name-value pair.
  • Parameter name and value are of type String.
  • The parameter is read-only i.e web container stores the parameter in the corresponding object and you can read and use that value. You can not modify the parameters.
There are 3 types of parameters
  1. ServletRequest Parameters
  2. ServletConfig Parameters
  3. ServletContex Parameters
ServletRequest Parameters
Client submitted data which is coming from web client to web server along with HTTP request are called as request parameters
Web container collects client submitted data and stores that in HttpServletRequest object as request parameters. As a developer, you can collect that data from the request object as follows.

Case 1:
String name = request.getParameter("name");
String[] course = request.getParameterValues("course");

Case 2:
You can access the parameter names and values as Map
Map<String,String[]> map = request.getParameterMap();
Set pnames = map.keySet();
Iterator it = pnames.iterator();
while(it.hasNext()){
String parameterName = (String)it.next();
Object val = map.get(parameterName);
String[] values = (String[])val;
for(String value: values){
System.out.println(value);
}
}

Case 3:
To access only request parameters
Enumeration<String> ens = request.getParameterNames();
List<String> list = Collections.list(ens);
for(String pn : list){
String pv = request.getParameter(pn);
System.out.println(pn+" "+pv);
}

The container is storing multiple values for the same key in the map in the form of a String array.
Map<String,String[]> map = ...;
String course[] = new String[2];
course[0]="Java";
course[1]="JDBC";
map.put("course",course);

ServletConfig Parameters
  • ServletConfig is an interface available in javax.servlet package and web container vendor are responsible to provide the subclass for this interface.
  • Every servlet will have its own ServletConfig object and can not be shared.
  • When you want to use any data which is common for all the users but specific to a particular servlet that data can be specified as config parameters or init parameters.
With Servlet 2.x: Specify the config parameters in web.xml
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.mak.servlets.HelloServlet</servlet-class>
<init-param>
<param-name>email</param-name>
<param-value>mak@gmail.com</param-value>
</init-param>
</servlet>
With servlet 3.x: specify the config parameters in servlet class with annotations
@WebServlet(name="helloServlet",urlPatterns={"hello.do"},
initParams={@WebInitParam(name="email",value="mak@gmail")})
public class HelloServlet extends HttpServlet{}

Web container collects data from either web.xml or annotation and stores that in ServletConfig object as config parameters. As a developer, you can collect that data from the config object as follows.
String email = config.getInitParameter("email");
You can override the following method in the servlet class to acess the ServletConfig
public void init(ServletConfig cfg)

You can use the following inheritance method from HttpServlet
public ServletConfig getServletConfig()
Here when HttpServlet class init() implementation will be invoked(from init() method) then the config object will be returned otherwise null will be returned.

Assume that HttpServlet class is implemented as follows
public abstract class HttpServlet...{
private ServletConfig config;
public void init(ServletConfig config){
this.config=config;
}
public ServletConfig getServletConfig(){}
...
}

Case 1:
class AServlet extends HttpServlet{
protected void service(...){
ServletConfig cfg = getServletConfig(); // inherited
//return the config object
//Since init() method from HttpServlet will be called and config will be initialized.
}
}

Case 2 :
class BServlet extends HttpServlet{
public void init(ServletConfig cfg){
super.init(cfg);    // invoking the HttpServlet init() method
}
protected void service(...){
ServletConfig cfg = getServletConfig();
//returns the config object
// Since init() method from your class will be called and you are calling HttpServlet init() so config will be initialized.
}
}

Case 3:
class CServlet extends HttpServlet{
public void init(ServletConfig cfg){
}
protected void service(...){
ServletConfig cfg = getServletConfig(); // inherited
//returns null value
// Since init() method from your class will be called and you are not calling HttpServlet init() so config won't be initialized.
}
}

ServletContext Parameters
  • ServletContex is an interface available in javax.servlet package and container vendor is responsible to provide the subclass for this interface.
  • One web application will have only one ServletContext object i.e ServletContext object can be shared with all the servlets running in the container.
  • When you want to use any data which is common for all the users and common to all the servlets then data can be specified as context parameters in the web.xml as
  • follows.
web.xml
<context-param>
<param-name>website</param-name>
<param-value>www.google.com</param-value>
</context-param>

Web container collects data from web.xml and stores that in ServletContext object as context parameters. As a developer, you can collect that data from the context object as follows:
String web = context.getInitParameter("website");
You can use the following method with ServletConfig or ServletContext object to access the corresponding parameter names.
Enumeration<String> ens = sc.getInitParameterNames();
List<String> list = Collections.list(ens);
for(String pn: list){
String pv = sc.getInitParameter(pn);
System.out.printl(pv);
}