Hanumath
Hanumath

Reputation: 1117

How can I send http request to another servlet

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.

Upvotes: 1

Views: 26466

Answers (3)

Yash
Yash

Reputation: 9578

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

Upvotes: 1

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

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.

Upvotes: 6

Robadob
Robadob

Reputation: 5349

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.

Upvotes: 2

Related Questions