For Servlet in Java, we can’t forward to another server because Servlet works within the sam container or the same server.

There are two files in this sample code like ProcessServlet.java and ForwardAndIncludeServlet.java
Here the sample code that I have done for testing to clarify with Servlet Containing Process.

ProcessServlet.java

/*
 * ProcessServlet.java
 *
 * Created on August 17, 2009, 6:43 PM
 */

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Seven
 * @version
 */
public class ProcessServlet extends HttpServlet {
    private static final String forwardTo
            = "ForwardAndIncludeServlet";
    private static final String includeIn
            = "ForwardAndIncludeServlet";
    
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        
        out.print("<html><head><style>");
        out.print("p,form{font-family:tahoma;font-size:10pt;}");
        out.print("input{width:20;height:20;}");
        out.print("</style></head><body>");
        
        // Displaying Form
        
        out.print("<form action=\"");
        out.print( req.getRequestURI() );
        out.print("\" method=\"post\">");
        out.print("<input type=\"hidden\" name=\"mode\" ");
        out.print("value=\"forward\">");
        out.print("<input type=\"submit\" value=\" \"");
        out.print("> ");
        out.print(" Forward to another Servlet ..");
        out.print("</form>");
        
        out.print("<form action=\"");
        out.print( req.getRequestURI() );
        out.print("\" method=\"post\">");
        out.print("<input type=\"hidden\" name=\"mode\" ");
        out.print("value=\"include\">");
        out.print("<input type=\"submit\" ");
        out.print("value=\" \"> ");
        out.print(" Include another Servlet ..");
        out.print("</form>");
        
        out.print("</body></html>");
        out.close();
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
        ServletContext sc = getServletConfig().getServletContext();
        
        String mode = req.getParameter("mode");
        
        // Forwarding to Servlet2
        
        if(mode != null && mode.equals("forward")) {
            req.setAttribute("mode", "Forwarding Response..");
            req.getRequestDispatcher(forwardTo).forward(req, res);
        }
        
        // Including response from Servlet2
        
        if(mode != null && mode.equals("include")) {
            req.setAttribute("mode", "Including Response..");
            req.getRequestDispatcher(includeIn).include(req, res);
        }
    }
    
    // </editor-fold>
}

ForwardAndIncludeServlet.java

/*
 * ForwardAndIncludeServlet.java
 *
 * Created on August 17, 2009, 7:24 PM
 */

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Seven
 * @version
 */
public class ForwardAndIncludeServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest req, HttpServletResponse res) 
		throws ServletException, IOException {

		res.setContentType("text/html");
		PrintWriter out = res.getWriter();

		// Nothing important here

		out.print("<html><head><style>");
		out.print("p,a{font-family:tahoma;font-size:10pt;");
		out.print("color:black;}");
		out.print("input{width:20;height:20;}");
		out.print("</style></head><body>");
		out.print("<p><a ");
		out.print("href=\"/servlet/com.stardeveloper.servlets.");
		out.print("TestDispatcherServlet1\">");
		out.print("Go to TestDispatcherServlet1</a>.</p>");
		out.print("</body></html>");

		out.close();
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res) 
		throws ServletException, IOException {

		String mode = (String)req.getAttribute("mode");

		res.setContentType("text/html");
		PrintWriter out = res.getWriter();

		out.print("<html><head><style>");
		out.print("p,a{font-family:tahoma;font-size:10pt;");
		out.print("color:black;}");
		out.print("input{width:20;height:20;}");
		out.print("</style></head><body>");
		out.print("<p><b>");
		out.print("TestDispatcherServlet2</b> :</p>");

		if(mode != null) {
			out.print("<p>Attribute value received : ");
			out.print( mode );
			out.print("</p>");
		} else {
			out.print("<p>I have been invoked directly.</p>");
		}

		out.print("</body></html>");
		out.close();
	}

}

Advertisements