<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> 附有JSP源码(http://www.cn-java.com/www1/TextFileReader.jsp)及JavaBean (TextFileReader.java 使用前需加以编译) 我们使用了较早期的jswdk,所以我们可以确信你也可以直接使用这些代码。 TextFileReader.java是一个bean, http://www.cn-java.com/www1/TextFileReader.jsp则是jsp文件。如果你也使用d jswdk,并使用相同的library environment,可叫bean文件放在jswdk1-0eaexamplesjsp下的textfileaccess目录(你可以创建它),jsp文件放在jswdk1-0eaexamplesWeb-infjspbeanstextfileaccess目录,你也必须创建它。 我们使用的jsp文件并不包含太多的java代码,主要的代码放在bean中。由此我们也可以看到JSP和JavaBean的基本联系。 对于有经验的开发者: 在"header"信息中我们要申明要使用、识别哪一个bean,并设置其属性。 首先,我们导入bean,如果你的jswdk设置正确并已经将文件放在上述位置,那么找到 resource应该没有问题。page命令的意思是它将为整个jsp页面来进行导入。 <%@ page import ="textfileaccess.TextFileReader" %> 告诉编译器我们将使用一个bean,以及如何识别它,并进行初始化(instansiate)。 scope指明被申明的对象对当前页有效。
然后我们决定要设置那些属性。这里是"FileName"。因为我们要使用Bean的setFileName 方法。所以Bean的名字必须包含。
那就是header信息,现在我们开始实际的HTML页面。
Read a text file
现在我们开始编写一些Java脚本。首先检查文件名是否已经设置好。如果设好了,我们就显示文件,否则我们要转到另一个页面。 <%if(file_reader.getFileName() != "") { %> file_reader是一个bean,所以我们可以用Java类来存取它。 :-)现在我们得到文件名称!
文件名称是: '<% out.println(file_reader.getFileName()); %>' :
文件内容,如果为空的话: <%if (file_reader.getContent() != null) { %> 我们可以建立一个textarea (HTML) 并用getRows()和getColumns() 方法来调节到合适的位置。然后将文件内容放入。 <% } %> jsp文件完成了。在仔细看以下Bean中的Java代码。我假设你们中的大多数都熟悉java,否则你怎么会加入JSP的行列。:-) **************JSP代码: http://www.cn-java.com/www1/TextFileReader.jsp http://www.cn-java.com/www1/TextFileReader.jsp Written by Martin Lindahl Copyright 1999, w3it.com, distributed by JSPea <%@ page import = "textfileaccess.TextFileReader" %>Read a text file
<% if (file_reader.getFileName() != "") { %> The content of the file '<% out.println(file_reader.getFileName()); %>' :
<% if (file_reader.getContent() != null) { %> <% } else { %> <% out.println(file_reader.getErrorMessage()); %> <% } %>
<% file_reader.reset(); %> Do you want to look at another file? <% } else { %> Welcome to the 'Read a file in JSP' example.
The example simply shows the file in a textarea.
文件名称是: '<% out.println(file_reader.getFileName()); %>' :
文件内容,如果为空的话: <%if (file_reader.getContent() != null) { %> 我们可以建立一个textarea (HTML) 并用getRows()和getColumns() 方法来调节到合适的位置。然后将文件内容放入。 <% } %> jsp文件完成了。在仔细看以下Bean中的Java代码。我假设你们中的大多数都熟悉java,否则你怎么会加入JSP的行列。:-) **************JSP代码: http://www.cn-java.com/www1/TextFileReader.jsp http://www.cn-java.com/www1/TextFileReader.jsp Written by Martin Lindahl Copyright 1999, w3it.com, distributed by JSPea <%@ page import = "textfileaccess.TextFileReader" %>
<% if (file_reader.getContent() != null) { %> <% } else { %> <% out.println(file_reader.getErrorMessage()); %> <% } %>
<% file_reader.reset(); %> Do you want to look at another file? <% } else { %> Welcome to the 'Read a file in JSP' example.
The example simply shows the file in a textarea.
Please fill out what file you want to look at. Be sure to type the complete path.
<% } %> **************Java Bean TextFileReader.java package textfileaccess; import java.io.*; import java.awt.event.*; import java.util.*; /** * TextFileReader is a bean that provides the basic functionality for * reading a textfile. */ public class TextFileReader { private String fileName, errorMessage; private int columns, rowCount; /** * Constructs a TextFileReader. */ public TextFileReader() { reset(); } /** * Resets all the variables in this bean. */ public void reset() { fileName = ""; errorMessage = ""; columns = 0; rowCount = 0; } /** * Sets the error message, if an error occurs. */ public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } /** * Returns the error message, if any. */ public String getErrorMessage() { return errorMessage; } /** * Returns the filename. */ public String getFileName() { return fileName; } /** * Sets the filename. */ public void setFileName(String fileName) { this.fileName = fileName; } /** * Returns the amount of rows in the file. */ public int getRows() { return rowCount; } /** * Returns the maximum amount of columns in a row. */ public int getColumns() { return columns; } /** * Returns the content of the file in a String. * If an error occurs, like if the file does not exists, null is returned. */ public String getContent() { String content = ""; File file = new File(fileName); if (!file.exists()) { setErrorMessage("Error: The file '" fileName "' does not exists."); return null; } else if (file != null) { try { // Create an BufferedReader so we can read a line at the time. BufferedReader reader = new BufferedReader(new FileReader(file)); String inLine = reader.readLine(); while (inLine != null) { if (inLine.length() 1 > columns) columns = inLine.length() 1; content = (inLine System.getProperty("line.separator")); inLine = reader.readLine(); rowCount ; } return content; } catch (IOException e) { setErrorMessage("Error reading the file: " e.getMessage()); return null; } } else { setErrorMessage("Unknown error!"); return null; } } } (原文来自jsp-interest.com)