Servlet Parameter -Servlet Request, Servlet Config and Servlet Context

Servlet Parameter

  • Servlet parameter is name value pair.
  • Parameter name and value are the type of String.
  • 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 type of parameter in servlet: 

ServletRequest Parameter: 
  • Client submitted data which is coming from web client to web server along with Http Request are called as Request Parameter.
  • Web container collects client submitted data and stores that in HttpServletRequest object as Request Parameter.
    Example:
    String userName = request.getParameter("username");
    String[] val = request.getParameterValues("city");

ServletConfig Parameter: 
  • ServletConfig is an interface available in javax.servlet package and web container vendor is responsible to provide the subclass for this interface.
  • Every servlet will have its own ServletConfig object and can not be shared.
  • When we want to use data which is comming for all the users but specific to particular servlet, that data can be specified as config parameters or init parameter.
      ServletConfig parameter specify in web.xml file as below:
       
        <servlet>
          <servlet-name>myServlet</servlet-name>
         <servle-class>
            com.javaforecast4u.servlet.MyServlet
         </servlet-class>
         <init-param>
         <param-name>mobileNo</param-name>
         <param-value>89898989</param-value>
          </init-param>
       </servlet>
  • Web container collects data from web.xml and stores that in SservletConfig object as config parameter.
  • We can collect the data
        String paramValue = config.getInitParameter("mobileNo");
  • We have to override the init method in servlet class to access the ServletConfig parameter
        public void init(ServletConfig sc); 

ServletContext Parameter: 
  • ServletContext 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 can be shared with all the Servlet running in the container.
  • When we want to use any data which is common for all the users and common to all the Servlet then data can be specified as context parameters in web.xml as below:
      <context-param>
      <param-name>javatutorial</param-name>
      <param-value>javaforecast4u.com</param-value>
      </context-param>
  • Web container collects data from web.xml and stores that is ServletContext object as context parameter.
           String tutorial = context.getInitParameter("javatutorial");

2 comments :


  1. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    iOS Training in Chennai
    Android Training in Chennai
    php Training in Chennai

    ReplyDelete