开发者

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?


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


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜