Top servlet Interview Questions Part 2 - Tech Interview Club

Latest

interview questions for java,spring,servlet,jsp,hibernate,webservices,.net,html,javascript,andriod,jquery

Comments

Saturday, 24 June 2017

Top servlet Interview Questions Part 2

1.What is Session Tracking
Session simply means particular period of time.
Session tracking is the way to maintain state of user or client across the multiple requests during the session so we need to maintain state using session tracking techniques.
All web applications are stateless because they are using stateless protocol Http protocol.Http protocol is a stateless so we need to maintain state using session tracking techniques. Each time user requests to the server, server treats the request as the new request. So we need to maintain the state of a user to recognize to particular user. HTTP is stateless that means each request is considered as the new request.

There are four techniques used in Session tracking:
1.    Hidden Form Field
2.    Cookies
3.    URL Rewriting
4.    HttpSession

2.Why Http is designed as stateless protocol?
 If http is statefull protocol for multiple requests are given by client to web application.single connection will be used between browser and webserver across the multiple requests.this may make clients to engage connections with webserver for long time even though the connections are idle.due to this webserver may reach to maximum connections even though most of the connection are idle .
To overcome that problem http is given as stateless protocol so no client can engage connection with webserver for longtime .more over the connection will be closed automatically at the end of each request related response generation .In internet environment there is a chance to having huge amount of clients for each website it is recommended to have stateless behavior for http.
State full web application:
If web application is capable of remembering a client data during a session across the multiple request then that web application is called as state full web application.
In statefull web application the web resource program can use the data of previous request while processing current request that means while processing request2 it can use request1 data..
Even though http is stateless protocol we can make our web application as statefull application for this we need to use session tracking .

3.What is Hidden Form Field?
In case of Hidden Form Field a hidden (invisible) text field is used for maintaining the state of a user.
In such case, we store the information in the hidden field and get it from another servlet. This approach is better if we have to submit form in all the pages and we don't want to depend on the browser.
<input type="hidden" name="uname" value="Vimal Jaiswal"

Advantages:

1.    It will always work whether cookie is disabled or not.
2.    The basic html knowledge is enough.
3.    This can be used along with all kind of server side technologies and supports all kind of web servers, application servers.
4.    All browsers support this technique.

Disadvantage:

1.    Extra form submission is required on each page.
2.    Only textual information can be used.it does not hold java objects.
3.    It not provides data secrecy we can view data using view page option of browser.
4.    Hidden field data travels over the network along with request and response so network traffic will increase.
5.    While creating each dynamic form page we need to add the previous form pages data as hidden box values this is burden to the programmer.

index.html
<form action="servlet1">  
Name :< input type="text" name="username"/><br/>  
<input type="submit" value="go"/>  
</form>  
FirstServlet.java
Import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
  
public class FirstServlet extends HttpServlet {  
public void doGet (HttpServletRequest request, HttpServletResponse response) {  
        try {  
  
        response.setContentType ("text/html");  
       PrintWriter out = response.getWriter ();  
          
        String n=request.getParameter ("username");  
        out.print("Welcome "+n);  
          
     //creating form that have invisible textfield  
      out.print ("<form action='servlet2'>");  
      out.print ("<input type='hidden' name='uname' value='"+n+"'>");  
       out.print ("<input type='submit' value='go'>");  
       out.print ("</form>");  
        out.close ();  
  
                } catch (Exception e) {System.out.println (e) ;}  
    }  
  
}  
SecondServlet.java
Import java.io.*;  
Import javax.servlet.*;  
Import javax.servlet.http.*;  

Public class SecondServlet extends HttpServlet {  
Public void doGet (HttpServletRequest request, HttpServletResponse response)  
        try {  
        response.setContentType ("text/html");  
        PrintWriter out = response.getWriter ();  
          
        //Getting the value from the hidden field  
        String n=request.getParameter ("uname");  
        out.print ("Hello "+n);  
  
        out.close ();  
                } catch (Exception e){System.out.println(e);}  
    }  
}  
web.xml
<Web-app>  
  
<Servlet>  
<servlet-name>s1</servlet-name>  
<servlet-class>FirstServlet</servlet-class>  
</servlet>  
  
<Servlet-mapping>  
<servlet-name>s1</servlet-name>  
<url-pattern>/servlet1</url-pattern>  
</servlet-mapping>  
  
<Servlet>  
<servlet-name>s2</servlet-name>  
<servlet-class>SecondServlet</servlet-class>  
</servlet>  
  
<Servlet-mapping>  
<servlet-name>s2</servlet-name>  
<url-pattern>/servlet2</url-pattern>  
</servlet-mapping>  
  
</web-app>  


19.What is the difference between GenericServlet and HttpServlet?

GenericServlet
HttpServlet
Can be used with any protocol (means, can handle any protocol). Protocol independent.
Should be used with HTTP protocol only (can handle HTTP specific protocols) .
Protocol dependent.
All methods are concrete except service() method. service() method is abstract method.
All methods are concrete (non-abstract). service() is non-abstract method.
service() should be overridden being abstract in super interface.
service() method need not be overridden.
It is a must to use service() method as it is a callback method.
Being service() is non-abstract, it can be replaced by doGet() or doPost() methods.
Extends Object and implements interfaces Servlet, ServletConfig and Serializable.
Extends GenericServlet and implements interface Serializable
Direct subclass of Servet interface.
Direct subclass of GenericServlet.
Defined javax.servlet package.
Defined javax.servlet.http package.
All the classes and interfaces belonging to javax.servlet package are protocol independent.
All the classes and interfaces present in javax.servlet.
http package are protocol dependent (specific to HTTP).
Not used now-a-days.
Used always.
In GenericServlet you cannot use Cookies or HttpSession, Session tracking is not
These all are possible in HttpServlet.
Similarities :
1. One common feature is both the classes are abstract classes.
2. Used with servlets only
20.What is webserver and web container?
Web Server or HTTP Server: a server which is capable of handling HTTP request send by a client and respond back with a HTTP response.
Web Container or Servlet Container or Servlet Engine: is used to manage the components like servlets,
jsp .It is a part of the web server.
Client send a http request to the web server now web server generates dynamic web pages with the help of servlet but web server not communicate with servlet directly.it must take the help of web container.so web server sends http request to web container but servlet only knows object not http request so web container creates the valid http request and http response object for servlet and creates thread for each request of that servlet. Web container manages the life cycle of the servlet and it is the responsible for invoking the methods in servlet. Because servlet is special java program that has no main method it has some call back methods and container knows which servlet  to call and which  call back method to call on servlet .after serves the request of client container send the response to web server and web server send http response to client.
Webserver:
1.       Listen client requests continuously (http request) (once web server is started one demon thread will be started to listen to client’s requests continuously and traps and takes the client generated http requests.
2.       Traps and takes client generated requests.
3.       Passes the http request to an appropriate web resource program of web application.
4.       Provide the container software to execute server side programs.
5.       Gather out put generated by web resource programs.
6.       Pass output of web resource program to browser window as http response in the form of web programs.
7.       Provide the deployment and un deployment of web application.
8.       Provide the support to middleware services.
9.       Container is software or software application that can manage the whole lifecycle of given resources.
10.   Servlet container takes care of servlet program lifecycle.
11.   Jsp container takes care of jsp program lifecycle.
12.   Applet (applet viewer) container takes care of applet program lifecycle.
13.   Servlet, jsp containers are part of webserver.
21.Who will create Servlet Object?
 Webserver or Application server.
Creating servlet object ,managing servlet object by calling life cycle methods, processing request & destroying servlet object is the responsibility of the underlying “web-server/application server”.
Programmer just writes the logic he wants to execute by taking the support of life cycle methods.
22.What are common tasks performed by Servlet Container?
Servlet containers are also known as web container, for example Tomcat. Some of the important tasks of servlet container are:
Communication Support: Servlet Container provides easy way of communication between web client (Browsers) and the servlets and JSPs. Because of container, we don’t need to build a server socket to listen for any request from web client, parse the request and generate response. All these important and complex tasks are done by container and all we need to focus is on business logic for the applications.
Lifecycle and Resource Management: Servlet Container takes care of managing the life cycle of servlet. From the loading of servlets into memory, initializing servlets, invoking servlet methods and to destroy them and making servlet instances eligible for garbage collection. . Container also provides utility like JNDI for resource pooling and management.
Multithreading Support: Container creates new thread for every request to the servlet and provide them request and response objects to process and finishing it when the Servlet service() method is over.
 So servlets are not initialized for each request and saves time and memory.
JSP Support: JSPs doesn’t look like normal java classes but every JSP in the application is compiled by container and converted to Servlet and then container manages them like other servlets.
Miscellaneous Task: Servlet containers manages the resource pool, perform memory optimizations, execute garbage collector, provides security configurations, support for multiple applications, hot deployment and several other tasks behind the scene that makes a developer life easier. Managing of deployment descriptor web.xml file.
23.What happens when our servlet gets first request from client?
1.       Servelet container loads our servlet class from web-inf /class folder of deployed web application.
2.       Servlet container instatiate(Object creation) our servlet class object as class.forName(“ser1”).newInstance();
3.       Class.forName(“serv1”)-àloads our servlet class
4.       newInstance() Ã creates object fro loaded class.
5.       During the instantiation process the Zero param constructor of our servlet clkass will executes
6.       Servlet container creates the one servlet config object for our servlet class object.
7.       Servlet container calls init() life cycle method having servletconfig object as argument value on our servlet class object.
8.       Step1 to step5 complete the servlet intantiate and initialization process.
9.       Servlet container calls the next life cycle method service() on our servlet class object this will process the request and generates the response and send that into browser.

24.What happens when our servlet gets other than first request?
Servlet container checks the availability of our servlet class object.
1.if available, servlet container calss service() method on exixting object of our servlet class to process the request.
2.if not available, servlet container perform all operation of first request.
25.What is difference between webserver and application server?

Webserver                                                                                  Application server
1.allows to deploy and execute web applications.                      1.allows to deploy and execute           web
Applications,ejbcomponent,enterprise
                                                                                                                    Applications and resource adopter apps
2.developed based on servlet and jsp api                                 2.developed based on all jeeapi specification
           Specifications.                                                                                       (likeservlet,jsp,ejb,jmsetc)
3.gives servlet and jsp containers                                              3.gives jsp,servlet and ejb container
4.does not allow to create adomain                                          4.allows to create domains.
5.allows only http protocol requests                                         5.allows both http and non http protocol
Based requests.
6.gives minimum no of middleware services.                          6.gives more no of middleware services.
7.suitable for small scale and medium scale apps                    7.suitable for large scale web apps and
Jee applications.
8.recognize .war file as applications                                        8.recognize .war,.ear,.jar,.rar file as application
9.ex:jws,tomcat,resin and etc                                                                                    9.ex.weblogic,jboss,websphere,glashfish etc.
26.What are the uses of Servlet?
Typical uses for HTTP Servlets include:
Ø  Processing and/or storing data submitted by an HTML form.
Ø  Providing dynamic content, to the client e.g. returning the results of a database query to the client.
Ø  A Servlet can handle multiple request concurrently and be used to develop high performance system
Ø  Managing state information on top of the stateless HTTP, e.g. for an online shopping cart system which manages shopping carts for many concurrent customers and maps every request to the right customer.map requests to Servlets.
                          
27.When the Servlet is unloaded?
The web-container destroys servlet object when one of the following situations occur.
Ø  When you shutdown the server
Ø  When server is crashed
Ø  When you stop the running web-application.
Ø  When web-application is reloaded.
Ø  When web-application is un deployed
Ø  When garbage collector destroys the servlet object.

28.When servlet object will be created?
The web-container creates object for a servlet when one of the following situations occur
Ø  When servlet gets first request from the browser.
Ø  For first request given to servlet after restarting the server.
Ø  For first request given to servlet after restarting the web-application.
Ø  For first request given to servlet after reloading the web-application.
Ø  During server startup or when web-application is deployed.
When<load-on-startup> is enabled.
29.Why HttpServlet class is declared abstract?
abstract class contains both abstract methods and concrete methods. if class contains there is no abstract method also we can declare the class as abstract class.
so we can declare Httpservlet as abstract class
HttpServlet class is abstract even though none of the 7 doXXX() methods with in it are abstract.
The reason is,if all 7 doXXX() methods are defined as abstract,then every developer who creates a child class for it, has to provide implementations for all the 7 methods,which is an quite an issue
The HttpServlet class is declared abstract because the default implementations of the main service methods do nothing and must be overridden. This is a convenience implementation of the Servlet interface, which means that developers do not need to implement all service methods. If your servlet is required to handle doGet() requests for example, there is no need to write a doPost() method too.
If you extend the class without overriding any methods, you will get a useless servlet; i.e. one that gives an error response for all requests. Similarly, if the class was not abstract, then any direct instance of HttpServlet would be useless.so prvent the creation of instance of http servlet they are used as abstract.



A subclass of HttpServlet must override at least one methodusually one of these:
1.       doGetif the servlet supports HTTP GET requests
2.         doPostfor HTTP POST requests 
3.       doPutfor HTTP PUT requests
4.         doDeletefor HTTP DELETE requests
5.        init and destroyto manage resources that are held for the life of the servlet
6.        getServletInfowhich the servlet uses to provide information about itself





30 waht is Servlet Request Object?

An object of the servlet Request is used to provide the client information to a servlet such as content Type, content length, parameter names, parameter values, header information, attributes.
Servlet API provides two important interfaces.
1. javax.servlet. ServletRequest
2. javax.servlet. HttpServletRequest
Httpservlet has the method that is related to http protocol.

31.What is Servlet Response?

The object of the servlet response is used to send the response from servlet to client.
It has two important interfaces
1. ServletResponse.
2. HttpServletResponse.

 32. When request & response objects will be created in servlet?
 For every new request coming to servlet a separate request & response objects will be created by “web server” before executing the service() method these two objects are visible in the service() method once request related response goes to browser request, response objects of that HttpServlet will be destroyed.


33.What is Servlet Chaining?
                  Taking the request from the browser window and process that request by using multiple servlets as a chain is called servlet chaining. The request from the client browser is sent to the first servlet in the chain. The response from the last servlet in the chain is returned to the browser. In between, the output from each servlet is passed (piped) as input to the next servlet, so each servlet in the chain has the option to change or extend the content.
                     All Servlet Programs that are participated in the servlet chaining will be used same request and response object because they process the same request that is given by client.
                     To perform servlet chaining we need Request Dispatcher object. Request Dispatcher object means it is the object of the container supplied java class implementing the javax.servlet.RequestDispatcher interface.
                      There are two types of mode of chaining.
                     1. Forwarding request mode chaining
                     2. Include response mode chaining.
If ser1,ser2,ser3,ser4 servlet programs are there in forwarding request mode of chaining  then the output of ser1,ser2,ser3 programs will be discarded and only output of the ser4 server programs goes to browser.
If ser1, ser2, ser3, ser4 servlet programs are there in includeresponse mode of chaining then the output of all servlet programs together goes to browser window as response
Request Dispatcher:
                Request Dispatcher Interface implemented by servlet container.to provide the facility of dispatching the request to anther resource it may be html,servlet or jsp.this interface can also be used to include the content of other resource also.it is the one of the way of servlet collaboration(communication).There are two methods in this interface.
    1. Forward
    2. Include
To dispatch the request from Servlet or JSP to web resource using Request Dispatcher we need to perform following steps:
            Get a Request Dispatcher object reference
             Using include () and forward () methods of Request Dispatcher.
There are three ways to create the request dispatcher object.
1.       The getRequestDispacther () method of Servlet Request.
This method obtains the Request Dispatcher object using path to the current request. The Servlet container builds complete path and locates the resource provided in the

getRequestDispacther () method of Servlet Context
Request Dispatcher rd = request.getRequestDispatcher("/home.jsp");
                rd. forward(request, response);
                     or
rd. include (request, response);



2. The getNamedDispacher () method of ServletContext.

This method takes String argument used to locate Servlet to which request is to be dispatched. When this method is called, the container locates the Servlet with given name in the context.

ServletContextsc=getServletContext ();
RequestDispatcherrd = sc.getNamedDispatcher("ser1");
      //ser1 is the logical name of the servlet that are given in web.xml.
             rd. forward(request, response);
                     or
rd. Include(request, response);


3. The getRequestDispatcher () method of ServletContext.

This method takes String argument to locate the resource to which request is to be dispatched. When this method is called, the container locates given path. Path should start with the / character. If given path does not start with / character it throws IllegalArgumentException
ServletContextsc=getServletContext ();
RequestDispatcherrd = sc.getRequestDispatcher("/home.jsp");
              rd. forward(request, response);
                         or
rd. include (request, response);

34. What is the difference between the getRequestDispatcher () and getNamedDispacher ()?

GetRequestDispatcher () 
getNamedDispacher ()
1. Invockable on both ServletRequest and ServletContext  
1.Invockable in only ServletContext.
2.Expect the URL pattern of the destination servlet   Or file name of the destination jsp or html as argument 
2.Expect logical name of the destination servlet or jsp program as argument

3.This method generated RequestDispatcher object can Point to the destination servlet or jsp or html file
3.this method generated Request                      Dispatcher object only points     Destination servlet or jsp file 


35.What is the difference between the request dispatcher object that is created based on servlet Context object and Request object?

1.       The Request Dispatcher objects that is created based on request objects that expects source servlet program and destination servlet or resource programs within the same web application.
In request.getRequestDispatcher (path) in order to create it we need to give the relative path of the resource.
2.       The Request Dispatcher objects that is created based on Servlet Context objects it allows to keep source servlet program and destination servlet or resource programs either in same web application or two different web applications of same server. But it is not possible if two different web applications in two different servers.
In context.getRequestDispatcher (path) in order to create it we need to give the absolute path of the resource.




Ex:
Web One
             Input.html


<form action=”svr1” method =”get”>//svr 1 URL pattern of appication1 servlet program

<input type=”text” name=”t1”/>
<input type=”submit” value=”submit”/>

</form>

Svr1.java

                Public class Svr1 extends  HttpServlet{
                  Public void service(HttpServletRequestreq,HttpServletResponse res)throws                          ServletException,IOException{

PrintWriter pw=res.getWriter();
res.setContentType(“text/html”);
int n=Integer.parseInt(req.getParameter(“t1”));
if(n>100){
pw.write(“svr1 programs n=”+n);
                                       }
else{
ServletContextsc=getServletContext();
ServletContext sc1=sc.getServletContext(“webTwo”);
RequestDispatcherrd=sc1.getRequestDispatcher(“/svr2”);
Rd.forward(req,res);
                           }
                       }
                   }




Web Two
           Svr2.java

                Public class Svr2 extends  HttpServlet{
                  Public void service(HttpServletRequestreq,HttpServletResponse res)throws                          ServletException,IOException{

PrintWriter pw=res.getWriter();
res.setContentType(“text/html”);
int n=Integer.parseInt(req.getParameter(“t1”));
pw.write(“svr2 programs n=”+n);

}
}
//(/svr2) is url pattern given in web.xml file

                      Most of the servers getcontext() method of javax.servlet.ServletContext interface not implemented so when method is called in server we get nullPointer Exception due to this request dispatching between two servlets of two differnet application is not supported in most of the servers(like weblogic10.x,tomcat,glassfish all versions jboss some versions).it is possible in weblogic9.x and 8.x.So RequestDispatcher object based severlet chaining is good only when two servlet programs are in same web applications. in remaining situations saendredirect is recommended .

36.What is include and forward methods?

Forward ():
1.Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
2.Forward() method is there to perform forwarding request mode of servlet chaining.
3.Both servlet1 and servlet2 programs will be use same request and response objects so the request data coming to servlet1 are visible and accessible in servlet2 program also.
4.All the statements placed in servlet1 program before and after forward() method will be executed but entire output generated by servlet1 will be discarded.
5.Both servlet’s can be there in the same web application or different web applications of same server

6.client send request first servlet and first servlet forward the request second servlet most important things is second servlet give a response to client.
7.It is very useful to configure the error servlet to other  main servlet programs of web application


public void forward(ServletRequestrequest,ServletResponse response)throws ServletException,java.io.IOException


RequestDispatcherrs = request.getRequestDispatcher("hello.html");
rs.forward(request,response);



Include():
1.Includes the content of a resource (servlet, JSP page, or HTML file) in the response.
2.clinet send request first servlet and first servlet forward the request second servlet but second servlet generate the response then second servlet return the response to frist servlet then frist servlet given a response to clinet.
3.include() method is there to perform include response mode of servlet chaining.
4.Both servlet1 and servlet2 programs will be use same request and response objects so the request data coming to servlet1 are visible and accessible in servlet2 program also.To pass additional data between servlet1 and serrvlet2 using request attributes.
5.response of second servlet is sent to the client. Response of the first servlet is not displayed to the user.
6.Both servlet’s can be there in the same web application or different web applications of same server
  7.Include the header and footer of web page for every page using include method.
public void include(ServletRequestrequest,ServletResponse response)throws ServletException,java.io.IOException
RequestDispatcherrs = request.getRequestDispatcher("hello.html");
rs.include(request,response);

Example:
Index.html
<form method="post" action="Validate">
Name:<input type="text" name="user" /><br/>
Password:<input type="password" name="pass" ><br/>
<input type="submit" value="submit">
</form>



Validate.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Validateextends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
     PrintWriter out = response.getWriter();
try {
         String name = request.getParameter("user");
        String password = request.getParameter("pass");

if(password.equals("studytonight"))
        {
RequestDispatcherrd = request.getRequestDispatcher("Welcome");
rd.forward(request, response);
        }
else
         {
out.println("<font color='red'><b>You have entered incorrect password</b></font>");
RequestDispatcherrd = request.getRequestDispatcher("index.html");
rd.include(request, response);
            }
}finally {           
out.close();
        }

    }
}

Welcome.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Welcomeextends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
try {

out.println("<h2>Welcome user</h2>");
        } finally {           
out.close();
        }
    }
}
Web.xml
<web-app>
<servlet>
<servlet-name>Validate</servlet-name>
<servlet-class>Validate</servlet-class>
</servlet>
<servlet>
<servlet-name>Welcome</servlet-name>
<servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Validate</servlet-name>
<url-pattern>/Validate</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Welcome</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

37.Error servlet?
The servlet program that executes only when exception raised in other servlet program is called error servlet. This servlet is useful to display the exception related details as non technical guiding messages in web browser window when exception raised in other servlet of web application.


38.Difference between include and forward methods?
Let us make a table of differences. In the table, I name two servlets, as seen in the above examples, S1 and S2S1 is the servlet which calls S2. Or to say, S1 is including servlet and S2 is included servlet.
Property
include() Method
forward() Method
What can be done?
Includes another file in our current file.
Will forward the client request to the forwarding page
Merge of response
Response of S1 and S2 are merged and sent to client (as if a single response). This way,
the Programmer can achieve "server side includes".
No merge of response. Only S2 response will go to the client
Retaining execution control
Shifted temporarily from S1 to S2. It works like a general simple method call.
Shifted permanently from S1 to S2.
Control coming back
Execution control comes back to S1 after executing S2 for further processing of S1
after include() statement from where the execution control shifted.
Once shifted, the control never returns to S1. It is permanent shifting.
            Response placement
Response of S2 is placed in S1.
Response of S2 is not placed in S2.
Client receives
Response of S1 and S2 is received by client.
Only response of S2 is received by client.
Control returned
After executing S2, control returned to S1.
After executing S2, control returned to client.
Extra activity
Once control is returned to S1 from S2, any activity can be done on the server like calling another servlet with another RequestDispatcher object.
Once control returned to client, no activity can be done on S1 or S2.
Usage
Used by Programmer when the output of both servlets S1 and S2 is required.
Used only S2 response is required.
Speed of delivery to client
Comparatively slower.
Faster.
Access
S2 has access to the request and response objects of S1, but limitations exist. S2 cannot set headers and also cannot call any other method like setCookie etc. affecting the response headers. That is, S2 cannot attempt to change the HTTP headers or response status code etc. and performing any activities like this is simply ignored.
Here also S2 cannot alter as response is delivered on S1 URL.
Used when
Used when static information is to be included.
Used when dynamic information is to be included. It is can be used where a Servlet can play the role of a controller to process the client input and deciding what response page is to be returned.
Client response
Client receives the response from the same servlet which he requested.
Client actually receives the response from a different servlet (not known to client).
Treatment of processing
The processing S2 can be treated as part of S2 processing.
The processing of S2 can be treated completely as a different entity. It is used to show a different resource on the server by S1.
 Similarities:
1.    Both are methods of RequestDispatcher interface.
2.   Both are method calls.
3.   Both are abstract methods in the interface but can be treated as concrete methods in Servlet programming. I mean, because we are not implementing RequestDispatcher to our Servlet program, there is no compulsion to the Programmer to override both.
4.   Processing done on the server when both methods are called is completely transparent (not visible or known) to client.
Both are not treated as redirection

No comments:

Post a Comment