RMS
RMS

Reputation: 11

How do I invoke a servlet without changing url?

I want to invoke a servlet without changing the url thorough jsp. How do I do this?

Upvotes: 0

Views: 1387

Answers (5)

Sebastien Lorber
Sebastien Lorber

Reputation: 92112

It can seems a bit dirty but it's working... and generally used by marketing tracking services... You can add a hidden 1x1px image to your jsp, with the url = your servlet. When trying to load the img, the servlet will be called by the browser...

http://yourservlet?args=...

This way you can call the servlet easily, but you can't treat the servlet response (or you just can show a different image to browser if needed...)

Upvotes: 0

In JSP you can invoke any code in snippets. I would suggest you change the program so that the servlet and the JSP both call a common method. That is MUCH easier to code and test.

Upvotes: 0

Little Bobby Tables
Little Bobby Tables

Reputation: 5351

Another (rude) way to do it is get the other servlet's response using java.net.URL.openStream() (or a similar call) and flushing the result to your servlet's output stream. However, as mentioned above, you're better off using standard AJAX techniques, or just a better OOP design.

Upvotes: 1

Yuval Adam
Yuval Adam

Reputation: 165182

If you must include a servlet response in your JSP:

<jsp:include page="yourServlet" />

But this is poor architecture. The proper way would be to have a servlet which acts as a controller to display data on a JSP.

Upvotes: 1

Quentin
Quentin

Reputation: 943108

The usual methods are via XMLHttpRequest or an iframe. Various libraries such as YUI will do the heavy lifting for you.

Upvotes: 1

Related Questions