1

In the servlet:

 List<myItem> yourObjectToReturn = search.parserContent();
    request.setAttribute("yourObjectToReturn",yourObjectToReturn);

the array yourObjectToReturn consists 3 variable(id, txtfile, sentence) which you can see from the myItem class

public class myItem{
   String sentence;
   int id;
   String txtfile;

//   public myItem(){
//   }
   public int getId(){
       return id;
   }
   public void setId(int id){
       this.id = id;
   }
   public String getTxtfile(){
       return txtfile;
   }
   public void setTxtfile(String txtfile){
       this.txtfile = txtfile;
   }
   public String getSentence(){
       return sentence;
   }
   public void setSentence(String sentence){
      this.sentence = sentence;
   }

}

how to display the id, txtfile, sentence in the JSP separately? How to pass the arraylist from servlet to JSP .

the JSP : how to edit my JSP. I got error of my JSP:

type safety: unchecked cast from objectto arraylist

<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% List<myItem> myList = (ArrayList<myItem>) request.getAttribute("yourObjectToReturn"); %>

 The search Result SENTENCE IS: <%=myList %>  --%> 

</body>
</html>
Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
HiPownedBi
  • 195
  • 2
  • 8
  • 18

3 Answers3

2

Don't use scriptlets in your jsp page. Include the JSTL standard taglib via:

<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>

Then in your JSP use the iteration tag:

<c:forEach items="${requestScope.yourObjectToReturn}" var="current">
    <c:if test="${current.sentence== 'secret' }">
       <h1>seeeeeeeeeecret revealed</h1>
    </c:if>
</c:forEach>   

Where:

${requestScope.yourObjectToReturn} is your collection object.

And (during each iteration):

${current} is your actual element.

For further reference look http://docs.oracle.com/javaee/5/tutorial/doc/bnahq.html

And to avoid weird errors: don't forget to import myItem class (Should really be MyItem, thou...)

EDIT: Before digging deep into JSTL, I suggest you to take a reading of this other question. Particularly focus on the selected answer, it provides great insights.

Community
  • 1
  • 1
BigMike
  • 6,683
  • 1
  • 23
  • 24
0

To retrieve the Id and Txtfile from the List you need to iterate with using for example a for loop like:

   ...
   for (int i=0; i < myList.size(); i++) {
     %>
        <%=myList.get(i).getId()%>
        <%=myList.get(i).getTxtfile())%>

     <%}%>
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

You can use for loop and html together as follows:

<%
@SupressWarnings("unchecked")
List<mtItem> myList
for (MyItem myitem : myList)
{
%>
The search result is <%=myitem%>
<%
}
%>
Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
  • i have an error when i write "<% List myList = (ArrayList) request.getAttribute("yourObjectToReturn"); %>" in the jsp – HiPownedBi Oct 17 '16 at 07:31
  • type safety: unchecked cost from objectto arraylist – HiPownedBi Oct 17 '16 at 07:38
  • You cannot avoid this warning (except by suppressing it). The problem is that Java at runtime is only able to check that the object is of type Collection at runtime when casting. It cannot check that it is of type Collection. That is what the error means. Try suppressing it like in edit. – Shashwat Kumar Oct 17 '16 at 08:16