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.











No comments:

Post a Comment