Home
JAVA SERVLET
Creating Dynamic Counter using File
Here is an example code to implement the universal counter
import java.io.*; import javax.servlet.http.*; import javax.servlet.*; public class fire extends HttpServlet { String prev; int ctr; byte[] buf; byte[] bufw; FileOutputStream fos; FileInputStream fis; public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException { PrintWriter pw=res.getWriter(); try{ fis=new FileInputStream("ctr.dat"); }catch(FileNotFoundException fis){} buf=new byte[(int)fis.available()]; fis.read(buf); prev=new String(buf); ctr = Integer.parseInt(prev); ctr=ctr+1; prev=String.valueOf(ctr); pw.println(ctr); fis.close(); fos=new FileOutputStream("ctr.dat"); bufw=new byte[prev.length()]; bufw=prev.getBytes(); fos.write(bufw); fos.close(); pw.close(); } }
Previous
Next