Thursday, 29 March 2012

Servlet Chaining Demos

To print Hello with name 


Step 1- Design JSP page

Step 2Make another jsp page to get the parameters (using forward Request Dispatcher)
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
<% String st=request.getParameter("txtName");
        if(st=="")
       {
            getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
       }
        else
       {
            getServletContext().getRequestDispatcher("/welcome.jsp").forward(request,response);
       }
%>
    </body>
</html> 



Step 3- Make another demo so that hello comes in front of the name you write in textfield
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       Hello---> <%=request.getParameter("txtName")%>
    </body>
</html>



Demo 2- To display image with text on the image as well as below the image with the help of servlet chaining.

Step 1- Design the first jsp page.


Step2- Design second jsp page in which image is to be displayed and store the image in images folder.
<html>
    <head>
        <title>Dispatcher Sample</title>
    </head>
    <body>
        <div align="center">
            <br><br>          
            <table width="800" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">
                <!-- Title row [Begin] -->
                <tr>
                    <td colspan="2" height="500" background="images/faculty.jpg" align="center" valign="middle">
                        <big><big><big>
                                    <div style="color:white;"> <b>Hello World</b></div>
                        </big></big></big>
                    </td>
                </tr>
                <!-- Title row [End] -->

                <tr>
                    <!-- Left Image [Begin] -->
                    <td width="600" background="images/main.jpg" align="center" valign="top">&nbsp;</td>                                   
                    <!-- Left Image [End] -->
                    <td>
                        <table border="0" width="100%">  
                            <!-- Content Row [Begin] -->
                            <!-- Content row [End] -->
                        </table>
                    </td>
                </tr>
               
                <!-- Footer row [Begin] -->
                <tr>
                    <td width="60">&nbsp;</td>
                    <td  bgcolor="#FFFFFF" align="center" valign="middle">
                        <table border="0">
                            <tr>
                                <td colspan="2" valign="middle" align="left">
                                    <div style="color: gray;">
                                       <small>Copyright&copy;http://nancy@techdazzler.com</small>
                                    </div>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                <!-- Footer row [End] -->
            </table>
        </div>
    </body>
</html>





Step 3- Make  a servlet to print hello name which we will be giving in text fied as shown

Step 4- Click on submit and output is ….






Demo 3- Use of Request Dispatcher (using include)

Step1- Deign JSP page1 as login application and css is used here





Step 2- Put  values




Step 3- Click on submit and  another jsp page is opened.
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
       
    </head>
    <body>
        <h1 align="center">LOGIN DETAILS</h1>
        <form action="index3.jsp">
            <table border="3" align="center">
                <% String Name=request.getParameter("txtName");
                String password= request.getParameter("txtPass");
                if(Name=="" && password=="" )
       {
                    getServletContext().getRequestDispatcher("/index3.jsp").include(request, response);
        }
                %>     
                <tbody>
                    <tr>
                        <td>Name</td>
                    <td><input type="text" name="txtName" value="<%=Name %>" readonly="readonly" /></td></tr>
                    <tr><td>Place</td>
                        <td><input type="text" name="txtPlace" value="" /></td></tr>
                    <tr>
                        <td>Age</td>
                        <td><input type="text" name="txtAge" value="" /></td></tr>
                <tr><td>Qualification</td>
                        <td><input type="text" name="txtQua" value="" /></td>
                    </tr>
                    <tr>
                        <td colspan="4" align="center">
                            <input type="submit" value="Submit" />
                        </td>
                    </tr>
                </tbody>
            </table>

        </form>
    </body>
</html>


Step 4- Fill information




Step 5- Click on submit and see the information on next page








Step 6-  Check that the information is travelling from 1 pg to another now fill again the leftover records



Step 7- Click on add and you will get the final  result

sendRedirect
The method response.sendRedirect() is used to dispatch the request for another servlet execution in servlet chaining. In this process, client or browser are involved in request dispatching. Firstly, servlet send response along with send redirect URL to the client and client again send that request to the server back to execute another servlet in servlet chaining. Here, all servets may or may not execute in same server or they may be executed differently  and client is  plays an important role in dispatching thus  servlet chaining is slow. In this process we can communicate with multiple servers.

HttpServletResponse           response;
response.setRedirect(/servlet.jsp);

Advantage:
servets may or may not execute in same server or they may be executed differently  and client is involved in dispatching.


Limitation:
Here, as first send Redirect() goes to client then to url to execute another servlet, so first servlet respond to client and client again redirects the URL to execte the servlet.Thus it takes more time to redirect the request URL for the servlet chaining.











Servlet Chaining


 Servlet Chaining

This topic has been included in J2EE(Java to Enterprise Edition i.e Advance Java) Here as discussed earlier, output of one servlet act as a input to another servlet and at last request is sent to the browser and final output is achieved. Servlet allows us to invoke more than one servlet in sequence when the URL is opened with a common servlet.the process continues as follows-



·        Two Ways to Chain Servlets

In Servlets/JSPs, there are two ways to achieve servlet chaining using


RequestDispatcher:

RequestDispatcher is used to dispatch the request URL to execute another servlet in servlet chaining. Process goes on this way that first servlet directly dispatch the request to execute another servlet by dispatching URL. There are two methods:
request.include()  and request.forward()
The above methods are used to dispatch the request for another servlet execution. In this process, client or browser not involved in request dispatching..Here, all servers are executing in same server and client is not participating in dispatching thus  servlet chaining is fast. In this process we cannot communicate with multiple servers. We can use either of the method.



RequestDispatcher rd=new RequestDispatcher();
rd.forward(/”index.jsp”);
or
RequestDispatcher rd=new RequestDispatcher();
rd.include(/”servletTwo.jsp”);


Diamgramatic-explanation:

Here, as shown request of servlet 1 is forward to Servlet 2 and further to Servlet 3. This way the process continues.
Advantage:
I.. Same url connection is used for communication between servlets.
ii. All servlets are executing in the same server.
iii. This servlet chaining process is faster than rendRedirect.
iv. Client is not involved in this process.

Limitation:
In this process we cannot send the request to other servlet that is executed in the other server i.e  limitation we cannot  communicate with multiple servers.



Include



Here, we are using include method i.e Servlet 2 is included in Servlet 1 and Servlet 3 is included in Servlet 2 and chaining goes in this manner.The output will remain same either method can be used



·        Is there any difference

output of one servlet act as a input to another servlet and at last request is sent to the browser and final output is achieved. This is called servlet chaining.


output of one JSp page act as a input to another JSP page and at last request is sent to the browser and final output is achieved.Is this also Servlet chaining.???
Yes, this is also called servlet chaining because jsp page gets converted into Servlet at last by servlet engine that’s the reason.For more clearance watch out the below demos…



























Servlet Mapping


Servlet Mapping

This mapping is specified with the help of servlet name in web.xml file, we can map servlet class with url pattern. In web.xml we have two child tags <servlet-name> and <url-pattern>.<servlet-name> specifies the name of the servlet, which is called for incoming URL pattern matching by giving values in<url-pattern> element Every<servlet> element  have two child elements - <servlet-name> and<servlet-class>. Servlet name and url pattern should be same as shown.







Definining Servlet Mapping



Location where web.xml is stored

Web xml is made in webapps àWEB-INF à web.xml(save here).
As per the folders shown above we can save our files accordingly like html file in META-INF and similarly jsp page as well as css files, images in images folder similarly scripts.Any program we want to make can be stored in classes the compiled from there.





Implicit  Mapping


Servet can contain JSP(.jsp) extensions we map jsp page to servlet with the help of servlet engine and jsp page is converted into servlet this is called Implicit Mapping. Servlet Mapping and Implicit Mapping are different as in servlet mapping we use url pattern for mapping of servlet whereas here in implicit mapping its automatic i.e jsp pg is converted into servlet with the help of servlet engine




Inter Servlet Communication



 Inter Servlet Communication


As the name comes in mind i.e Inter Servlet Communication , one can understand it as it is communication between two or more servlets. Servlets talk to each other. There are many ways by which Servlets can communicate, some of them are mentioned as follows:
§  Request Dispatching
§  HTTP Redirect
§  Servlet Chaining
§  HTTP request (using sockets or the URLConnection class)
§  Shared session, request, or application objects (beans)
§  Direct method invocation (deprecated)
§  Shared static or instance variables (deprecated)
Basically interServlet communication can be acheived through servlet channing..


As shown above, we are given 3 servlets , output of Servlet 1 act as input to servet 2 similarly , output of Servlet 2 act as input to Servlet 3 .This process is called Servlet Chaining.Many servets can communicate to each other using same URL. Servlet can use HTTP request rather than direct calls.One Servlet can call other servlet by sending an Http request by opening same url connection. The Output from the last Servlet is sent back to the browser. To understand in more better form below diagram is given. 

·         How do JSP page communicate with Server
Step1:- Design JSP page



Step2:-Select the items



Step3:-Click on Add To Cart and the values will get on servlet as shown



·        Camparison of JSP and Servlet on basis of Performance




JSP PAGE
SERVLET


Here, we can experience a delay
When JSP page is accessed first time.
Here, no delay is experienced


It undergoes a translation phase where
Jsp page is converted into servlet by
JSP engine.
Once, servlet is loaded in the memory
It follows the life cycle for request processing. 










                                   Jsp Page




Servlet



Servlet Mapping

This mapping is specified with the help of servlet name in web.xml file, we can map servlet class with url pattern. In web.xml we have two child tags <servlet-name> and <url-pattern>.<servlet-name> specifies the name of the servlet, which is called for incoming URL pattern matching by giving values in<url-pattern> element Every<servlet> element  have two child elements - <servlet-name> and<servlet-class>. Servlet name and url pattern should be same as shown.







Definining Servlet Mapping



Location where web.xml is stored

Web xml is made in webapps àWEB-INF à web.xml(save here).
As per the folders shown above we can save our files accordingly like html file in META-INF and similarly jsp page as well as css files, images in images folder similarly scripts.Any program we want to make can be stored in classes the compiled from there.





Implicit  Mapping


Servet can contain JSP(.jsp) extensions we map jsp page to servlet with the help of servlet engine and jsp page is converted into servlet this is called Implicit Mapping. Servlet Mapping and Implicit Mapping are different as in servlet mapping we use url pattern for mapping of servlet whereas here in implicit mapping its automatic i.e jsp pg is converted into servlet with the help of servlet engine



Defination: Servlet Chaining

This topic has been included in J2EE(Java to Enterprise Edition i.e Advance Java) Here as discussed earlier, output of one servlet act as a input to another servlet and at last request is sent to the browser and final output is achieved. Servlet allows us to invoke more than one servlet in sequence when the URL is opened with a common servlet.the process continues as follows-



·        Two Ways to Chain Servlets

In Servlets/JSPs, there are two ways to achieve servlet chaining using


RequestDispatcher:

RequestDispatcher is used to dispatch the request URL to execute another servlet in servlet chaining. Process goes on this way that first servlet directly dispatch the request to execute another servlet by dispatching URL. There are two methods:
request.include()  and request.forward()
The above methods are used to dispatch the request for another servlet execution. In this process, client or browser not involved in request dispatching..Here, all servers are executing in same server and client is not participating in dispatching thus  servlet chaining is fast. In this process we cannot communicate with multiple servers. We can use either of the method.



RequestDispatcher rd=new RequestDispatcher();
rd.forward(/”index.jsp”);
or
RequestDispatcher rd=new RequestDispatcher();
rd.include(/”servletTwo.jsp”);