Gil404
Gil404

Reputation: 721

Can't find the right way to get parameter value from the HttpRequest:

Can't find the right way to get parameter value from the HttpRequest:

this is my JQuery file:

$(document).ready(function() {
var currBoard;
var currCell;
$(".cell").click(function() {

    Cardboard = $ (this). attr ('name');
    currCell = $(this).attr('id');
    $.get('InGameServlet', function(responseText) {
        $("#"+currCell).text(responseText);

        alert("cell num:" + currCell + " Board: " + currBoard);
    });
});

});

This is my Servlet:

@WebServlet(name = "InGameServlet", urlPatterns = {"/InGameServlet"})
public class InGameServlet extends HttpServlet {
   protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    printSign(request.getParameter("currBoard"), request.getParameter("currCell"),     response);
}

In debug mode i see that the values i get from the request are NULL!

What am i doing wrong?

Upvotes: 0

Views: 1423

Answers (4)

Suresh Atta
Suresh Atta

Reputation: 121998

You are not passing values from your ajax request

$(".cell").click(function() {

    Cardboard = $ (this). attr ('name');
    currCell = $(this).attr('id');
    $.get('InGameServlet?currBoard="+Cardboard+"currCell="+currCell', function(responseText) {  //passing data as quesry param.
        $("#"+currCell).text(responseText);

        alert("cell num:" + currCell + " Board: " + currBoard);
    });
});

Then in servlet get request parameters as

request.getParameter("currBoard");

So it becomes,

printSign(request.getParameter("currBoard"),request.getParameter("currCell"),response);

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

You are calling getAttribute() instead of getParameter().

Request parameters are stored as request parameters in the HttpServletRequest.

Use

String value = request.getParameter("your parameter key");

This obviously depends on if your request actually contains request parameters. Here's how you do that with jQuery's get().

Upvotes: 2

Jason P
Jason P

Reputation: 27012

First you need to send the parameters with the request:

var data = {
    currBoard: $(this).attr('name'),
    currCell: $(this).attr('id')
};
$.get('InGameServlet', data, function (responseText) {
    $("#" + currCell).text(responseText);

    alert("cell num:" + currCell + " Board: " + currBoard);
});

Then retrieve them with request.getParameter() instead of getAttribute()

Upvotes: 0

kgautron
kgautron

Reputation: 8263

You don't seem to be passing any parameter in your $.get ajax call. In addition parameters are obtained by the getParameter() method not getValue().

Upvotes: 0

Related Questions