Servlet collaboration: filtering and chaining

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

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 2
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();
}
}

Read more about Servlet Chaining
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: Servlet collaboration: filtering and chaining
Servlet collaboration: filtering and chaining
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeGLR4_nz6bHNNrhWocpi8vxfjje_jY3DY3PbKwtZ8FGp361pPNY7QQOG4RsdhNroYmi5eildzvAfBBFwi4VXQKHOWCmEFEEAIjtNDyIDmI3KJnVdPEWurLmkb0jWsJcfUMIIQMkej7DoY/s400/mime.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjeGLR4_nz6bHNNrhWocpi8vxfjje_jY3DY3PbKwtZ8FGp361pPNY7QQOG4RsdhNroYmi5eildzvAfBBFwi4VXQKHOWCmEFEEAIjtNDyIDmI3KJnVdPEWurLmkb0jWsJcfUMIIQMkej7DoY/s72-c/mime.PNG
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/servlet-collaboration-filtering-and.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/servlet-collaboration-filtering-and.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content