1

I have this code

<%=out.write("<input 
  type=\"hidden\" 
  id=\"tid\" 
  value=\""+request.getParameter("id").toString()+"
\"/>")%>
<script type="text/javascript">
  getPage(document.getElementById("tid").value)
</script>

This code creates a hidden field with the value came from

<site root>/viewPage.jsp?id=erwdf

url and pass the value of this hidden field to a jsp function. When I ran this code on Tomcat it gave an error as

The method print(boolean) in the type JspWriter is not applicable for the arguments (void)

on JSP code line I given above. So am I doing anything wrong or is there any alternative method to pass a GET parameter to a JavaScript function? I don't know much about Javascript just started to learn it.

Neysor
  • 3,893
  • 11
  • 34
  • 66
Aishwarya Shiva
  • 3,460
  • 15
  • 58
  • 107
  • 1
    Why so overcomplicated with a hidden field? Why not just `getPage('<%=request.getParameter("id")%>')`? – BalusC Mar 21 '12 at 15:01
  • thanks @BalusC that was amazing. Actually I don't have much experience in JavaScript. Now its giving value as `getPage('adsd');` so are single quoted data are treated as strings in JavaScript? – Aishwarya Shiva Mar 21 '12 at 15:09
  • Yes, otherwise it's treated as a global variable, but you don't have any JS variable with that name. – BalusC Mar 21 '12 at 15:12

3 Answers3

3

Why so overcomplicated with a hidden field?

Just do

getPage('<%=request.getParameter("id")%>');

Or easier, with EL

getPage('${param.id}');

You may only want to escape special JS characters by Apache Commons Lang StringEscapeUtils, otherwise the generated JS code may break whenever the parameter value contains a single quote or any other special JS character.

getPage('<%=StringEscapeUtils.escapeJavaScript(request.getParameter("id"))%>');

Or when in EL

getPage('${util:escapeJS(param.id)}');

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

I believe you meant <%out.write instead of <%=out.write

about the other issue from the comments, this will assist with getPage and perform escaping of quotes, other special chars...

<script type="text/javascript">
  getPage("<% try {
      out.write(URLEncoder.encode(request.getParameter("id").toString(), "UTF-8"));
    } catch (Exception e) {
    } %>")
</script>
dldnh
  • 8,923
  • 3
  • 40
  • 52
  • ya it worked thanks @dldnh but again my Javascript function getPage is not executing – Aishwarya Shiva Mar 21 '12 at 14:45
  • instead of putting the value into an `` and using `document.getElementById` you should just pass it directly to `getPage` -- `getPage("<% out.write(request.getParameter("id").toString()); %>")` -- probably need some escaping, too, inside the quotes – dldnh Mar 21 '12 at 15:06
  • don't u think it will pass the whole `"<% out.write(request.getParameter("id").toString()); %>"` to getPage()?? I only want to pass the id value to getPage() – Aishwarya Shiva Mar 21 '12 at 15:14
  • it will pass whatever's written by `out.write` -- I've edited my answer to show what I mean about the encoding, since you could have non-friendly characters in your request parameter that could corrupt the call to getPage. – dldnh Mar 21 '12 at 15:17
2

You don't have to store it in a hidden field to access it from js. You can read it from the documents location. I personally use a method like this to grab GET parameters from my url.

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

var id = getUrlVars()['id'];

Reinard
  • 3,624
  • 10
  • 43
  • 61
  • Thanks @Sir Troll your code is not giving error but when I passed getUrlVars()['id'] in my function getPage() its not executing getPage() – Aishwarya Shiva Mar 21 '12 at 14:44