Servlet chaining

In servlet chaining , multiple servlets are called for a single client HTTP request, each servlet providing part of the HTML output. Each s...


In servlet chaining, multiple servlets are called for a single client HTTP request, each servlet providing part of the HTML output. Each servlet receives the original client HTTP request as input, and each servlet produces its own output independently. Figure shows the servlet chaining process flow.

 

This servlet is specified on the original request, and multiple servlets are specified in an initialization parameter as the target:

Parameter name: chainer.pathlist
Parameter value: /chainFirst /chainSecond

Each servlet is called in the order specified on the alias, and the output HTML is made up of the output from all of the servlets.

Servlet filtering and chaining have the advantage of allowing the Web developer to create modular servlets that can, for example, output standard HTML headers and footers or provide common dynamic content for pages.

Example 1 and Example 2 show how two servlets can be used in collaboration to produce a single output response. Notice that the second servlet must process the output of the first servlet, in order to produce the desired composite result. One possible application of this technique might be when one servlet produces as its output an XML formatted response, and we chain it to another servlet that wraps the appropriate style sheet around the XML before sending it back to the browser.

Example 1

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ChainerFirst extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>ChainerFirst</TITLE><BODY>");
out.println("<H2>Servlet API Example - ChainerFirst</H2><HR>");
out.println("<H4><font color=\"Red\">This part of the output produced by
the first servlet..</font></H4>");
}
}

Example 2

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ChainerSecond extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType(req.getContentType());
PrintWriter out = res.getWriter();
//Need this to read through the output of the last servlet:
BufferedReader in = req.getReader();
String line;
while((line = in.readLine()) != null)
out.println(line);
out.println("<H4><font color=\"Green\">This part of the output produced
by the second servlet..</H4>");
out.println("</BODY></HTML>");
out.close();
}
}
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 chaining
Servlet chaining
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYkmSNnH-o9CcKZmo71kEU79rTtIVsQT-82omWE-WlFmtqbIJDc7vtPfZsq6yq6oeF0sHeVy6XDAuSVbs90agWlgSGac2U-4rpkbh7h-6cmsAiOHGFTCJxO-KBYFaLiL5GZ446IWx0U3VQ/s400/chaining.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYkmSNnH-o9CcKZmo71kEU79rTtIVsQT-82omWE-WlFmtqbIJDc7vtPfZsq6yq6oeF0sHeVy6XDAuSVbs90agWlgSGac2U-4rpkbh7h-6cmsAiOHGFTCJxO-KBYFaLiL5GZ446IWx0U3VQ/s72-c/chaining.PNG
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/servlet-chaining.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/servlet-chaining.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