top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can I daisy chain servlets together such that the output of one servlet serves as the input to the next?

+2 votes
346 views
How can I daisy chain servlets together such that the output of one servlet serves as the input to the next?
posted Oct 7, 2015 by Shyam

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

There are two common methods for chaining the output of one servlet to another servlet :

the first method is the aliasing which describes a series of servlets to be executed
the second one is to define a new MIME-Type and associate a servlet as handlers As I don’t really use the second one, I’ll focus on the aliasing.

To chain servlets together, you have to specify a sequential list of servlets and associate it to an alias. When a request is made to this alias, the first servlet in the list is invoked, processed its task and sent the ouptut to the next servlet in the list as the request object. The output can be sent again to another servlets.

To accomplish this method, you need to configure your servlet engine (JRun, JavaWeb server, JServ …).

For example to configure JRun for servlet chaining, you select the JSE service (JRun servlet engine) to access to the JSE Service Config panel. You have just to define a new mapping rule where you define your chaining servlet.

Let say /servlets/chainServlet for the virtual path and a comma separated list of servlets as srvA,srvB.

So when you invoke a request like http://localhost/servlets/chainServlet, internally the servlet srvA will be invoked first and its results will be piped into the servlet srvB.

The srvA servlet code should look like :

public class srvA extends HttpServlet { ... public void doGet (...) {
PrintWriter out =res.getWriter(); rest.setContentType("text/html");
... out.println("Hello Chaining servlet"); } }

All the servlet srvB has to do is to open an input stream to the request object and read the data into a BufferedReader object as for example :

BufferedReader b = new BufferedReader( new InputStreamReader(req.getInputStream() ) ); String data = b.readLine(); b.close();

After that you can format your output with the data.

It should work straigthforward with Java Web Server or Jserv too. Just look at in their documentation to define an alias name.

answer Oct 8, 2015 by Karthick.c
Similar Questions
+2 votes
public class ServletClassName extends HttpServlet {
//declare instance variables for the servlet
int  instanceVar ; // not thread−safe
// overriding the Generic Servlet.init ( )
@Override
public void init ( ) throws ServletException {
// initialize instance variables, etc.
instanceVar = 0 ;
}
. . .
}

Per each page request, in the service() method (which eventually will be either doGet(), doPost(), or whatever),
you can manipulate instance variables.

Write a Servlet that counts the total number of visits (how many times it's being visited from all clients). The Servlet should display the following information

• Total Page Visits:
• Client Remote Address:
• Client Host Address: <client_fully_qualied_url__or__client_IP_address>

The last two can be obtained from the HttpServletRequest, Refer to the Java EE API

then how can re-do these using the Java API for HttpSession.

...