digvijay
digvijay

Reputation: 361

Calling a servlet from a JavaScript with request parameters

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?

Upvotes: 0

Views: 7096

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328546

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.

Upvotes: 3

Related Questions