2008年(3500)
分类:
2008-05-04 20:15:02
建立带textboxbutton的form。注意我们不必定义form的action,因为使用了同一个页面。并注意textbox中要填入文件名字。
<% } %> jsp文件完成了。在仔细看以下Bean中的Java代码。我假设你们中的大多数都熟悉java,否则你怎么会加入JSP的行列。:-) **************JSP代码: TextFileReader.jsp TextFileReader.jsp Written by Martin Lindahl Copyright 1999, w3it.com, distributed by JSPea <%@ page import = "textfileaccess.TextFileReader" %>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) 下载本文示例代码