We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 6 April 2014

Servlet – Request Dispatcher include

In this tutorial we are going to see about Servlet Request Dispatcher include with example program.
void include(ServletRequest request,ServletResponse response)throws ServletException,java.io.IOException

Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes.

The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.

This method sets the dispatcher type of the given request to DispatcherType.INCLUDE.

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index.html");
requestDispatcher.include(request, response);

request - a ServletRequest object that contains the client's request
response - a ServletResponse object that contains the servlet's response

RequestDispatcher getRequestDispatcher(String);

The getRequestDispatcher(String) method of request object returns RequestDispatcher Interface.

Now see the example program

1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .

2. Create the Project called ServletExample as given below.

Servlet-dynamic web project

3. Create package called com.javatutorialscorner.servlet under ServletExample.

4. Create following  Servlet as shown in figure.

  • RequestDispatcherInclude
  • DestinationServlet
Create Servlet

http servlet 
5. Click Next it will show URL mapping.You can edit Servlet URL if you need.

6. Click Next it will show methods available  in HttpServlet. Select appropriate method you need.

http servlet Services

7. By default your servlet will be mapped in web.xml, if your servlet not mapped in your web.xml use the following configuration to map the servlet in your web.xml

Servlet Mapping

<servlet> 
    <servlet-name>Your Servlet Name</servlet-name> 
    <servlet-class>Fully Qulaified Servlet Class Name</servlet-class> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>Your Servlet Name</servlet-name> 
    <url-pattern>/URL to Call Servlet</url-pattern> 
  </servlet-mapping> 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
 <display-name>ServetExample</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
  <servlet-name>RequestDispatcherInclude</servlet-name>
  <servlet-class>com.javatutorialscorner.servlet.RequestDispatcherInclude</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>RequestDispatcherInclude</servlet-name>
  <url-pattern>/RequestDispatcherInclude</url-pattern>
 </servlet-mapping>
 <servlet>
  <servlet-name>DestinationServlet</servlet-name>
  <servlet-class>com.javatutorialscorner.servlet.DestinationServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>DestinationServlet</servlet-name>
  <url-pattern>/DestinationServlet</url-pattern>
 </servlet-mapping>
</web-app>

8. Add the required code inside doGet() method.

RequestDispatcherInclude.java

package com.javatutorialscorner.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RequestDispatcherInclude
 */

public class RequestDispatcherInclude extends HttpServlet {
 private static final long serialVersionUID = 1L;

 /**
  * @see HttpServlet#HttpServlet()
  */
 public RequestDispatcherInclude() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  String target = request.getParameter("target");
  RequestDispatcher requestDispatcher = null;
  response.setContentType("text/html");
  PrintWriter writer = response.getWriter();
  if (target.equals("page")) {
   writer.print("Taget Type : " + target);
   requestDispatcher = request.getRequestDispatcher("/index.html");
  } else {
   writer.print("RequestDispatcher Include -  Taget Type : " + target);
   requestDispatcher = request
     .getRequestDispatcher("DestinationServlet");
  }

  requestDispatcher.include(request, response);

 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

}

DestinationServlet.java

package com.javatutorialscorner.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DestinationServlet
 */

public class DestinationServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 /**
  * @see HttpServlet#HttpServlet()
  */
 public DestinationServlet() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  String target = request.getParameter("target");
  response.setContentType("text/html");
  PrintWriter writer = response.getWriter();
  writer.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
    + "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"><title>Java Tutorials Corner - Servlet -  Request Dispatcher include</title>"
    + "</head><body><h1>Java Tutorials Corner - Servlet - Request Dispatcher include</h1><table><tr>"
    + "<td> Request Dispatcher Target : </td><td>"
    + target
    + "</td></tr></table></body></html>");
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

}

9. Create html page in WebContent folder

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Java Tutorials Corner - Servlet - Request Dispatcher
 Include</title>
</head>
<body>
 <h1>Java Tutorials Corner - Servlet - Request Dispatcher Include</h1>
 <br />
 <a
  href="http://localhost:8080/ServletExample/RequestDispatcherInclude?target=page">Page</a>
 <br />
 <a
  href="http://localhost:8080/ServletExample/RequestDispatcherInclude?target=servlet">Servlet</a>

</body>
</html>

In above html page in form tag method="get"to mention the form submit using get method.The default method to submit form is GET.

10. Now save and Run the servlet, Run As –> Run on Server – Select your web Server to run the servlet. ( see How to configure tomcat in eclipse) 
http://www.javatutorialcorner.com/2014/03/how-to-configure-tomcat-in-eclipse.html

11.call the following URL .

http://localhost:8080/ServletExample/index.html

Output

image

Click The Link page

See the response of servlet Included in page.

image

Click The Link Servlet

See the response of first servlet Included in Response of second Servlet.

image

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Servlet – Request Dispatcher include Rating: 5 Reviewed By: eHowToNow