Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help in iterating array from jsp

 
Caleb Jhay
Greenhorn
Posts: 18
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I dont know where to post this question and I'm new in this forum. I have a problem iterating an array in jsp below is my code. I'm trying to iterate and write the value of a on my javascript and it doesnt work. Been doing this for almost 2 days and I'm kinda new to this stuff. I want to print the value of a using javascript so any help is pretty welcome.



Regards
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64626
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

There are a number of really big, really fundamental problems here.

Firstly, if you are just starting off, you are already a decade behind the times! It is no longer acceptable to put Java code and Java scriptlets in a JSP. That is a bad bad practice that has been discredited since 2002!

Also, you can't just mix server-side code and client-side JavaScript and expect them to mix. They're not running at the same time or even on the same system.

Do yourself a favor and learn to write JSP the correct and modern way with the JSTL and EL in place of discredited scriptlets.

With regards to understanding fundamentals, please read this article. When you a done with that one, this article will outline how to properly structure web applications.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64626
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of particular note, in the following line of code:you are apparently expecting some sort of magic to happen to transform a Java structure into a JavaScript structure. After reading the first article linked above, you'll see why this simply cannot work.

In fact, do a View Source in the browser to see what's being delivered to it. Does it make any sense. (Hint: answer is "no")

After reading the articles, you'll should understand the concept of "template text" and how to use it to set up the data such that it can be used from JavaScript.

And of course,using modern JSTL and EL in place of Java scriptlets.
 
Caleb Jhay
Greenhorn
Posts: 18
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Welcome to the Ranch!

There are a number of really big, really fundamental problems here.

Firstly, if you are just starting off, you are already a decade behind the times! It is no longer acceptable to put Java code and Java scriptlets in a JSP. That is a bad bad practice that has been discredited since 2002!

Also, you can't just mix server-side code and client-side JavaScript and expect them to mix. They're not running at the same time or even on the same system.

Do yourself a favor and learn to write JSP the correct and modern way with the JSTL and EL in place of discredited scriptlets.

With regards to understanding fundamentals, please read this article. When you a done with that one, this article will outline how to properly structure web applications.


Thank you for your reply. First of all this is not my code. This project was done few years ago and I was just asked to debug some problem. Second I'm not really a java programmer thats why I know nothing about this stuff. The thing is the existing jsp code is connected to database and it has a query that select records from the database. I was asked to pass some parameter in the query and that parameter will came from a text box in the jsp. I know it is possible by submitting the page and doing this transaction inside the controller or by ajax. but right now studying java and the project framework is not an option to me because time is the luxury that I dont have. The only solution that I'm thingking right now is to iterate a java array inside javascript.

thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64626
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you have time to read a few articles?
 
nithin chinni
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Caleb Jhay wrote:Hi guys I dont know where to post this question and I'm new in this forum. I have a problem iterating an array in jsp below is my code. I'm trying to iterate and write the value of a on my javascript and it doesnt work. Been doing this for almost 2 days and I'm kinda new to this stuff. I want to print the value of a using javascript so any help is pretty welcome.



Regards


Okay this should work fine.
<html>
<head>
<title>testing</title>
</head>
<body>
<table>
<%
String a[] = {"January", "Febuary", "March"};
for(i=0;i<3;i++)
{ %>
<tr><td><%=a[i]%></td></tr><%
}


%>
</body>
</html>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic