Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Servlet and invoke Java code from JavaScript along with parameters

I have session key that is a JavaScript variable which I got from a REST API call. I need to call my Java code in a servlet and pass that key as a parameter. What JavaScript function can I use to do that?

like image 290
Pranav Avatar asked Jan 25 '10 12:01

Pranav


People also ask

How send data from Javascript to servlet?

get(URL,data,function(data,status,xhr),dataType): this method loads data from the server through HTTP GET request. attributes: URL: mandatory, defines the relative path of the servlet. data: optional, the data to be passed along with the request (although GET filters are recommended to be sent as a query string)

How can we send parameters to a servlet?

You can pass parameters to an HTTP servlet during initialization by defining these parameters in the Web application containing the servlet. You can use these parameters to pass values to your servlet every time the servlet is initialized without having to rewrite the servlet.

How servlets are invoked from the client?

Launch a servlet with a Java app rather than a JSP 2077433Can I use a Java application (client) instead of a JSP (JavaServer Page) to invoke a servlet on an application server? A: Yes, you can invoke a servlet from a Java client by simply using the java. net. URL and java.

What is servlet How do you invoke a servlet?

A servlet is typically invoked via a servlet mapping in your servlet container configuration, when a request is made to the servlet container for a path matching that mapping. There are a number of resources for learning more about servlets on the Sun Oracle Java site's servlet page.


2 Answers

Several ways:

  1. Use window.location to fire a GET request. Caveat is that it"s synchronous (so the client will see the current page being changed).

    window.location = "http://example.com/servlet?key=" + encodeURIComponent(key);
    

    Note the importance of built-in encodeURIComponent() function to encode the request parameters before passing it.

  2. Use form.submit() to fire a GET or POST request. The caveat is also that it"s synchronous.

    document.formname.key.value = key;
    document.formname.submit();
    

    With

    <form name="formname" action="servlet" method="post">
        <input type="hidden" name="key">
    </form>
    

    Alternatively you can also only set the hidden field of an existing form and just wait until the user submits it.

  3. Use XMLHttpRequest#send() to fire an asynchronous request in the background (also known as Ajax). Below example will invoke servlet"s doGet().

    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://example.com/servlet?key=" + encodeURIComponent(key));
    xhr.send(null);
    

    Below example will invoke servlet"s doPost().

    var xhr = new XMLHttpRequest();
    xhr.open("POST", "http://example.com/servlet");
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("key=" + encodeURIComponent(key));
    
  4. Use jQuery to send a crossbrowser compatible Ajax request (above xhr code works in real browsers only, for MSIE compatibility, you"ll need to add some clutter ;) ).

    $.get("http://example.com/servlet", { "key": key });
    

    $.post("http://example.com/servlet", { "key": key });
    

    Note that jQuery already transparently encodes the request parameters all by itself, so you don"t need encodeURIComponent() here.

Either way, the key will be just available by request.getParameter("key") in the servlet.

See also:

  • How to use Servlets and Ajax?
  • Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
like image 166
BalusC Avatar answered Oct 30 '22 07:10

BalusC


No JavaScript function per se, but browsers usually* provide an XMLHttpRequest object and you can go through that.

Libraries such as YUI and jQuery provide helper functions to simplify its usage.

* for a value of "usually" that includes pretty much any browser that supports JavaScript and was released since Netscape 4 died

like image 44
Quentin Avatar answered Oct 30 '22 09:10

Quentin