0

I'm newbie on developing Java Servlet. Right now, I know how to pass single value from java servlet to javascript, but I can't find the solution on passing multiple values. Please help me to solve my problem. I'm using jquery for ajax to pass the value from jsp file to java servlet file.

The problem of this is that not getting the value of json object from servlet in ajax.js

Here is my codes so far:

index.jsp:

<fieldset>
<legend>JSP Servlet Practice</legend>
<br>
Enter First Number: <input type="text" id="txt1" />
<br>
<br>
Enter Second Number: <input type="text" id="txt2" />
<br>
<form>
    <input type="radio" name="opt" value="Add" id="opt">Add
    <input type="radio" name="opt" value="Min" id="opt">Minus
    <input type="radio" name="opt" value="Mult" id="opt">Multiply
    <input type="radio" name="opt" value="Div" id="opt">Divide
</form>
<br>
<input type="button" value="Enter" id="btn">
<input type="button" value="Clear" id="btnClear">
<input type="submit" id="btnReverse" value="Reverse Number">

ajax.js: (In this part indicates the event of each button.)

$(document).ready(function() {
$("#btnReverse").click(function() {
    $.get('getTextFromJSP2', {
        txt1 : $('#txt1').val(), txt2 : $('#txt2').val()
    }, function(json) {
        $('#txt1').text(json.txt11);
    });
});

And this is the java servlet file, getTextFromJSP2.java:

@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    clsPL pl = new clsPL();
    String txt1 = request.getParameter("txt1");
    String txt2 = request.getParameter("txt2");
    JSONObject json = new JSONObject();
    json.put("txt11", txt1);
    json.put("txt22", txt2);
    response.setContentType("text/plain");
    response.getWriter().write(json.toString());
}

Thank you in advance.

4
  • Check this: stackoverflow.com/questions/14477845/… Commented Sep 8, 2015 at 3:51
  • Might help if you describe the problem you're seeing when you try running this code, to save someone from trying to reverse-engineer the problem before describing a solution? Commented Sep 8, 2015 at 3:55
  • @GeoffreyWiseman question updated. sorry for that. Problem is not getting values inside the jsonobject in ajax.js Commented Sep 8, 2015 at 4:15
  • @akhil_mittal sir that link shows how to pass multiple values from javascript to servlet file. The one that I need is the opposite of that. :( Commented Sep 8, 2015 at 5:32

1 Answer 1

0

Can you try putting following?

response.setContentType("application/json");
Sign up to request clarification or add additional context in comments.

4 Comments

Nothing happened sir :(
It seems you are passing JSON String from servlet. Can you try to parse this string to JSON object. You can use following code: var jsonObj = JSON.parse(json); $('#txt1').text(jsonObj.txt11);
Still nothing happened sir. Maybe you know the different approach on how to pass multiple values from servlet to javascript. if yes, please tell how to do that :(
Sir I used session instead of json or whatever and I got it haha Thanks for your help sir.

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.