0

I have 2 web apps, no front-end(i.e html/Jsp) in either. Both have one servlet each.
Lets call them WebApp1/WebApp2 and ServiceServlet1/ServiceServlet2.
I have 2 war files, WebApp1.war and WebApp2.war and both deployed.

I call the ServiceServlet1 directly from the browser with -
http://localhost:8080/WebApp1/ServiceServlet1
Obviously the doGet method will be called(POST is associated only with FORM, correct me if I am wrong).
The ServiceServlet1 is build something like -

    public class ServiceServlet1 extends HttpServlet {
     @Override
     protected void doGet(HttpServletRequest httpRequest, HttpServletResponse httpResponse)
       throws ServletException, IOException {
      doPost(httpRequest, httpResponse);
     }

     @Override
     protected void doPost(HttpServletRequest httpServletRequest,
       HttpServletResponse httpServletResponse) throws ServletException,
       IOException {
      RequestDispatcher requestDispatcher;

      try {
// Process something
       requestDispatcher = getServletContext().getRequestDispatcher("/WebApp2/ServiceServlet2");
       requestDispatcher.forward(httpServletRequest, httpServletResponse);
      } catch (IOException ioException) {
       ioException.printStackTrace();
      } catch (ServletException servletException) {
       servletException.printStackTrace();
      }
     }
    }

Essentially, what I require is to call the doPost() of ServiceServlet2
I have tried few different ways with httpReq.getRequestDispatcher(), sendRedirect etc. but have failed so far.

So how can I make it happen?

Thank you.

Swift-Tuttle
  • 485
  • 3
  • 14
  • 25

2 Answers2

1

In addition to the answer of ckuetbach, you can't change the request method when dispatching the request. If the second servlet cannot be changed to execute the same business logic on doGet() as well, then you have to fire a POST request yourself programmatically.

HttpURLConnection connection = (HttpURLConnection) new URL("http://localhost/WebApp2/ServiceServlet2").openConnection();
connection.setRequestMethod("POST");
InputStream response = connection.getInputStream();
// ... Write to OutputStream of your HttpServletResponse?

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • "If the second servlet is really that bad designed and you cannot fix it to be more flexible" - I didnt quite get what you mean by, if you can pls elaborate. Can you pls tell what options are possible here, I mean in context of making the second servlet more flexible. Any flipside to the solution you have provided? – Swift-Tuttle Jan 27 '11 at 12:44
  • Now I reread it, I must admit that it sounds a bit harsh. I'll update the answer. – BalusC Jan 27 '11 at 12:50
  • Just to confirm. The data I have to send to the second servlet, I will write through the OutputStream, which is a String(right?), consider it to be an xml file content. The HttpReq obj of the second servlet will then contain this file content and I can extract through `httpReq.getInputStream()`. Am I thinking correctly? – Swift-Tuttle Jan 27 '11 at 14:30
  • Just write it as query string in request body like `name=value` and then it'll just be available by `request.getParameter("name")`. See also the linked article. – BalusC Jan 27 '11 at 14:33
0

The two servlets don't share the same Classloader, because they are in different ´*.WAR´-files.

As far as I know, you have to chances to do what you want:

  1. Disable Classloader-Separation in tomcat (I've never done this tomcat)
  2. Repackage the two aplication within one *.WAR
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79