Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output truncated after including a servlet

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello All:

I am using the following code in include the output of a servlet into a jsp page. The servlet reads in a random line from a text file and outputs it.

<jsp:include page=&quot;webapp/servlet/CSRandomQuote&quot; flush=&quot;true&quot; />

Everything after this line is deleted in the output. I even tried including this line in one file and then including that file in another file and it still killed off everything after the servlet output its data. Any idea why this would be happening?

FYI in case you are wondering whether there is some strange character in the text file the servlet reads from that is causing this, I think I have ruled this out. The servlet was free on the web and it included a demo text file. When I used the demo text file instead of my own, I still have the same issue.

Thanks in advance for any help anyone can give me with this.

Chris Stringer
Chief Technology Officer
FatEarth
 
Hi,

Did your servlet end your html codes? Like did you print to your html codes in your servlet </html>?

Hope this helps,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
No, it didn't end the html code. There were several lines of text as well as closing body and html tags following the servlet. None of these show up in the browser though. If I view the source of the page in my browser, the last thing it shows is the output of the servlet.

-Chris
 
hi

did your output file begin with <HTML>

and have a <BODY> tag ?

if not you've got a problem :)
manu0
 
Okay, just to clarify for everyone, here is a sample of what the page looks like:

------
<html>
<body>

<jsp:include page=&quot;webapp/servlet/CSRandomQuote&quot; flush=&quot;true&quot; />

<p>If this line prints out than I am not having this problem.</p>

</body>
</html>
------

The output ends with the servlet output. The text after the output as well as the closing body and html tags don't show up.
 
Hi Chris,

Perhaps you would like to display your servlet codes as well?

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
As reguested, here are the two servlets that are relevant to the include issue I'm having:

------This servlet reads in the data from the text file-----

package com.coolservlets.randomquote;

import java.util.Vector;
import java.io.*;

public class QuoteCollection {

/**
* An object that takes in a filename, and creates a String array out
* of the text from the file.
*/
public QuoteCollection(String newKey, String [] newQuotes) {
//The &quot;name&quot; (key) of the object is the file name used to make it.
key = newKey;
quotes = newQuotes;
}
/**
* Returns the key for this quote collection.
*/
public String getKey() {
return key;
}
/**
* Gets a random quote out of the data.
*/
public String retrieveQuote() {
return quotes[(int)(Math.random()*100)%quotes.length];
}

private String key;
private String[] quotes;

-------This servlet actually outputs the data-------

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Vector;
import java.util.Hashtable;
import com.coolservlets.randomquote.QuoteCollection;

/**
* The CSRandomQuote servlet.

*

* It will display a random quote from a named collection of quotes. Collections

* are stored in text files with one quote per line in the text file. When

* using the servlet, simply give the name of the text file you would like to

* use and the servlet will do the rest.

*

* To make the servlet MUCH faster, this version includes caching of each quote

* collection. This means that the text file only has to be read once and then

* is stored in memory for future accesses. The only drawback to this method

* is that changes in the text file will not be seen until the servlet is

* restarted or if you give the reload command. See the servlet documentation

* for ways to deal with this issue.

*

* Servlet paramaters:

* fileName - value: name of file

* This is the name of the file to pull quotes from

* reload - value: true

* Forces the servlet to reload the file from disk before displaying a quote

*

* @authors Arend Miller & Matt Tucker

* @version 1.1

*/
public class CSRandomQuote extends HttpServlet {
/**
* Initializes the servlet variables.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
database = new Hashtable();
}
/**
* Processes the HTTP get request. As you can see, all it does is call
* the doPost function.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
/**
* Ahh, the HTTP post request. The meat and bones of the whole servlet.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType(&quot;text/html&quot;);
PrintWriter out = response.getWriter();

//A flag that will indicate the program was unable to load the
//specified quote file or if there is some other error.
boolean error = false;

// A user can specify any text file to read as a parameter.
// If no specific file is requested, the &quot;default.txt&quot; file is used.
String fileName = request.getParameter(&quot;fileName&quot;);
if (fileName == null)
fileName = &quot;default.txt&quot;;

//Check to see
boolean reload = false;
String reloadText = request.getParameter(&quot;reload&quot;);
if (reloadText != null && reloadText.equals(&quot;true&quot;))
reload = true;

//Now, we need to make sure that servlet users can ONLY access files in
//the directory we want them too. So, and &quot;..&quot;,&quot;/&quot; or &quot;\&quot; characters mean
//we'll reject the string and print an error. If there's some other
//way to bypass security, let us know. Basically, we don't want anyone
//printing out random lines of /etc/passwd :)
if (fileName.indexOf(&quot;..&quot;) > 0 || fileName.indexOf(&quot;/&quot;) > 0 || fileName.indexOf(&quot;\\&quot;) > 0)
error = true;

//New in v1.1 : Caching! Checks to see whether the file is in in memory
//(hashtable). If not, it puts the textfile into a QuoteCollection object
//and adds it to the database.
if(!database.containsKey(fileName) || reload) {
try {
//We'll read in the text file from disk and put the quotes into a new
//quote collection.
Vector tempVector = new Vector();
BufferedReader in = new BufferedReader(new FileReader(pathToFiles + fileName));
String aLine;
while ((aLine = in.readLine()) != null)
//Blank lines are ignored
if (!aLine.equals(&quot;&quot;))
tempVector.addElement(aLine);
//done reading lines into a vector so close file.
in.close();
//Copy vector contents into a String array
String [] quotes = new String[tempVector.size()];
tempVector.copyInto(quotes);
//Construct the quote collection using the data and put the collection
//into the database.
QuoteCollection newCollection = new QuoteCollection(fileName,quotes);
database.put(fileName, newCollection);
}
catch (Exception e) { error = true; }
}

//Now, if there were no errors, pull specified collection from the
//hashtable and print a random quote.
if (!error) {
//Now find a random element in the array and print it out.
String rand = ((QuoteCollection)database.get(fileName)).retrieveQuote();
out.println(rand);
}
else
//There was some error so print out an error message.
out.println(&quot;<b>Error!</b> Could not load &quot; + fileName + &quot;. Please notify the webmaster.&quot;);

//All done so close the stream
out.close();
}
/**
* Returns Servlet information
*/
public String getServletInfo() {
return &quot;CSRandomQuote 1.1 - CoolServlets.com&quot;;
}

private String pathToFiles = &quot;coolservlets/files/quotes/&quot;;
private Hashtable database;
 
One final bit of info for anyone who is interested:

Here is the actual code of the page where I am attempting to include the file. Note that nothing appears after the servlet outputs its line of text.

--------
<html> <head>
<title>Testing My Include</title>
<body>
<p> <jsp:include page=&quot;webapp/servlet/CSRandomQuote&quot; flush=&quot;true&quot; />
</p>
<p>If this line is viewable than my issue with truncated data is solved.</p>

</body> </html>
 
I think you should not close the out ( out.close(); ) in this case.

Otto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top