Reputation: 7974
I have a url-mapping in my web.xml
such that requests for a specific url x.pt
gets mapped to a Servlet say Servlet1
. In the service()
of this servlet I check if the request has some specific parameter.
If so, the call is delegated to another servlet Servlet2
by instantiating it and calling its service method.
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// if the call is for some special events (request has some specific parameter)
if (req.getParameter(conditionCheck()) {
doPost(req, res);
} else {
// Report parsing
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try {
// instantiate Servlet2 object
servlet2.init(this.getServletConfig());
servlet2.service(req, res);
servlet2.destroy();
} catch (Exception e) {
LOG.error("Unable to execute event", e);
}
}
The browser returns some JSON text when the request is for special events( i.e. to Servlet2) Do let me know if I need to do something extra for getting response of Servlet2 to the brwoser.
Thanks in advance!
Upvotes: 0
Views: 1297
Reputation: 7335
As Kris says, I'd expect a RequestDispatcher to work, but I'm always uncomfortable when I see a servlet being called directly like this. Do you have the opportunity to move the logic that is provided by servlet2 into a separate object that both servlet1 and servlet2 can call upon? If you can, I think it'll give you a better, more easily testable solution.
Upvotes: 0
Reputation: 5792
You can forward your request using RequestDispacher:
RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
rd.forward(request, response);
Upvotes: 1