How to call a servlet from java

I have a third-party servlet inside the JAR that I cannot change. I expanded this servlet and used it in normal mode, since the servlet should be used, the client side makes an HTTP request that calls my servlet.

But now the client needs an automatic service, that is, I will need to make some requests to this third-party servlet from the same webapp where the servlet is located.

I looked at the third-party servlet code, but I did not find a place to bypass the servlet, because the HttpServletRequest and HttpServletResponse objects are passed from method to method ... Basically, it seems that I will need to re-implement all third-party codes.

The solutions that I found but do not satisfy me:

Servlet call from URL with HttpURLConnection:. In my common sense, saying that calling a third-party servlet from a URL is not the best way to go, except for the added overhead, I don’t want to disclose a third-party servlet. Calling my servlet from a URL also creates problems with sessions and other things.

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

Use jMock or something like that: I haven’t researched this solution, but it’s not mistaken to use the test library in a real environment.

Anyone have an idea how to interact with this third party servlet?

EDIT:

Since my English is not very good, and it’s hard for me to explain myself, here is a schematic attempt to explain better

Schematic

EDIT2: after a third-party meeting, they suggest isolating the methods I need to avoid calling the servlet. If you don't have the same luck, I checked the answers of both gigadot and BalusC.

+6
source share
3 answers

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

Now what you want to do is periodically create a report and store it in the session so that the user can receive the report, to receive it using another servlet.

If so, you want the task to run periodically on your server. You will need some kind of task scheduler to run on your server, and what the task does is just make an HTTP request to your servlet (it could be an HTTP GET or POST).

+1
source

Calling my servlet from a URL also creates problems with sessions and other things.

If this is the only problem, simply use the CookieManager to save cookies (and therefore the session) in subsequent URLConnection .

 // 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:

+1
source

You can try to split the servlet logic into several stages. An entry point that accepts a query / result, an action that processes the passed parameters and generates the result.

 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 allows you to trigger an action (whatever) to do what you want, and then do what you want with the answer. If returning a string is not enough, you can make any return type you want, perhaps something that can contain a list of headers, a response code, and a content template.

0
source

Source: https://habr.com/ru/post/902678/


All Articles