Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call servlet from java

Tags:

java

servlets

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.

like image 810
Jose Antonio Avatar asked Nov 29 '11 18:11

Jose Antonio


People also ask

How can we call servlet from JSP?

Create a Servlet which does something like following in doGet() method. Now call the Servlet by the URL which matches its <url-pattern> in web. xml , e.g. http://example.com/contextname/servletURL. Do note that the JSP file is explicitly placed in /WEB-INF folder.

How do servlets work in Java?

Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver. Properties of Servlets are as follows: Servlets work on the server-side.

How can I call servlet without form and submit button?

How can I call servlet without <form> and submit button? You can use the same URL for the servlets as you use for a form action. P.S. Your URLs should not be page-relative. Use server-relative URLs that start with the context path.


1 Answers

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).

like image 54
gigadot Avatar answered Oct 29 '22 11:10

gigadot