1

In my project folder,We have 2 java files under ContextPath/WEB-INF/Classes/*.class names are App1.class and App2.class

If I want to Run App1.class,Just i need to trigger URL in browser.

  http://localhost:8080/Mapping/App1

in the same way,if you want to trigger App2.class,use the following link

 http://localhost:8080/Mapping/App2

I want to trigger App2 from App1,means If you trigger App1 with corresponding URL in browser,It will be trigger App2.

I don't want to any response also.

How can I do this.

can anyone help me.

Thanks.

Hanumath
  • 1,117
  • 9
  • 23
  • 41

3 Answers3

6

I want to trigger App2 from App1,means If you trigger App1 with corresponding URL in browser,It will be trigger App2.

Considering App1 and App2 are configured as servlets in your Mapping web-app; you can make use of a RequestDispatcher to forward() the request to App2. This would happen server-side i.e. the browser would receive the response as if its coming from App1.

if (isForwardReqd()) {
    RequestDispatcher rd = request.getRequestDispatcher("App2");
    rd.forward(request, response);
}

Please, note App1 must not have committed a response before doing the forward(), otherwise you'd get an IllegalStateException.

Reference :
http://docs.oracle.com/javaee/7/api/javax/servlet/RequestDispatcher.html

Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • I got one more doubt,If I trigger one more app like `App3` from `App2`.can I do like this.finally flow is `App1`-->`App2`-->`App3`. – Hanumath Aug 09 '13 at 14:39
  • @user2642355 Yes, you can chain any number of servlets. Just make sure the only last servlet writes the response. If the servlets in between want to add something to the response; past it as a request attribute. – Ravi K Thapliyal Aug 09 '13 at 14:41
  • If I have `App3` is in another package name as `External`.The following statement is it right. ` RequestDispatcher rd = request.getRequestDispatcher("External/App3");` – Hanumath Aug 09 '13 at 14:50
  • what is `isForwardReqd()`. – Hanumath Aug 09 '13 at 14:53
  • No, `External.App3` should be mapped as a servlet in your `web.xml`. if you map it to `/App3` then that's the path you use to get dispatcher. `isForwardReqd()` is just a dummy method to check whether you need to forward or not. It's not required. – Ravi K Thapliyal Aug 09 '13 at 14:53
  • okay,Thanks.Can you check my previous question `http://stackoverflow.com/questions/18147115/how-can-i-call-externalapplication-based-on-activemq-message-using-jms`.What you suggested that same thing I did already,but's not working. If you my previous question then you will understand. – Hanumath Aug 09 '13 at 15:01
  • @user2642355 Berrylium is correct in saying that JSP/Servlets wasn't the best option to choose here. You should have implemented this through standalone Java classes. But, anyways I did check your code and `request.getRequestDispatcher("ClassName.class");` is incorrect. Like I said the path is configured in `web.xml`. It's not supposed to be package qualified class name. You're thinking J2SE standalone classes/package but you're implementing it using J2EE servlets. Please, reconsider your choice of technology platform here. – Ravi K Thapliyal Aug 09 '13 at 15:16
  • I changed request.getRequestDispatcher("ClassName").still it's not working.we have database is in outside.So How standalone Application is useful.I am new to Java So I will consider your suggestion.can You help me to write the code.First tell me, are you understand my workflow. – Hanumath Aug 09 '13 at 15:31
  • You also need to have a `` with `/ClassName` configured in `web.xml`. Please, take the help of someone well-versed in JEE. It won't be possible to have an elaborate discussion on this site. – Ravi K Thapliyal Aug 09 '13 at 15:50
2

You could send a Get request using Java;

URL url = new URL("http://localhost:8080/Mapping/App2");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.connect();

Alternatively you should probably configure App2 so that it's action is handled by a separate class or a method accessible to both servlets.

Robadob
  • 5,319
  • 2
  • 23
  • 32
1

Possible Ways

HTTP GET request with (optionally) query parameters

String query = String.format("param1=%s&param2=%s", 
             URLEncoder.encode("param1Value", "UTF-8"), 
             URLEncoder.encode("param1Value", "UTF-8"));

    URL url = new URL(servletURL + "?" + query);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("GET");
    Map<String, List<String>> header = conn.getHeaderFields();
    int responseCode = conn.getResponseCode();
    System.out.println("Headers : "+header);
    System.out.println("Response Code "+responseCode);

RequestDispatcher - Dispatch the Request from one resource to other-resource. If they are available in same project & server.

  • This interface allows you to do a server side forward/include, executes service(...)/doGet(...) method of requested Servlet.

    RequestDispatcher rd = req.getRequestDispatcher("/servlet2");
    rd.forward(req, resp); // rd.include(req, resp);
    

    Same Server, Different-Project

    RequestDispatcher rd = req.getServletContext().getContext("/Project2").getRequestDispatcher("/ips");
        rd.forward(req,  resp);
    

.sendRedirect()

  • Redirects the request from client side[URL get changed in client's browser].
  • When server encounters sendRedirect method it Sends a temporary redirect response to the client with 3XX status code, then requests new URL.
  • www.sun.com redirects to www.oracle.com/sun/index.html

    response.sendRedirect(servletURL); // Different Server.
    

Invoking Other Servlet using different ways

Community
  • 1
  • 1
Yash
  • 9,250
  • 2
  • 69
  • 74