File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet Chaining Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Chaining" Watch "Servlet Chaining" New topic
Author

Servlet Chaining

Austin Hoffman
Greenhorn

Joined: Feb 07, 2005
Posts: 21
What is servlet chaining?
Using ServletContext.getServlet() is servlet chaining?

Regards,
Austin
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No, using a RequestDispatcher to forward the requests from one servlet to another (or to a JSP) is.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Austin Hoffman
Greenhorn

Joined: Feb 07, 2005
Posts: 21
Hello All,
I believe that using ServletContext.getServlet() is also servlet chaining.

Using RequestDispatcher to forward the request to another servlet is however a preffered mechanism...

Any comments.....................

Regards,
Austin
Heonkoo Lee
Ranch Hand

Joined: Feb 10, 2005
Posts: 85
FYI,
For some requests, a chain of ordered servlets can be invoked rather than just one servlet. The input from the browser is sent to the first servlet in the chain and the output from the last servlet in the chain is the response sent back to the browser. Each servlet in the chain has the inputs and outputs piped to the servlet before and after it, respectively. (from Sun's documentation)

getServletContext().getServlet("name"), which is nothing but a method call, is deprecated and always returns null, and it's in the API for backward compatibility.

Regards,
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
"Servlet Chaining" is an old term - I think the preferred approach now is to use the "FilterChain" Interface to control passing request and response between servlets. FilterChain was introduced in version 2.3 of the servlet API.
Bill


Java Resources at www.wbrogden.com
Heonkoo Lee
Ranch Hand

Joined: Feb 10, 2005
Posts: 85
Originally posted by William Brogden:
"Servlet Chaining" is an old term - I think the preferred approach now is to use the "FilterChain" Interface to control passing request and response between servlets. FilterChain was introduced in version 2.3 of the servlet API.
Bill


I think they are similar in the sense that both utilize the forward mechanism. But, there is a difference such that you must declare in web.xml the intercepting filter and map it to the servlet name or url to be filtered. As for the 'Servlet Chaining', if a request is forwarded from one servlet/jsp to another, that is called servlet chaining.

In addition, the filter is used to filter not only request, but also response. If two filters A and B are declared for the same url pattern, the request is first filtered by A and then B. The response can also be filtered by those filters in reverse order, B and A. So the response can be altered by the filters as needed. I hope my understanding is right...

Regards,
[ April 03, 2005: Message edited by: Heonkoo Lee ]
Ravinder S Edhan
Ranch Hand

Joined: Dec 15, 2003
Posts: 57
Servlet chaining also means passing request from one servlet to another using below method:

request.getRequestDispacter("either JSP or SERVLET")

..... right .... I'm facing one problem doing so ....


RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/classes/com.kookiestest.example.KookieResult");


where KookieResult is in the same package is above class file.

It gives me .... Error 404--Not Found...

Please correct me where I'm making mistake.

Cheers
Ravinder S Edhan
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
use,
request.getRequestDispatcher("/WEB-INF/classes/com.kookiestest.example.KookieResult").forward(request, response);
Ravinder S Edhan
Ranch Hand

Joined: Dec 15, 2003
Posts: 57
Thanks Sripathi,

But still I'm getting the same error. Below are the two java files


KookieTest.java

package com.kookiestest.example;

import java.io.IOException;

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

public class KookieTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");

String name = request.getParameter("username");
Kookie kookie = new Kookie("username", name);

response.addKookie(kookie);
System.out.println("Before request dispatcher");
request.getRequestDispatcher("/WEB-INF/classes/com.kookiestest.example.KookieResult").forward(request, response);
}
}


KookieResult.java

package com.kookiestest.example;

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

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


public class KookieResult extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

System.out.println("inside result");
Kookie[] kookies = request.getKookies();
for(int i = 0; i < kookies.length; i++) {
Kookie kookie = kookies[i];
if(kookie.getName().equals("username")) {
String username = kookie.getValue();
out.print("Hello " + username);
break;
}
}
}
}


Any suggestions

Cheers
Ravinder S Edhan
Heonkoo Lee
Ranch Hand

Joined: Feb 10, 2005
Posts: 85
If you pass a path starting with '/' as in '/someDir/TargetServlet' to the request.getRequestDispather() method, the path is resolved just after server name. For example, if the caller servlet's url is http://www.server.com/AppName/CallerServlet, the path will be resolved as http://www.server.com/someDir/TargetServlet.

Also, I never used WEB-INF directory in the path to forward to. So, I'm not sure if it will work. I always use the mapped url for the servlet.

Regards,

[ April 04, 2005: Message edited by: Heonkoo Lee ]
[ April 04, 2005: Message edited by: Heonkoo Lee ]
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Austin Hoffman:
Hello All,
I believe that using ServletContext.getServlet() is also servlet chaining.

Using RequestDispatcher to forward the request to another servlet is however a preffered mechanism...

Any comments.....................

Regards,
Austin


Why ask the question if you're just going to disagree with the person that answers it?


James Carman, President<br />Carman Consulting, Inc.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12675
    
    5
Just to add to the nomenclature confusion you will see the architecture in which an initial servlet decides which servlet or JSP a request should be forwarded to called "Model 2".
Bill
Ravinder S Edhan
Ranch Hand

Joined: Dec 15, 2003
Posts: 57
Hi Heonkoo

Thanks for the hint Lee. I did the url-mapping as did for the first servlet.

Now my web.xml look like below

<web-app>


<servlet>
<servlet-name>KookieTest</servlet-name>
<servlet-class>com.kookiestest.example.KookieTest</servlet-class>
</servlet>

<servlet>
<servlet-name>KookieResult</servlet-name>
<servlet-class>com.kookiestest.example.KookieResult</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>KookieTest</servlet-name>
<url-pattern>/KookieTest</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>KookieResult</servlet-name>
<url-pattern>/KookieResult</url-pattern>
</servlet-mapping>


<welcome-file-list>
<welcome-file>jsp/form.html</welcome-file>
</welcome-file-list>

<login-config>
<auth-method>BASIC</auth-method>
<realm-name>WebApp</realm-name>
</login-config>

</web-app>


and at the same time I changed my KookieTest java file as follows

package com.kookiestest.example;

import java.io.IOException;

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

public class KookieTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");

String name = request.getParameter("username");
Kookie kookie = new Kookie("username", name);

response.addKookie(kookie);
System.out.println("Before request dispatcher");
RequestDispatcher view = request.getRequestDispatcher("KookieResult");
view.forward(request,response);}
}

Thanks and three cheers. It took me a full day to resolve this problem. This is how we learn

Cheers
Ravinder.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Chaining
 
Similar Threads
servlet chaining
Cleared SCWCD with 89%
passed SCWCD WITH 91%
servlet chaining
Servlet Chaining