user601367
user601367

Reputation: 2368

How do I pass JavaScript values to Scriptlet in JSP?

Can anyone tell me how to pass JavaScript values to Scriptlet in JSP?

Upvotes: 22

Views: 190872

Answers (9)

Pranav
Pranav

Reputation: 1

I Used a combination of the scriptlet, declaration, and expression tags...

<%! 
   public String st; 
%>
<%
   st= "<html> <script> document.writeln('abc') </script> </html>";
%>
<%=
   " a " + st + " <br> "
%>

The above code is working completely fine in my case.

Upvotes: 0

Vineela Thonupunuri
Vineela Thonupunuri

Reputation: 591

You cannot do that but you can do the opposite:

In your jsp you can:

String name = "John Allepe";    
request.setAttribute("CustomerName", name);

Access the variable in the js:

var name = "<%= request.getAttribute("CustomerName") %>";
alert(name);

Upvotes: 1

JediCate
JediCate

Reputation: 500

This is for other people landing here. First of all you need a servlet. I used a @POST request. Now in your jsp file you have two ways to do this:

  1. The complicated way with AJAX, in case you are new to jsp: You need to do a post with the javascript var that you want to use in you java class and use JSP to call your java function from inside your request:

    $(document).ready(function() {
        var sendVar = "hello";
        $('#domId').click(function (e)
        {                               
            $.ajax({
                type: "post",
                url: "/", //or whatever your url is
                data: "var=" + sendVar ,
                success: function(){      
                        console.log("success: " + sendVar );                            
                      <% 
                          String received= request.getParameter("var");
    
                          if(received == null || received.isEmpty()){
                              received = "some default value";
                          }
                          MyJavaClass.processJSvar(received); 
                      %>;                            
    
                }
            });
        });
    
    });
    
  2. The easy way just with JSP:

    <form id="myform" method="post" action="http://localhost:port/index.jsp">
         <input type="hidden" name="inputName" value=""/>
                   <% 
                          String pg = request.getParameter("inputName");
    
                          if(pg == null || pg.isEmpty()){
                              pg = "some default value";
                          }
                          DatasyncMain.changeToPage(pg); 
                      %>;     
    </form>
    

Of course in this case you still have to load the input value from JS (so far I haven't figured out another way to load it).

Upvotes: 0

harishtps
harishtps

Reputation: 1439

I can provide two ways,

a.jsp,

<html>
    <script language="javascript" type="text/javascript">
        function call(){
            var name = "xyz";
            window.location.replace("a.jsp?name="+name);
        }
    </script>
    <input type="button" value="Get" onclick='call()'>
    <%
        String name=request.getParameter("name");
        if(name!=null){
            out.println(name);
        }
    %>
</html>

b.jsp,

<script>
    var v="xyz";
</script>
<% 
    String st="<script>document.writeln(v)</script>";
    out.println("value="+st); 
%>

Upvotes: 18

Jim Blackler
Jim Blackler

Reputation: 23169

I've interpreted this question as:

"Can anyone tell me how to pass values for JavaScript for use in a JSP?"

If that's the case, this HTML file would pass a server-calculated variable to a JavaScript in a JSP.

<html>
    <body>
        <script type="text/javascript">
            var serverInfo = "<%=getServletContext().getServerInfo()%>";
            alert("Server information " + serverInfo);
        </script>
    </body>
</html>

Upvotes: 1

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17435

Your javascript values are client-side, your scriptlet is running server-side. So if you want to use your javascript variables in a scriptlet, you will need to submit them.

To achieve this, either store them in input fields and submit a form, or perform an ajax request. I suggest you look into JQuery for this.

Upvotes: 15

Mukesh Singh Rathaur
Mukesh Singh Rathaur

Reputation: 13105

Its not possible as you are expecting. But you can do something like this. Pass the your java script value to the servlet/controller, do your processing and then pass this value to the jsp page by putting it into some object's as your requirement. Then you can use this value as you want.

Upvotes: 0

Harry Joy
Harry Joy

Reputation: 59660

If you are saying you wanna pass javascript value from one jsp to another in javascript then use URLRewriting technique to pass javascript variable to next jsp file and access that in next jsp in request object.

Other wise you can't do it.

Upvotes: 0

Stijn Geukens
Stijn Geukens

Reputation: 15628

simple, you can't!

JSP is server side, javascript is client side meaning at the time the javascript is evaluated there is no more 'jsp code'.

Upvotes: 3

Related Questions