aspose file tools*
The moose likes JSP and the fly likes A simple question, but hard for me. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "A simple question, but hard for me." Watch "A simple question, but hard for me." New topic
Author

A simple question, but hard for me.

Richard Phen
Ranch Hand

Joined: Apr 27, 2002
Posts: 52
Question:

There is a counter in my index.jsp. It reads and writes count.txt to count the visiter. I test it successfully with placing the count.txt in the root directory of jswdk1.0.1(\jswdk1.0.1\count.txt). If I want to change the location of count.txt,for example, \jswdk1.0.1\examples\count.txt, how to modify the code below?
<%@ page import="java.io.*"%>
<%
BufferedReader test;
test = new BufferedReader(new FileReader("count.txt"));
//directory: \jswdk1.0.1\count.txt, works OK!
//test = new BufferedReader(new FileReader(".\\count.txt"));
//Error:cannot find .\count.txt!
String tmp = null;
int i =0;
try
{
tmp = test.readLine();
}
catch(IOException e)
{
out.println("Error");
}
if(tmp==null)
{
i=0;
}
else
{
i=Integer.parseInt(tmp)+1;
}
try
{
PrintWriter outf = new PrintWriter(new FileOutputStream("count.txt"));
outf.println(i);
outf.close();
}
catch(IOException e)
{
out.println(e.getMessage());
}
test.close();
%>
Thanks a lot!!!


SCJP<br />SCJD
Sandeep. Oza
Ranch Hand

Joined: Feb 04, 2002
Posts: 66
Dear Rechard,
Write as:
test = new BufferedReader(new FileReader("../examples/count.txt"));
Good Day.
Sandeep Oza


Yes, Everything Is Possible
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12676
    
    5
Seems to me that this code:

relies on the JVM's current directory. You have no control over the current directory in a servlet/JSP engine.
Bill


Java Resources at www.wbrogden.com
Richard Phen
Ranch Hand

Joined: Apr 27, 2002
Posts: 52
Thanks Sandeep and William! It works now!
I have a new question about counter. I modified the jsp counter to javaBean and use it in jsp web page, I compile and run this javaBean well at Command Prompt, but I encountered a error that shows cannot find method setCount() when I use it in jsp page. Why? Could you help me fix it?
code of javabean:
package Counter;
import java.io.*;
public class Count
{
private BufferedReader test;
private String tmp = null;
private int i =0;
private PrintWriter outf;
private File file;

public Count()
{
file = new File(".\\count.txt");
}
public void setCount()
{
try
{
test = new BufferedReader(new FileReader(file));
tmp = test.readLine();
}
catch(IOException e)
{
System.out.println("error");
}
if(tmp==null)
{
i=0;
}
else
{
i=Integer.parseInt(tmp)+1;
}
try
{
outf = new PrintWriter(new FileOutputStream(file));
outf.println(i);
outf.close();
test.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
/**public static void main(String[] args)
{
Count c = new Count();
c.doCount();
}*/
}
code in jsp:
<jsp:useBean id="MyCount" scope="session" class="Counter.Count"/>
<% MyCount.setCount();%>
Thanks a lot!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A simple question, but hard for me.
 
Similar Threads
Problems using/making method!
A question about JavaBean?
servlet help me
How do you read a file in Servlet?
PrintWriter can only write 130k?