java 将数组传给 jsp,如何用jsp Java数组传递到JavaScript数组?

I have a list of strings on my server which I am trying to get to the client in the form of an array. The code I am attempting to use is the following:

Within the jsp I have a List column

I am attempting the following code:

for(var i = 0; i < ; i++)

{

colArray[i] = "";

}

This code simply returns the first element in the columns list for every element in the colArray.

I have also tried:

colArray = ;

which does not work either.

I feel like I am making a little mistake somewhere and am just not seeing it. Is what I am trying to do possible in the way that I am attempting?

Thanks.

解决方案

You're getting the JSP code that is executed on the server mixed up with the JavaScript code that's executed on the client. The snippet is executed once, on the server, and the JavaScript loop around it is irrelevant at this point. When it arrives the the client, the loop's body just says colArray[i] = "first entry"; which of course puts the same string into every element of the array.

What you need to do instead is to have a loop execute on the server, like this:

colArray[] = "";

My JSP skills are rusty, and the syntax may be different, but I hope you get the idea.

Edit: As was pointed out in the comments, you need to be VERY careful about escaping anything in those Strings that could cause them to be interpreted as JavaScript code (most prominently quotation marks) - especially if they contain user-generated content. Otherwise you're leaving your app wide open to Cross-site scripting and Cross-site request forgery attacks.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值