开发者

txt file can not be created in java class in jsp

开发者 https://www.devze.com 2023-03-18 03:51 出处:网络
I am using tomcat 6 ,eclipse ee , win xp and I am working on a JSP project. In this proect , there is an operative class named \"DummyClass\" and its mission is to read a text file (input.txt) and cr

I am using tomcat 6 ,eclipse ee , win xp and I am working on a JSP project. In this proect , there is an operative class named "DummyClass" and its mission is to read a text file (input.txt) and create objects according to it. Then I've made an queries over it in the JSP file .

There is a Web-Content folder and the jsp file (NewPage.jsp) and its contents 开发者_JS百科are below:

<jsp:useBean id="user" class="actual.Searcheduser" scope="session"/> 
<jsp:useBean id="mydummy" class="dummyCreator.DummyClass" scope="session"/>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TSBS Telefon Rehberi Sonuç Sayfası</title>
</head>
<body>

<%
 System.out.println("kagdfcg");
 mydummy.readFile(input.txt);  //Line 1
 mydummy.fileCreator();        //Line 2

 %>
</body>
</html>

The code raise error in Line 1, it can not find the "input.txt" in the project. I copy it everywhere on the folder, but it can not see the "input.txt". In order tofind the correct place, I've created the Line 2, it creates a sample "sample.txt" but no file has created.

How can I overcome this?


First of all this isn't way yo play with jsp/servlet.. use Servlet to perform the logic. use jsp just to present view.

And in order to read text file ... you need to write it to accessible place , add more info on dir structure where is it ?


try

 mydummy.readFile("input.txt");  //Line 1
 mydummy.fileCreator();        //Line 2

a string is marked with quotation sign at the begin and the end


from the java tutorial on Files

Relative or Absolute?

A path is either relative or absolute. An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.

A relative path needs to be combined with another path in order to access a file. For example, joe/foo is a relative path. Without more information, a program cannot reliably locate the joe/foo directory in the file system. read the entire tutorial at : http://download.oracle.com/javase/tutorial/essential/io/path.html

In your example you are using relative files so thats probably a reason why you cant find it. Guessing you installed your tomcat on your computer and are not using some remote instance its easy to store files in locations like c:\tmp or similar, easy to find easy to clean.


File reading / business logic is a job for the front controller (say a servlet). Never use scriplets in the JSP

Any beans you attach on the JSP should be meant for presentation logic


It is more feasible to write all the business logic in servlet rather than a jsp file.

These codes can help you

<%@page import="java.io.*"%>
java.net.URL url =config.getServletContext().getResource("file.txt");
BufferedReader buffreader =new BufferedReader(new InputStreamReader(url.openStream()));

OR

<%
BufferedReader input = new BufferedReader(new FileReader("file.txt"));
String line = "";
while ((line = input.readLine()) != null) {
   out.println(line);
}
output.flush();
input.close();
%> 
0

精彩评论

暂无评论...
验证码 换一张
取 消