I have a problem with calling ajax requests.the Servlet returns whole page as response
HTML Code
<form action="userlogin" method="POST">
User name : <input type="text" name="username" id="username"/><br/>
Password : <input type="text" name="password" id="password"/><br/>
<input type="submit" value="Login" name="submitbtn" id="submitbtn"/><br/>
<label id="failedText"></label>
</form>
JQuery Code
$(document).ready(function () {
$("input[name='submitbtn']").click(function (e) {
e.preventDefault();
$.ajax({
url: 'userlogin',
type: 'POST',
data: $("form[action='userlogin']").serializeArray(),
success: function (responseText) {
if (responseText != 'failure')
$("form[action='userlogin']").submit();
else
$('#failedText').text('Invalid Username or password');
}
});
});
});
Serlet Code
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
if (username.equals("admin") && password.equals("password")) {
path = "adminhome.html";
response.sendRedirect(path);
}else
response.getWriter().write("failure");
if it is failure it works the way i wanted, if it success i need to navigate to admin home page,but the code responses with the whole page as response text.