Thanks for your reply here is the code , I have tried many but nothing works. I am pasting all the codes and may be you can help me in it. I have already created a text file in the WEB-INF folder with the detail.
Thanks
Jsp
<form action=“AddProductServlet” method=“post” >
<table cellspacing=“5” border=“0”>
<tr>
<td align=“right”>Product Code:</td>
<td><input type=“text” name=“code”></td>
</tr>
<tr>
<td align=“right”>Product Description:</td>
<td><input type=“text” name=“description”></td>
</tr>
Java Classes: one for Product and one for Product IO
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package business;
import java.text.NumberFormat;
import java.io.Serializable;
public class Product implements Serializable
{
private String code;
private String description;
private double price;
public Product()
{
code = "";
description = "";
price = 0;
}
public Product(String code, String description, Double price)
{
this.code = code;
this.description = description;
this.price = price;
}
public void setCode(String code)
{
this.code = code;
}
public String getCode()
{
return code;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return description;
}
public void setPrice(double price)
{
this.price = price;
}
public double getPrice()
{
return price;
}
public String getPriceNumberFormat()
{
NumberFormat number = NumberFormat.getNumberInstance();
number.setMinimumFractionDigits(2);
if (price == 0)
return "";
else
return number.format(price);
}
public String getPriceCurrencyFormat()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
}
ProductIO which read the file and direct to the txt file
package data;
import java.io.;
import java.util.;
import business.*;
public class ProductIO
{
private static ArrayList<Product> products = null;
public static ArrayList<Product> getProducts(String path)
{
products = new ArrayList<Product>();
File file = new File(path);
try
{
BufferedReader in =
new BufferedReader(
new FileReader(file));
String line = in.readLine();
while (line != null)
{
StringTokenizer t = new StringTokenizer(line, "|");
if (t.countTokens() >= 3)
{
String code = t.nextToken();
String description = t.nextToken();
String priceAsString = t.nextToken();
double price = Double.parseDouble(priceAsString);
Product p = new Product();
p.setCode(code);
p.setDescription(description);
p.setPrice(price);
products.add(p);
}
line = in.readLine();
}
in.close();
return products;
}
catch(IOException e)
{
e.printStackTrace();
return null;
}
}
public static Product getProduct(String productCode, String path)
{
products = getProducts(path);
for (Product p : products)
{
if (productCode != null &&
productCode.equalsIgnoreCase(p.getCode()))
{
return p;
}
}
return null;
}
public static boolean exists(String productCode, String path)
{
products = getProducts(path);
for (Product p : products)
{
if (productCode != null &&
productCode.equalsIgnoreCase(p.getCode()))
{
return true;
}
}
return false;
}
private static void saveProducts(ArrayList<Product> products,
String path)
{
try
{
File file = new File(path);
PrintWriter out =
new PrintWriter(
new FileWriter(file));
for (Product p : products)
{
out.println(p.getCode() + "|"
+ p.getDescription() + "|"
+ p.getPrice());
}
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void insert(Product product, String path)
{
products = getProducts(path);
products.add(product);
saveProducts(products, path);
}
public static void update(Product product, String path)
{
products = getProducts(path);
for (int i = 0; i < products.size(); i++)
{
Product p = products.get(i);
if (product.getCode() != null &&
product.getCode().equalsIgnoreCase(p.getCode()))
{
products.set(i, product);
}
}
saveProducts(products, path);
}
public static void delete(Product product, String path)
{
products = getProducts(path);
for (int i = 0; i < products.size(); i++)
{
Product p = products.get(i);
if (product != null &&
product.getCode().equalsIgnoreCase(p.getCode()))
{
products.remove(i);
}
}
saveProducts(products, path);
}
}
My servlet Code
package AddProduct;
import java.io.;
import javax.servlet.;
import javax.servlet.http.*;
import business.Product;
import data.ProductIO;
public class AddProductServlet extends HttpServlet
{
/*
* @param request servlet request
* @param response servlet response
*/
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// get parameters from the request
String code = request.getParameter(“code”);
String description = request.getParameter(“description”);
double price = 0.0;
// get a relative file name
ServletContext sc = getServletContext();
String path = sc.getRealPath("/WEB-INF/products.txt");
// use regular Java objects to write the data to a file
Product product = new Product(code, description, price);
ProductIO.insert(product, path);
// send response to browser
response.setContentType("text/html;charset=UTF-8");
FileOutputStream fos = new FileOutputStream("/WEB-INF/products.txt");
PrintWriter pw = new PrintWriter(fos);
pw.println(""+code);
pw.println(""+description);
pw.close();
fos.close();
String url = "/edit_product.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
}