Solved

Javascript Function (Not using a form post) To Call Servlet (where no return values are needed)

Posted on 2008-06-20
5
798 Views
Last Modified: 2013-11-24
Hello:

I simply want to call a servlet from a javascript function.  My javascript function is not part of a form post.
The javascript function gets called from a Flash client actually.  My servlet takes a few parameters and is
expected to simply perform an update on the server side.  No html is returned for display and is not needed.

I tried something like this.:

function jsfunction()
{
        document.href.location="http://<domain.com>/servletname?parm1&parm2";
}

This doesn't work.  What is the easiest way to do this?  It needs to make the call and NOT WAIT for a response, as I my client is displaying a Flash container, and I do not want execution cycles waiting for
a return.

Please do not get involved in what my Flash client is doing and why I have it....I just need the javascript
to servlet solution without WAIT.

Thanks,

Dan
0
Comment
Question by:danw11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 21834641
0
 

Author Comment

by:danw11
ID: 21835018
HonorGod:

Your URL points me to a solution that is meant to update part of the DOM for changing the display
of the form.  I want a call to a servlet that does not call a callback() function.  In the example the validate()
function calls the server side.  That is good, but I don't want anything called back on the Javascript side.

Why do I need AJAX for this????

Can we do it without that?

Dan
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 21835239
Well, Ajax, by definition, allows your program to send a request to a service
application.  You can certainly have an empty callback function, something
along the lines of



  //------------------------------------------------------------------
  // Ajax routines for service contact
  //------------------------------------------------------------------
  var xmlHttp;
 
  //------------------------------------------------------------------
  // Name: createXMLHttpRequest()
  // Role: Create the XML Request Object used by the Ajax routines
  //------------------------------------------------------------------
  function createXMLHttpRequest() {
    if ( window.ActiveXObject ) {
      xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
    } else if ( window.XMLHttpRequest ) {
      xmlHttp = new XMLHttpRequest();
    }
  }
 
  //------------------------------------------------------------------
  // Name: getInfo()
  // Role: Issue the request to the service
  //------------------------------------------------------------------
  function getInfo( val ) {
    createXMLHttpRequest();
    var queryString = "/ContextRoot/ServiceName?ParmName=" + val;
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open( "GET", queryString, true );
    xmlHttp.send( null );
  }
 
  //------------------------------------------------------------------
  // Name: handleStateChange()
  // Role: Callback routine invoked when XML Request state changes
  //------------------------------------------------------------------
  function handleStateChange() {
  }

Open in new window

0
 

Author Comment

by:danw11
ID: 21837866
Honor God:

Excellent.  It works.  And, I now see the difference between my original attempt.:

document.href.location="http://<domain.com>/servletname?parm1&parm2";

and the AJAX solution.  The former actually redirects the browser to the URL for the servlet, which is
completely undesirable.  The latter (your AJAX solution) calls the servlet as expected without any
redirection.

Thanks,

Dan
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 21838105
Glad to be of some help.  Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question