0

I'm working on a project where I need to do a research field that will redirect me on a jsp page to display the results. I'm working without any database so i'm comparing the string the user write in the input to an array of strings.

i have a welcome.jsp like this:

<form name="form" method="post" action="http://localhost:8080/Miniproject/ResServlet">
   <label for="book">Search for a book</label><br>
  <input type="text" name="book" id="book">
  <br>

  <input type="submit" value="Chercher un livre" id="button_submit_book">
</form> 

My servlet ResServlet.java is:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String book = request.getParameter("book");

        ArrayList<String> resultat = Reservation.search(book);
         request.setAttribute("resultat",resultat);
         RequestDispatcher dispatcher=getServletContext().getRequestDispatcher("/view/books.jsp");
            dispatcher.include(request, response);



        doGet(request, response);
    }

My Reservation.java model is:

import java.util.ArrayList;

public class Reservation {
     public String book;

     public String getBook() {

            return this.book;

        }

     public void setBook( String book ) {

            this.book = book;

        }

     public static ArrayList<String> search( String book ) {
         String book1 = book.toLowerCase();
         String[] booksarray = {"De la guerre, Clausewitz","Les Misérables, Victor Hugo","Le Rouge et le noir, Stendhal","livre1","livre2","livre3","livre4"};
         ArrayList<String> result = new ArrayList<String>();
         for (String bookname: booksarray) {   
             String book2 = bookname.toLowerCase();
             if(book2.equals(book1)) {
                 result.add(bookname);
             }
            }

        return result;

         }


        }

i tried to do with equals but also with contains but nothing works

finally the books.jsp file is:

<h2>RESULT OF THE RESEARCH</h2>

<% String[] resultat = request.getParameterValues("resultat"); %><br>
<% out.println(resultat); %><br>

but in my books.jsp it always display 'null' i don't know why...

if one of you know why it doesn't work it would help me a lot, thanks you for your attention and time :D

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vaaroz
  • 13
  • 6
  • 2
    resultat is an **attribute** of type List. Why do you expect to find its elements in request.getParameterValues("resultat")? Parameters and attributes are not the same thing. Also, you should forward, not include, and there's no good reason to call doGet() from doPost(), and a search should be done with a GET, not a POST. – JB Nizet Apr 18 '18 at 17:47
  • ok thanks for your quick answer. a parameter is something that the user enter and an attribute is something set by the server so i changed my code to create and set an attribute in the session but when i try to call session.getAttribute("resultat") in my JSP there is a mismatch in type whereas my variable is ArrayList resultat so it's the same type :/ – Vaaroz Apr 18 '18 at 18:08
  • ok i tried with: ArrayList resultat= (ArrayList)session.getAttribute("resultat"); and it worked, thx – Vaaroz Apr 18 '18 at 18:14
  • Why put the attribute in the session? It should be set in the request, not in the session. – JB Nizet Apr 18 '18 at 18:51
  • i should do a request.setAttribute(..) ? and then getAttribute(...) in the JSP right ? – Vaaroz Apr 19 '18 at 14:49

1 Answers1

0

You've set your results in the request object, I haven't tested this but I would usually set the value in session as

session.setAttribute("resultat", resultat);

In your books.jsp then you can retrieve it as

<% ArrayList<String> resultat = (ArrayList<String>)(session.getAttribute("resultat")); %>

The same might work for the request object, it's worth testing this but you will need to use getAttribute(), not getParameterValues() as this was not a parameter.

Note that the attribute you set has the type ArrayList so you need to use the same type when you call getAttribute() (ArrayList is not the same as String[])

PhilDin
  • 2,802
  • 4
  • 23
  • 38
  • ok i tried this but i have an error like "cannot convert from object to arraylist, what i dont understand is that my 'resultat' attribute and not parameter this time is an arraylist so it should not create a problem of type here right ? – Vaaroz Apr 18 '18 at 18:05
  • The resultat value does have type ArrayList but Java doesn't know that, you have to tell it with a cast. I've modified the code to include the cast. – PhilDin Apr 18 '18 at 18:31
  • perfect, that's what i did :) thanks – Vaaroz Apr 19 '18 at 14:49