JSP reads text file

Source: Internet
Author: User
Tags empty header html page join readline reset rowcount
Js
With JSP source code (TEXTFILEREADER.JSP) and JavaBean (Textfilereader.java need to be compiled before use)

We used the earlier JSWDK, so we can be sure that you can also use the code directly.

Textfilereader.java is a bean, and textfilereader.jsp is a JSP file. If you also use D JSWDK and use the same library environment, you can call the bean file in the Textfileaccess directory under jswdk1-0eaexamplesjsp (you can create it), The JSP file is placed in the Jswdk1-0eaexamplesweb-infjspbeanstextfileaccess directory and you must also create it.

The JSP file we use does not contain much Java code, and the main code is placed in the bean. From this we can also see the basic relationship between JSP and JavaBean.
For experienced developers:

In header information, we want to declare which bean to use, identify, and set its properties.

First, we import the bean, and if your JSWDK is set up correctly and you have placed the file in the above location, then finding resource should be fine. The page command means that it will be imported for the entire JSP page.

<%@ page import = "textfileaccess." Textfilereader "%>

Tell the compiler that we will use a bean and how to identify it and initialize it (instansiate). Scope indicates that the declared object is valid for the current page.

<jsp:usebean id= "File_reader" class= "textfileaccess". Textfilereader "scope=" Session "/>

Then we decided to set those properties. This is "FileName". Because we want to use the bean's Setfilename method. So the bean's name must be contained.

<jsp:setproperty name= "File_reader" property= "FileName"/>

That's header information, and now we're going to start the actual HTML page.



<body bgcolor= "White" >

<font size=4>

Now we're going to start writing some Java scripts. First, check to see if the filename is set. If set, we will display the file, otherwise we will go to another page.

<%if (File_reader.getfilename ()!= "") {%>

File_reader is a bean, so we can use Java classes to access it. :-) now we get the file name!


The file name is: ' <% out.println (File_reader.getfilename ()); %> ':



The contents of the file, if empty:

<%if (file_reader.getcontent ()!= null) {%>

We can create a textarea (HTML) and use the GetRows () and GetColumns () methods to adjust to the appropriate location. Then put the contents of the file into.

<Form>

<textarearows=<%=file_reader.getrows ()%>cols=<%= file_reader.getcolumns ()%>id= textarea1name= textarea1></FONT>

<%out.println (File_reader.getcontent ()); %>

</TEXTAREA>

</Form>

If the file is empty, there must be an error and we will get an error message:

<%}else {%>

<% out.println (File_reader.geterrormessage ()); %>

<%}%>





Reset all values and return to the home page:

<% File_reader.reset (); %>

Do your want to <a href= "textfilereader.jsp" >look at another file</a>?

<%}else {%>

If the file name is empty, an error page is displayed.

Welcome to join here: ' Read a file in JSP '


This example simply displays the contents of the file in textarea?lt;p>

Please fill in what you want to see the file. And be sure to type the full path. <p>

Create a form with Textboxbutton. Note that we do not have to define action for form because the same page is used. And note the textbox to fill in the file name.

<form method=get></FONT>

FileName? <input type=text name= filename></font>

<input type=submit value= "Show it!" >

</form>

<%}%>

</font>

</body>


The JSP file is complete. Take a closer look at the Java code in the following bean. I assume that most of you are familiar with Java, otherwise you will join the ranks of JSP. :-)

JSP code: TEXTFILEREADER.JSP
<!--
textfilereader.jsp
Written by Martin Lindahl
Copyright 1999, w3it.com, distributed by Jspea
-->

<%@ page import = "textfileaccess." Textfilereader "%>

<jsp:usebean id= "File_reader" class= "textfileaccess". Textfilereader "scope=" Session "/>
<jsp:setproperty name= "File_reader" property= "FileName"/>

<body bgcolor= "White" >
<font size=4>

<% if (file_reader.getfilename ()!= "") {%>

The content of the file ' <% out.println (File_reader.getfilename ()); %> ':



<% if (file_reader.getcontent ()!= null) {%>

<Form>
<textarea rows=<%= file_reader.getrows ()%> cols=<%= file_reader.getcolumns ()%> id=textarea1 name= Textarea1>

<% out.println (File_reader.getcontent ()); %>

</TEXTAREA>
</Form>

<%} else {%>
<% out.println (File_reader.geterrormessage ()); %>

<%}%>





<% File_reader.reset (); %>
Do your want to <a href= "textfilereader.jsp" >look at another file</a>?


<%} else {%>

Welcome to the ' Read a file in JSP ' example.

The example simply shows the file in a textarea.<p>
Please fill out what file for you want. Be sure to type the complete path.<p>

<form method=get>
FileName? <input Type=text name=filename>
<input type=submit value= "Show it!" >
</form>

<%}%>

</font>
</body>


Java Bean Textfilereader.java
Package textfileaccess;

Import java.io.*;
Import java.awt.event.*;
Import java.util.*;

/**
* Textfilereader is a bean this 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 of 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, as if the file does not exists, and 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 a BufferedReader so we can read a in 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;
}
}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.