This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet reads text file" Watch "Servlet reads text file" New topic
Author

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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 {
@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

Joined: Jun 06, 2007
Posts: 2716
    
    6

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


SCJP, SCWCD.
|Asking Good Questions|
 
 
subject: Servlet reads text file
 
Similar Threads
checked values
JComboBox with DefaultComboBoxModel doesn't allow to add or remove
I want two buttons on a page, one for "search refine", one for "submit", how to do ?
itemcode to servlet
Opening a page with details about an item (servlet)