emrahozkan
emrahozkan

Reputation: 193

request.getParameter() method return null with ajax call

I am trying to invoke doPost method in servlet with an ajax call yet the request.GetParameter(textboxID) returns null value. The doPost method is well invoked but prints null everytime. below is my jsp and servlet. I have been working on it for weeks yet haven't figured out what is wrong. Any help is highly appreciated.

JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script src="/javascript/basic.js"></script>
<script src="javascript/jquery-1.11.1.js"></script>
<script>
     $(document).ready(function() {
        $("#btnSubmit").click(function(e) {
            alert("jquery invoked");
            $.ajax({
                url : 'myservlet',
                type : 'GET',
                dataType : 'json',
                data : $("form").serialize,
                success : function(data) {
                    if (data.isValid) {
                        $('#output').html(data.filePath2);
                        $('#output').slidedown(499);
                    } else {
                        alert('Data is not valid!');
                    }
                }
            });
            return false;
        });     
    });  
</script>
<body>
    <form>
        <input type="text" id="txtFilepath"  name="txtFilepath"/> <br>
        <input type="Submit" id="btnSubmit" value="Submit">
    </form>
    <p id="output"/>
</body>
</html>

servlet

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    Map<String, Object> map = new HashMap<String, Object>();
    // TODO Auto-generated method stub
    String filepath = request.getParameter("txtFilepath");
    System.out.println(filepath);

    map.put("isValid", true);
    map.put("filePath2", filepath);
    write(response, map);
}

private void write(HttpServletResponse response, Map<String, Object> map)
        throws IOException {
    // TODO Auto-generated method stub
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(new Gson().toJson(map));
}

Upvotes: 0

Views: 3661

Answers (3)

Anurag Anand
Anurag Anand

Reputation: 498

Add this doGet() method to your servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 

doPost(request,response);

}

In jquery you should correct this data : $("form").serialize to data : $("form").serialize()

.serialize() is a Method.

And most important point is <input type="Submit" id="btnSubmit" value="Submit"> should be

<input type="Button" id="btnSubmit" value="Submit"> , The Button should be of type Button not type Submit . It will always fail, if it is of Type Submit.

OR

Add following line Before Ajax call e.preventDefault(), it will cancel the Default behaviour of submit button.

So it should be

$("#btnSubmit").click(function(e) {

        e.preventDefault();

        alert("jquery invoked");
        $.ajax({... Rest of the code

Upvotes: 1

Gaurav Daga
Gaurav Daga

Reputation: 343

You are implementing POST inside servlet and calling GET from jQuery ajax. Try using type:'POST' instead of type:'GET' inside your javascript function.

Hope this helps!

Upvotes: 2

frankfg
frankfg

Reputation: 625

You are missing the parenthesis at the serialize call:

data : $("form").serialize()

Upvotes: 1

Related Questions