0

I want to send JSP java variables that I had defined via ajax request, it it possible?

My JSP :

<%
    String fileName = (String)request.getAttribute(MatafConstants.PARAM_IMG_FILE_NAMES);
%>

And then I want to send this String:

    $.ajax({
                  url: "/some/urlServlet",
                  type: "get", //send it through get method
                  data: { 
                    "statusID": status, 
                    "test": '<%=fileName%>' //here is the param I send
                  },
                  success: function(response) {
                    //Do Something
                    console.log(response);
                  },
                  error: function(xhr) {
                    //Do Something to handle error
                  }
 });

How can I sent this param? Or the bigger question I can I use JSP java variable in JavaScript?

8
  • What happens with your current approach? Commented Jun 13, 2017 at 8:57
  • do you want to keep your JS code on the same page with JSP? Commented Jun 13, 2017 at 9:04
  • Just saying, Ajax is using the Javascript, there is no JSP on the client side. This means that "fileName" is a constant at this point (that you output/write with the <%= ...%> tag. Check the source on the client, you will see that this will be like "test": 'your parameter' //here is the param I send Commented Jun 13, 2017 at 9:11
  • Well it works, but if the JSP variable I want to send is a Map? Commented Jun 13, 2017 at 9:50
  • You want to send the full Map with ajax ? If not, how are you choose the value you want ? Commented Jun 13, 2017 at 9:52

2 Answers 2

1

You can create hidden field with value from scriplet, and then use it's value in Java Script. Something like below:

JSP code

<input type="hidden" id="fileName" value="<%=(String)request.getAttribute(MatafConstants.PARAM_IMG_FILE_NAMES)%>">

Java script code

data: { 
    "statusID": status, 
    "test": document.getElementById("fileName").value;
},
Sign up to request clarification or add additional context in comments.

4 Comments

And if it is an array?create each elemnent?
What type of elements do you have in array?
If that request only need to use that value, this could be done directly in the data.test field. There is nothing special about using an hidden field in this case
@AxelH yes, agree, now see comment to main question.
0

Answer is YES. and your request looks correct. But make sure you add the AJAX call in the same page where that variable you are initiating.

<jsp:declaration>
    String name=null;
    String role=null;
    String company=null;
</jsp:declaration>
<jsp:scriptlet>
    if(request.getSession().getAttribute("username")==null){
        response.sendRedirect("../login.jsp");
    }
    else{
        name=(String)request.getSession().getAttribute("username");
        role= (String)request.getSession().getAttribute("role");
        company=(String)request.getSession().getAttribute("company");
    }
</jsp:scriptlet>

in javascript on the same page:

<script>
            var name="<jsp:expression>name</jsp:expression>";
            var company="<jsp:expression>company </jsp:expression>";
</script>

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.