0

I want to pass a javascript variable to a jsp scriptlet (both are in the same jsp only). I tried the following.

enter code here

<html>
    <script language="javascript" type="text/javascript">
    function    submit($i)   {
        var url=$i;   
        document.write("url is"+url); //giving me selected url and displaying on browser
        window.location.replace("view.jsp?url="+url);    
    }   
    </script>   
    <form method="get" action="/UrlServlet">   
        <select name="url" id="url" onchoice="submit(value);">   

            <c:forEach var="dataList" items=${dataList}>   
                <option value="${dataList.url}">${dataList.url}    
            </c:forEach>    
        <%    
             String name=request.getParameter("url");

             out.println(name);//giving me null as output 
            }
        %>
</html>

It is giving a null value when I print name. Can you please tell me what was wrong? When I print $i value in javascript it is giving me the url name from dataList. Here dataList is a list returned from my servlet. It is giving correct values only. But when I select one url from the drop down list it is giving me null at request.getParameter(). Please, can someone help me?

I tried this <%String st="<script>document.writeln(v)</script>;" in scriptlet instead of window.location.replace() in javascript. Then also I am not getting url value using getParameter, instead I am getting same statement <%String st="<script>document.writeln(v)</script> as output.

Please do help.

martieva
  • 131
  • 2
  • 11
user2365917
  • 1,095
  • 5
  • 15
  • 21
  • see here http://stackoverflow.com/questions/9699202/passing-a-variable-from-scriptlets-to-javascript it might help to you – PSR May 17 '13 at 10:34
  • I read it.But I am unable to get value of selected item.Please help me.And more over I am using select not input tag.And I am just selecting not submitting the option.Please help me. – user2365917 May 17 '13 at 10:37
  • Why are you trying to display "name" parameter where you pass `view.jsp?url=`... Have you tried `String myUrl=request.getParameter("url"); out.println(myUrl);` – TCHdvlp May 17 '13 at 10:38
  • Yes.But is giving me null value.And I tried this one also.in my scriptlet <%String st=""; And removed the line window.location.replace()-- line in javascript.Then I am getting out put as <%String st="" when I print url parameter.It is not giving the url value. – user2365917 May 17 '13 at 10:39
  • 1
    When the page first loads it'll print `null`. Then if the form is submitted to the same page then the value of the select element should be in the `"name"` parameter. I'm not familiar with the `onchoice` event, but if you are submitting via that `submit()` function it will _not_ set the `"name"` parameter because it doesn't actually submit the form, it passes it in the query string as `"url"`. – nnnnnn May 17 '13 at 10:39
  • @user2365917 where you are getting null.Your select box is alos not closed – PSR May 17 '13 at 10:48
  • If I print the name using out.println(name) am getting null value.But in Javascript if I print variable "url" I am getting the url value in my browser after selection. – user2365917 May 17 '13 at 10:51
  • @TCHdvlp I have tried to print but getting null value. – user2365917 May 17 '13 at 10:53
  • Have a look at this http://stackoverflow.com/questions/5701031/how-do-i-pass-javascript-values-to-scriptlet-in-jsp – Rohan May 18 '13 at 08:11
  • What `html/jsp/servlet` is making a request to this `JSP` ? You need to understand how **Request parameters** work. `name` will have some value only if requesting page sends `name` element in the request. – Ravi Trivedi May 18 '13 at 10:28
  • @R.S I have tried using window.location.replace() and the second way in that url http://stackoverflow.com/questions/5701031/how-do-i-pass-javascript-values-to-scriptlet-in-jsp Still not getting – user2365917 May 20 '13 at 06:06

0 Answers0