开发者

How to pass parameters in jsp page

开发者 https://www.devze.com 2023-03-10 13:33 出处:网络
I am calling a servlet with params window.location.href = \"/csm/csminfo.jsp?CFG_ID=\"+cfgid+\"&path=\"+path;

I am calling a servlet with params

window.location.href = "/csm/csminfo.jsp?CFG_ID="+cfgid+"&path="+path;

In the 开发者_开发知识库other csminfo on body load i am calling a function to retrieve these params

<body onload="getConfigDetails(<%= request.getParameter("CFG_ID") %>,<%= request.getParameter("path") %>)">

JS

function getConfigDetails(cfgid,path)
{
alert(cfgid+","+path);
}

But no alert gets popped up, what is the problem here?

I am using firefox, using error console i got this error

How to pass parameters in jsp page


You didn't quote the strings properly:

 <body onload="getConfigDetails('<%= request.getParameter("CFG_ID") %>','<%= request.getParameter("path") %>')">

Some other issues:

  1. When building the URL on the original page, you should make sure the parameter values are properly encoded by using the JavaScript built-in "encodeURIComponent()" function.
  2. JSP scriptlets are an old, ugly way of doing things, and really have no place in new code. You should look for resources to learn about JSTL:

    <body onload="getConfigDetails('${param.CFG_ID}','${param.path}')">
    
  3. Whether you use JSTL or scriptlets, values you pull from the HTTP parameters and inject into your page source should be run through an HTML escape mechanism. In JSTL, that'd look like this:

    <body onload="getConfigDetails('${fn:escapeXml(param.CFG_ID)}','${fn:escapeXml(param.path)}')">
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号