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