Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Chaining

 
Austin Hoffman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is servlet chaining?
Using ServletContext.getServlet() is servlet chaining?

Regards,
Austin
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, using a RequestDispatcher to forward the requests from one servlet to another (or to a JSP) is.
 
Austin Hoffman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Heonkoo Lee
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use,
request.getRequestDispatcher("/WEB-INF/classes/com.kookiestest.example.KookieResult").forward(request, response);
 
Ravinder S Edhan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic