8

Can you call a servlet with a link? For example

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

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.

bluish
  • 26,356
  • 27
  • 122
  • 180
Ankur
  • 50,282
  • 110
  • 242
  • 312

3 Answers3

13
<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")

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
10

Just to clear a misconception:

As far as I know you can't call Java code with Javascript events.

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.

  1. 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>
    
  2. 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>
    
  3. 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.

Just substitute http://www.google.com/search with your own servlet to get the above examples to work in your environment.

For more background information, you may find this article useful as well.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    To further clarify the misconception - you cannot 'call Java from javascript', but you can invoke a URL which ultimately results in Java code being executed. The difference is subtle, but important to understand. – belugabob Feb 15 '10 at 16:24
2

Perhaps the following is what you're after:

<jsp:include page="/MyServlet">
    <jsp:param name="param" value="value"/>
</jsp:include>
David Grant
  • 13,929
  • 3
  • 57
  • 63