Jose Antonio
Jose Antonio

Reputation: 444

How to call servlet from java

I've a third-party servlet inside a JAR that I cannot change. I've extended that servlet and been using it normally as a servlet should be used, the client side makes an HTTP request that invokes my servlet.

But now the client wants an automatic service, that is, I will need to do some requests to that third party servlet from the same webapp where the servlet is.

I looked at the the third party servlet code but I didn't found a place to bypass the servlet because the HttpServletRequest and HttpServletResponse objects are passed from method to method... Basically it seems that I would need to re-implement all the third party code.

Solutions I found but do not satisfy me:

Call servlet from URL with HttpURLConnection: My common sense says that calling the third party servlet from a url is not the best way to go, besides the overhead added, I don't want to expose the third party servlet. Calling my servlet from a url also brings problems with sessions and other things.

Call the doGet directly: This seems to be out of the question because there is no implementation for the HttpServletRequest and HttpServletResponse.

Use jMock or something like that: Didn't explore this solution yet, but it seams wrong to use a test-driven library in the real environment.

Anyone has an idea how to interact with that third party servlet?

EDIT:

Since my English is not very good and I'm finding difficult to explain myself here goes a schematic to try to explain better

Schematic

EDIT2: After a meeting the third party maker they offer to isolate the methods I need to avoid calling the servlet. If you don't have the same luck I did check out both gigadot and BalusC answers.

Upvotes: 6

Views: 2791

Answers (3)

BalusC
BalusC

Reputation: 1108537

Calling my servlet from a url also brings problems with sessions and other things.

If that's the sole problem, then just use the CookieManager to maintain the cookies (and thus also the session) in subsequent URLConnection calls.

// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));

// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...

connection = new URL(url).openConnection();
// ...

See also:

Upvotes: 1

gigadot
gigadot

Reputation: 8969

If I understand your question correctly, you have implemented or have a third party servlet that generate the report for you.

Now what you want to do is to periodically generate the report and store in session so that when user want to get the report they can retrieve it using another servlet.

If this is the case then you want the task to be running periodically on your server. You will need some sort of task scheduler to run on your server and what the task does is just make a http request to your servlet (this can be http GET or POST).

Upvotes: 2

Nthalk
Nthalk

Reputation: 3833

You could try to separate out your servlet logic into several phases. The entry point that takes the request/result, the action that processes parameters sent and generates the output.

public void doGet(HttpServletRequest req, HttpServletResponse rsp){
    relay(rsp,act(req.getParameter("a"));
}

public static String act(String a){
    return "You provided: " + a;
}

public static void relay(HttpServletResponse rsp, String content){
    rsp.setResponseCode(200);
    rsp.getOutputStream().write(content.getBytes());
}

This lets you call act(whatever) to do what you want, and then do what you want with the response. If returning a string is not enough, you could make any return type you want, probably something that could contain a list of headers, response code, and content template.

Upvotes: 0

Related Questions