postheadericon RequestDispatcher include Example






Create Following Files


  1. Create web application [ RequestDispatcherDemo ]
    • header.jsp
    • footer.jsp
    • body.jsp
    • style.css
  2. In web application directory create [ RequestDispatcherDemo \ WEB-INF ] directory
    • create web.xml file
  3. Create directory [ RequestDispacherDemo \ WEB-INF \ classes ]
    • compile RequestDispacherDemo.java and upload RequestDispacher.class 



header.jsp

<html>
<head>
<LINK REL="STYLESHEET"  HREF="style.css"  TYPE="text/css">
</head>
<body bgcolor="#ababab">
<div class="header">header</div>

</body>
</html>



footer.jsp

<div class="footer">footer</div>

body.jsp

<div class="body">body</div>


style.css

.header
{
left:20%;
height:20%;
width:60%;
position:absolute;
background:#f99000;
text-align:center;
padding:5px;
}
.body
{
top:20%;
left:20%;
height:60%;
width:60%;
position:absolute;
background:#ffffff;
text-align:center;
padding:5px;
}
.footer
{
top:80%;
left:20%;
height:20%;
width:60%;
position:absolute;
background:#a1a000;
text-align:center;
padding:5px;
}



RequestDispatcherDemo.java

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

public class RequestDispacherDemo extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
        RequestDispatcher dispatcher;
dispatcher = request.getRequestDispatcher("header.jsp");
dispatcher.include(request, response);
dispatcher = request.getRequestDispatcher("body.jsp");
dispatcher.include(request, response);
dispatcher = request.getRequestDispatcher("footer.jsp");
dispatcher.include(request, response);
}
}




web.xml

<web-app>
 
   <servlet>
      <servlet-name>RequestDispacherDemo</servlet-name>
      <servlet-class>RequestDispacherDemo</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>RequestDispacherDemo</servlet-name>
        <url-pattern>/index.html</url-pattern>
    </servlet-mapping>

 </web-app>



0 comments:

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.