Java 你能用一个链接调用一个servlet吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2267064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you call a servlet with a link?
提问by Ankur
Can you call a servlet with a link? For example
你能用一个链接调用一个servlet吗?例如
<a href="/servletName">link text</a>
And possibly pass parameters to the request object by adding them to the querystring.
并且可能通过将参数添加到查询字符串来将参数传递给请求对象。
If not, I have seen this kind of thing:
如果没有,我见过这样的事情:
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(/MyServlet);
dispatcher.include(request,response);
But how would I trigger this? For example if it was JavaScript code I could put it within a jQuery click function, or if this was a servlet I would put it into a method.
但我将如何触发这个?例如,如果它是 JavaScript 代码,我可以将它放在 jQuery click 函数中,或者如果这是一个 servlet,我会将它放入一个方法中。
But how do I call this code from within a JSP. As far as I know you can't call Java code with JavaScript events.
但是如何从 JSP 中调用此代码。据我所知,您不能使用 JavaScript 事件调用 Java 代码。
采纳答案by Bozho
<a href="servletUrl?param=value">click</a>
is perfectly legal and will work.
是完全合法的并且会起作用。
That will make the doGet(..)
method of the servlet be called, and you can get the parameter using request.getParameter("param")
这将使doGet(..)
servlet的方法被调用,您可以使用request.getParameter("param")
回答by David Grant
Perhaps the following is what you're after:
也许以下是您所追求的:
<jsp:include page="/MyServlet">
<jsp:param name="param" value="value"/>
</jsp:include>
回答by BalusC
Just to clear a misconception:
只是为了澄清一个误解:
As far as I know you can't call Java code with Javascript events.
据我所知,您不能使用 Javascript 事件调用 Java 代码。
You can perfectly call Java code with JavaScript code (and events). To the point, you just need to let JavaScript send a fullworthy HTTP request to the server side. There are basically 3 ways for this.
您可以使用 JavaScript 代码(和事件)完美调用 Java 代码。到此为止,您只需要让 JavaScript 向服务器端发送一个完整的 HTTP 请求即可。基本上有3种方法。
The first way is to simulate invocation of an existing link/button/form. E.g.
<a id="linkId" href="http://www.google.com/search?q=balusc">Link</a> <script type="text/javascript"> document.getElementById('linkId').click(); </script>
and
<form id="formId" action="http://www.google.com/search"> <input type="text" id="inputId" name="q"> </form> <script type="text/javascript"> document.getElementById('inputId').value = 'balusc'; document.getElementById('formId').submit(); </script>
The second way is to use window.location to fire a plain GET request. For example:
<script type="text/javascript"> var search = 'balusc'; window.location = 'http://www.google.com/search?q=' + search; </script>
The third way is to use XMLHttpRequest object to fire an asynchronous request and process the results. This technique is the base idea of "Ajax". Here's a Firefox compatible example:
<script type="text/javascript"> function getUrl(search) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { var responseJson = eval('(' + xhr.responseText + ')'); var url = responseJson.responseData.results[0].unescapedUrl; var link = document.getElementById('linkId'); link.href = link.firstChild.nodeValue = url; link.onclick = null; } } var google = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' xhr.open('GET', google + search, true); xhr.send(null); } </script> <p>My homepage is located at: <a id="linkId" href="#" onclick="getUrl('balusc')">click me!</a></p>
This can be rewritten in a shorter and crossbrowsercompatible manner with jQuery.
第一种方法是模拟对现有链接/按钮/表单的调用。例如
<a id="linkId" href="http://www.google.com/search?q=balusc">Link</a> <script type="text/javascript"> document.getElementById('linkId').click(); </script>
和
<form id="formId" action="http://www.google.com/search"> <input type="text" id="inputId" name="q"> </form> <script type="text/javascript"> document.getElementById('inputId').value = 'balusc'; document.getElementById('formId').submit(); </script>
第二种方法是使用 window.location 来触发一个普通的 GET 请求。例如:
<script type="text/javascript"> var search = 'balusc'; window.location = 'http://www.google.com/search?q=' + search; </script>
第三种方法是使用 XMLHttpRequest 对象来触发异步请求并处理结果。这种技术是“Ajax”的基本思想。这是一个与 Firefox 兼容的示例:
<script type="text/javascript"> function getUrl(search) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { var responseJson = eval('(' + xhr.responseText + ')'); var url = responseJson.responseData.results[0].unescapedUrl; var link = document.getElementById('linkId'); link.href = link.firstChild.nodeValue = url; link.onclick = null; } } var google = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' xhr.open('GET', google + search, true); xhr.send(null); } </script> <p>My homepage is located at: <a id="linkId" href="#" onclick="getUrl('balusc')">click me!</a></p>
这可以用jQuery以更短且跨浏览器兼容的方式重写。
Just substitute http://www.google.com/search
with your own servlet to get the above examples to work in your environment.
只需http://www.google.com/search
用您自己的 servlet替换即可使上述示例在您的环境中工作。
For more background information, you may find this articleuseful as well.
有关更多背景信息,您可能会发现这篇文章也很有用。