0

I'm a beginner in JavaEE. I do a simple example to send objects from servlet to jsp. In jsp, I want to display the valeurs of these objects on site page. I used ${ } to get the objects, but it doesn't work. Could anybody please help me? There are my code for test.java and test.jsp: test.java:

public class Test extends HttpServlet {
public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
    /* Création et initialisation du message. */
    String paramAuteur = request.getParameter( "auteur" );
    String message = "Transmission de variables : OK ! " + paramAuteur;

    /* Création du bean et initialisation de ses propriétés */
    Coyote premierBean = new Coyote();
    premierBean.setNom( "Coyote" );
    premierBean.setPrenom( "Wile E." );

    /* Création de la liste et insertion de quatre éléments */
    List<Integer> premiereListe = new ArrayList<Integer>();
    premiereListe.add( 27 );
    premiereListe.add( 12 );
    premiereListe.add( 138 );
    premiereListe.add( 6 );

    /* Stockage du message, du bean et de la liste dans l'objet request */
    request.setAttribute( "test", message );
    request.setAttribute( "coyote", premierBean );
    request.setAttribute( "liste", premiereListe );

    /* Transmission de la paire d'objets request/response à notre JSP */
    this.getServletContext().getRequestDispatcher( "/WebContent/test.jsp" ).forward( request, response );
}

}

jsp file:

    <p>Ceci est une page générée depuis une JSP.</p>
    <p>
        ${test}
        ${param.auteur}
    </p>
    <p>
        Récupération du bean :
        ${requestScope.coyote.prenom}
        ${requestScope.coyote.nom}
    </p>
    <p>
        Récupération de la liste :
        <%
        ArrayList<Integer> list = (ArrayList) request.getAttribute("liste");
        for (Integer lis: list) {   
           %>
             <%=lis%>
           <%}
        %>
    </p>

Thanks,

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
helenora
  • 1
  • 1
  • So, what happens when executing this code? You said you use the JSP EL (${...}), but I see nothing of the sort in the posted JSP code. – JB Nizet Oct 22 '14 at 19:45
  • Hi, I'm sorry, I tried to post all my jsp with HTML code. But it didn't work. In fact, I feel that my jsp couldn't receive the object from test.java. There was the error at line: for(Integer i:list), so I added if{...} to verify that. I saw that list is null. – helenora Oct 23 '14 at 12:54
  • Hi everyone, I found the problem. I couldn't send the objects from test.java to test.jsp bc I put test.jsp in the wrong place, it has to be found in WEB-INF. Sorry for your time. – helenora Oct 23 '14 at 14:44

2 Answers2

1

You could do something like this:

<p>
  <%
   ArrayList<Integer> lists = (ArrayList<Integer>) request.getAttribute("liste");
   for (Integer list: lists) {   
   %>
     <%=list%>
   <%}%>
</p>

With EL you can do the following:

<p>
  <c:forEach items="${liste}" var="list">
   ${list}
  </c:forEach>
</p>
Sas
  • 2,473
  • 6
  • 30
  • 47
  • It goes against modern JSP to inline raw Java code like this. Ideally JSP should be more declarative and the Java should be in beans prepared before the JSP runs. If you do it this way your Java code gets broken up and mixed in with the HTML. – Ashley Frieze Oct 22 '14 at 20:10
  • I know, I would not use scriptlets in my code. Since OP already wrote the code in scriptlet, I used scriptlets to illustrate how this can be done... :) – Sas Oct 22 '14 at 20:13
  • Hi Sas, I tried with your code, It didn't work. It said that there was the error at for(Integer list:lists) :( – helenora Oct 23 '14 at 13:03
0

Try the answer from here - How to access at request attributes in JSP? - your request attributes should be visible in JSP.

Community
  • 1
  • 1
Ashley Frieze
  • 4,993
  • 2
  • 29
  • 23