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));
}