What happens when web server will start

What happens when Web Server will start

  • When Web Server starts then Web Container will be started
After Web Container Start Up following tasks will be happen:
  • Web Container reads the data from web.xml file and stores the data in main memory.
  • Container create the ServletContext object and initializes ServletContext object with the context parameters specified in web.xml
  • Thread pool will be created by the Container
  • All the Listeners will be initialized
  • All the Filters will be initialized
  • Container checks whether any servlet is configured with <load-on-startup> tag then that servlet will be initialized.
When Container is initializing the Servlet at Container start-up, Container will do the following tasks:
  • Servlet class will loaded.
  • Servlet instance will be created by calling default constructor.
  • Container will caste the instance into javax.servlet.Servlet type to ensure it is a servlet.
  • Container creates the ServletConfig object and initializes ServletConfig object with the config parameters specified in web.xml.
  • Container call the init() method by passing ServletConfig object as parameter.

What happens when Web Server shutdown

What happens when Web Server shutdown

When Web Service shutdowns then following tasks will be happened:

  • ServletContext object will be destroyed.
  • Thread pool will destroyed.
  • Container will destroy all the Servlet by invoking the destroy() method.
  • Container will destroy all the Filter by invoking the destroy() method.
  • Container will destroy all Listener.
  • All other object running inside the Container will be destroyed and finally Container is down and unable to process the user request.

Difference between forward() of Request Dispatcher and sendRedirect() of Servlet Response



forward() of RequestDispatcher
sendRedirect of ServletResponse
Using forward() method we can send the control from one web component to another web component which are in same application.
Using sendRedirect() method we can send the control from one web component of one application to another web component of same or another web appplication
When we are forwarding control using forward() method then we send the data as an request attribute. 
When we are forwarding control using sendRedirect() method we can send the data as query string.
Forwarding is happening completely at server side and client can not observe that.
Redirecting is happening at server side and client side and client can observe  the change in the url

Request Dispatcher - forward() and include() method

RequestDispatcher: 
  • RequestDispatcher is an interface in javax.servlet package. The subclass is implemented by container vendor.
  • It has two methods: forward() and include().
 
 1.) forward() method 
  • forward() method is used to forward the request from:
         --Servlet to HTML
         --Servlet to JSP
         --Servlet to Servlet
         --JSP to HTML
         --JSP to JSP and 
         -- JSP to Servlet
  • We can use many forward() methods in one servlet conditionally to forward the control to other resource but only one forward will be executed.
       RequestDistpatcher rd= null;
       rd = request.getReqeustDispatcher("hello.jsp");
       rd.forward(request, response);
  • When we have other statement after the forward() method then those statement will be executed but we can't write any content in the response stream.
2.) include() method: 
  • include() method is used to include the response of servlet or jsp in another servlet or jsp.
  • We can place many include() methods in one servlet to include many resources and all include methods will be executed. 



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");