0

I have an arraylist that is declared and populated in a JSP file:

<% for(int i=0; i< GDI.getRow(); i++){
    associatedLines[i] = GDI.getRow().get(i).getNumplanindex;
}

ArrayList<Integer> availableLines = new ArrayList<Integer>();
for(int i=0; i<associatedLines.length; i++){
    if(associatedLines[i] == null){
         availableLines.add(i);
    }
}

%>

I would like to use the contents of availableLines in the dropdown list and be able to store the value selected to be used somewhere else.

I am almost certain that I need to use JSTL but I am not sure how to do it.

Hopefully somebody can help. Thanks!

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
laitha0
  • 4,148
  • 11
  • 33
  • 49
  • 2
    Why is this code in a JSP? It should be in Java. The list should be exposed as a request attribute then iterated over using ``, for example. What's the specific issue? – Dave Newton Jun 24 '13 at 14:26
  • The code is in jsp because the contents of it depends on whats in teh session, I did not create a sepearate class just to populate one arraylist. I was able to populate teh drop down menu with the values, but I need to store whatever is selected in some variable and pass it to a java script, I cant get this part right – laitha0 Jun 24 '13 at 14:28
  • 2
    That the session is involved is immaterial. There is almost never any reason to write Java code in a JSP to, and multiple reasons *not* to: view pages should be backed by servlets. Passing "what is selected" to JS is a matter of either (a) emitting valid JS built by your JSP compilation (meh) or (b) using any of several tricks to avoid having to do so, e.g., putting data in hidden divs, etc. – Dave Newton Jun 24 '13 at 14:35
  • makes sense, this is my first exposure to JSP and I am trying to minimize the code inside the JSP files, but I did not think there is a need to create a class and a servlet jurt for that – laitha0 Jun 24 '13 at 14:39
  • Can you provide me with a good tutorial on how to avoid java code inside JSP files? Thanks – laitha0 Jun 24 '13 at 14:41
  • Just don't put it there. Without using a framework or the mapping annotations now available it's a bit of a pain because you're basically creating your own framework. If going full-in to a real framework, or using current Java EE isn't a go, check out [Front Man](http://www.bibeault.org/frontman/), a really simple, light-weight, almost-non-existent framework that can help ease the conceptual transition to best practices and full-on frameworks. – Dave Newton Jun 24 '13 at 14:45

1 Answers1

4

You can add the bellow line just after the for loop in your scriptlet:

request.setAttribute("availableLines", availableLines);

And then you can use the availableLines variable in your drop down list using JSTL like bellow:

 <select>
    <c:forEach var="line" items="${availableLines}">
        <option><c:out value="${line}"/></option>
    </c:forEach>
 </select>

I think, this answers your original question.

Edit:

But one thing you should know that, writing scriptlet is deprecated for years! So it is advised to move your scriptlet code to your corresponding servlet. Here is a step by step tutorial for Servlet and jsp for beginners.

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
  • How can I use this value to pass it to a javaScript. I have a button next to it: . but this doe not seem to work – laitha0 Jun 24 '13 at 14:54
  • `` should work! – Sazzadur Rahaman Jun 24 '13 at 15:08
  • I am moving all my java code to servlets following your tutorial thanks alot for the reference. The above code for "insert" is not working, it is printing 0 and the debugger is reading the code as : it is ignoring the ${line} for some reason?!! – laitha0 Jun 24 '13 at 15:21
  • yes it is reading it fine in the select statement and I did it exactly as you gave it to me in your first post. but it is completley ignoring it inside the onclick statement – laitha0 Jun 24 '13 at 15:26
  • to get `line` varrable in your button, your `button` should be inside the foreach tag! If its not, you can only access `availableLines` in your button's onclick method like above! – Sazzadur Rahaman Jun 24 '13 at 15:29
  • I can't put it inside teh foreach tag, the button wont show. and inside the select statement eclipse complains that it is not in the right place!! – laitha0 Jun 24 '13 at 15:36
  • In any case, its not possible to access `line` in your `onclick`!!! Anyways I have edited my answer! – Sazzadur Rahaman Jun 24 '13 at 15:37
  • is there a workaround you know of to get this value?! there has to be a way to pass it to eh function! – laitha0 Jun 24 '13 at 15:42
  • 1
    If you want something like [this question](http://stackoverflow.com/questions/8832375/how-to-get-selected-value-from-dropdown-list-in-javascript), you can see the javascript portion of the answer! – Sazzadur Rahaman Jun 24 '13 at 15:49