0

how to get servlet response message in javascript variable is there any possible way to get response message in java script?

Html:

<form method="post" id="importForm">
Name<input type="text" name="name"/>
<input type="submit" onclick="importScenarioFromServer();">
</form>

javascript:

function importScenarioFromServer(){
    var result = document.forms["importForm"].submit();
}

servlet:

int numberOfRecs = db.setDBValue(dMap);
if (numberOfRecs == 1) {
String result = "success";
response.getWriter().print(result);
}
10
  • 1
    Use ajax if you don't want to refresh the page, for this JQuery#ajax will come in handy. Commented Oct 18, 2014 at 10:04
  • @arvind i'm sending multipart request but, ajax didn't support multipart requst. Commented Oct 18, 2014 at 10:06
  • See this: stackoverflow.com/questions/12831680/… Commented Oct 18, 2014 at 10:20
  • @AMolTate i tried but it's not work Commented Oct 18, 2014 at 10:30
  • What problem you are facing for using ajax? Commented Oct 18, 2014 at 10:32

1 Answer 1

1

This can be implemented using ajax either in javascript or by using jquery. we can serialise form in ajax hit and then can get response data from servlet in success event callback function. below is example...

function getData()
{
var client;
var data;
var url_action="/temp/getData";
if(window.XMLHttpRequest)
{
    client=new XMLHttpRequest();
}
else
{
    client=new ActiveXObject("Microsoft.XMLHTTP");
}
client.onreadystatechange=function()
{
    if (client.readyState==4 && client.status==200)
    {
         document.getElementById("response").innerHTML=client.responseText; 
    }
};
data="name="+document.getElementById("name").value+"&file="+document.getElementById("filname").value;
client.open("POST",url_action,true);
client.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
client.send(data);
}


Servlet post method

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out=response.getWriter();
    log.info("Good");
    out.println("Good to go");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.