0

I am calling a servlet from JavaScript with request parameters but the servlet is not getting called.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<title>Test</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style type="text/css" media="screen">
</style>
</head>
<body>
  <table border="1" cellpadding = "15">
    <tr><td>1</td><td>2</td><td>3</td></tr>
    <tr><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>7</td><td>8</td><td>9</td></tr>
  </body>
</table>
<script>
$('td').click(function(){
    var colIndex = $(this).parent().children().index($(this));
    var rowIndex = $(this).parent().parent().children().index($(this).parent());
    alert('Row: ' + rowIndex + ', Column: ' + colIndex);
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://localhost:8080/TestFinalWebApp/MyServlet?rowIndex=' + rowIndex + "&colIndex=" + colIndex, true);
    xhr.send(null);
});
</script>
</body>
</html>

This is the deployment descriptor:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
    <servlet-name>GetParameters</servlet-name>
    <servlet-class>com.example.web.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>GetParameters</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
</servlet-mapping>  
</web-app>

EDIT:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String rowIndex = request.getParameter("rowIndex");
        String colIndex = request.getParameter("colIndex");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print("rowIndex:" + rowIndex + "  colIndex:" + colIndex);
    }

Can you please tell me why the servlet is not getting called?

10
  • Where is the code of receiving response from servlet? Commented May 15, 2012 at 8:01
  • Is the JavaScript function even called? Is the alert shown? Commented May 15, 2012 at 8:03
  • @alexis - yes the alert is shown.. Commented May 15, 2012 at 9:35
  • @ravi - i will post the code.. let me edit the question Commented May 15, 2012 at 9:35
  • 1
    for people looking to explore more options: [1]: stackoverflow.com/questions/2132242/… Commented May 16, 2012 at 5:49

1 Answer 1

3

Some comments:

  1. Since you already include jQuery, use the ajax() function. It has much better error handling and solves many corner cases for you.

  2. Update to a more recent version of jQuery. The latest release is 1.7.2.

  3. localhost will only work if the server is on the same machine as the browser. OK during development but will break when you deploy. Either get rid of it (then the browser will prepend it for you) or make sure the URL is generated from the servlet context.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks aaron.. ajax function worked.. I am a newbie so took some time... but got what i required..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.