• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet reads text file

 
Brooke Maddox
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a project where I have to implement Model 2 architecture. One of the requirements is to have to have a flat text file that holds some inventory items. Accessing the system, an authorized user will be able to see the list of items ( in the ItemsCatalog page) and get the detailed information about each of them when selecting a specific item in that list ( the detail data will be presented in the ItemDetails page, where a user will be redirected when making a specific item selection from the list ).

So far I have a Servlet that I will use to read the text file, but my question is what to do with the items once they are read. I thought I should save them in a bean but I really don't know how to do that.

Can anyone help me.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

You are on the right track. You do need to store the data you read in a bean and then do request.setAttribute() with it so you can read it in your JSP.

A bean is just a Java class. Can you think of how to create a Java class that represents an item in the List? Then you can create an ArrayList of these for storing in the request.
 
Brooke Maddox
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the bean set up, and I believe I have my servlet reading the file and setting the items to an ArrayList. Now I see you said to use request.setAttribute(), which I assume is how I store each item from the array to the bean. Again I am not sure on how I go about doing this.

I really appreciate the help,
 
Brooke Maddox
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code for my Servlet. I am still stuck on how to save the items to the bean


public class NewServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
}

public void processRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {

}

public void doGet(HttpServletRequest req, HttpServletResponse response)
throws IOException, ServletException
{
ArrayList<testBean> inventory =new ArrayList<testBean>();

PrintWriter out = response.getWriter();

ServletContext context = getServletContext();

String fileName = "/WEB-INF/folder/text";
InputStream ins = context.getResourceAsStream(fileName);

if(ins!=null){
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader reader = new BufferedReader (isr);

String text = null;

while ((text = reader.readLine())!= null){
String[] eachItem = text.split(";"); // split into a string array.
inventory.add(new testBean(eachItem[0], eachItem[1], eachItem[2], eachItem[3], eachItem[4] ));

response.sendRedirect("display.jsp");
}
reader.close();
}
}
}
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you put the inventory into the request that goes to JSP by using request.setAttribute().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic