21

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

Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183
user601367
  • 2,308
  • 12
  • 34
  • 44
  • can you provide more precise details of your requirement? Like what do you plan to achieve .. like may be some psuedocode or something? – Mahendra Liya Apr 18 '11 at 09:53

9 Answers9

18

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); 
%>
Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65
harishtps
  • 1,439
  • 7
  • 20
  • 35
13

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.

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
2

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'.

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
1

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);
Qiniso
  • 2,587
  • 1
  • 24
  • 30
1

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>
James Dunn
  • 8,064
  • 13
  • 53
  • 87
Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • 2
    may be you didn't read the question properly, he want to pass javascript value to scriplet tag and not value inside the scriplet to javascript :) – Mahendra Liya Apr 18 '11 at 10:10
0

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).

JediCate
  • 396
  • 4
  • 8
0

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.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
0

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.

Mukesh Singh Rathaur
  • 12,577
  • 2
  • 23
  • 24
0

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.

hiddeneyes02
  • 2,562
  • 1
  • 31
  • 58
Pranav
  • 1
  • 3