If multiple servlets are needed to produce a response to a particular client, then the normal procedure for producing HTML responses become...
If multiple servlets are needed to produce a response to a particular client, then the normal procedure for producing HTML responses becomes a little more complex. Two ways for multiple servlets to collaborate on the response are filtering and chaining.
Note: We recognize that sometimes the terms filtering and chaining are used interchangeably. However, in our discussion here, we use filtering to refer to only the MIME type filtering.
Servlet MIME filtering
Servlet MIME filtering
In servlet filtering, the servlet changes the MIME type of the response it sends from text/html to a user-defined MIME type. When using text/html, the Web application server would normally send the response straight back to the browser. With our own MIME type, we configure the Web application server to associate the MIME type with a particular servlet, and the output of the first servlet is used as input to the second servlet. In this way, servlets can filter their output as input to other servlets.
Figure shows the servlet filtering process flow.
In taking the output of one servlet, and using it as input to another servlet, this is useful for translation or substitution, for example, if you want to convert from the XML of one servlet into HTML for the user. Servlet filtering may have to be explicitly enabled in the Web Application Server through the
httpd.properties, enable.filters=true property.
Example 1 shows how we can write to a specially defined MIME type from one servlet. On the server, we define a second servlet to be the handler of this MIME type (Example 2).
Example 1
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FilterFirst extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/Deb");
PrintWriter out = res.getWriter();
out.println("<H2>Servlet API Example - FilterFirst</H2><HR>");
out.println("<H4>Output from the FilterFirst servlet</H4>");
out.close();
}
}
Example 2import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FilterFirst extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/Deb");
PrintWriter out = res.getWriter();
out.println("<H2>Servlet API Example - FilterFirst</H2><HR>");
out.println("<H4>Output from the FilterFirst servlet</H4>");
out.close();
}
}
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FilterSecond extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
//reading the output from the first servlet..
BufferedReader in = req.getReader();
String line;
out.println("<HTML><BODY>");
while((line = in.readLine()) != null)
out.println(line);
out.println("<H4><font color=\"Green\">This part of the output produced
by the second filter servlet..</H4>");
out.println("</BODY></HTML>");
out.close();
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FilterSecond extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
//reading the output from the first servlet..
BufferedReader in = req.getReader();
String line;
out.println("<HTML><BODY>");
while((line = in.readLine()) != null)
out.println(line);
out.println("<H4><font color=\"Green\">This part of the output produced
by the second filter servlet..</H4>");
out.println("</BODY></HTML>");
out.close();
}
}
Read more about Servlet Chaining