Search code examples
jspscriptlet

jsp out.write scriptlet tag within scriptlet


how do i out.write scriptlet tag when the out.write is already in scriptlet tag?

    <% if (answerList.size() > 2){
                            int toPrint = answerList.size() - 2;
                            int init = 2;

                            for (int x = 0; x < toPrint; x++){
                                out.write("<td width='30%' class='label' nowrap='nowrap'><label></label></td>");
                                out.write("<td><input type='checkbox' name='to_delete" + (init+1) + "' id='to_delete" + (init+1) + "' value='to_delete'> &nbsp &nbsp &nbsp ");
                                out.write("<input type='text' name='answer' id='answer' class='mlselect'  maxlength='200' size='90' value='<%=((SurveyAnswerBean)answerList.get(answerList.size()-"+(init+1) +")).getAnswerText()%>'> &nbsp ");
                             }
                            System.out.println("Answer List more than 2"); 
                            System.out.println("To print: " + toPrint);
                        }%>

i have tried but it has tag error, any help will be appreciated!


Solution

  • The answer is that you cannot nest scriplets tags, and it also doesn't make sense to do it (becuase you can't evaluate server-side variables on the client-side).

    You should replace:

    "...value='<%=((SurveyAnswerBean)answerList.get(answerList.size()-"+(init+1) +")).getAnswerText()%>'...
    

    with:

    "...value='" + ((SurveyAnswerBean)answerList.get(answerList.size()-(init+1))).getAnswerText() +"'...