File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet reads text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet reads text file" Watch "Servlet reads text file" New topic

Servlet reads text file

Brooke Maddox
Ranch Hand

Joined: Nov 23, 2012
Posts: 42
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

Joined: May 26, 2003
Posts: 33117

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Brooke Maddox
Ranch Hand

Joined: Nov 23, 2012
Posts: 42
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

Joined: Nov 23, 2012
Posts: 42
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 {
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);

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] ));

Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2851

Now you put the inventory into the request that goes to JSP by using request.setAttribute().

|Asking Good Questions|
I agree. Here's the link:
subject: Servlet reads text file
jQuery in Action, 3rd edition