Followers

JSP:RequestDispatcher

index.html
==========


<html>
    <head>
        <title>get Request Demo </title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="first.jsp" >
            <p>Please enter your name : <input type="text" name="suserName" ></p>
            <p>Please enter your address : <input type="text" name="saddress" ></p>
            <p>Please enter your Age : <input type="text" name="sage" ></p>
            <p><input type="submit" value="Send" ></p>
         
        </form>
    </body>
</html>


first.jsp
==========
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
        out.println("first servlet");
        String name = request.getParameter("suserName");
        String addr = request.getParameter("saddress");
        String ag = request.getParameter("sage");
        int age = Integer.parseInt(ag);
     
        out.println("<br /><br />");
        if(name.endsWith("kumar")){          
        RequestDispatcher rd=request.getRequestDispatcher("second.jsp");
        rd.include(request, response);
        }else{
        RequestDispatcher rd=request.getRequestDispatcher("third.jsp");
        rd.forward(request, response);
        }
        out.println("<br/><br/>");
        out.println("first servlet");
    %>
 
 
    </body>
</html>

second.jsp
==========

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
        String name = request.getParameter("suserName");
        String address = request.getParameter("saddress");
        String age = request.getParameter("sage");
        int ag = Integer.parseInt(age);
        out.println("<br /><br />");
        out.println("Hi "+name +" Please come and collect your pub pass by paying 1000 rupees");      
        out.println("<br/><br/>");
             
        %>
    </body>
</html>


third.jsp
==========

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%  
        out.println("<br/><br/>");
        String name = request.getParameter("suserName");
        String address = request.getParameter("saddress");
        String age = request.getParameter("sage");
        int ag = Integer.parseInt(age);
        out.println("<br /><br />");
        out.println("Hi "+name +" Please come and collect your pub pass by paying 2000 rupees");      
        out.println("<br/><br/>");
     
        %>
    </body>
</html>

No comments:

Post a Comment