×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Output truncated after including a servlet

Output truncated after including a servlet

Output truncated after including a servlet

(OP)
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="webapp/servlet/CSRandomQuote" flush="true" />

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
http://www.FatEarth.com
http://www.FatEarth.net

RE: Output truncated after including a servlet

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 :)

RE: Output truncated after including a servlet

(OP)
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

RE: Output truncated after including a servlet

hi

did your output file begin with <HTML>

and have a <BODY> tag ?

if not you've got a problem

manu0

RE: Output truncated after including a servlet

(OP)
Okay, just to clarify for everyone, here is a sample of what the page looks like:

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

<jsp:include page="webapp/servlet/CSRandomQuote" flush="true" />

<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.

RE: Output truncated after including a servlet

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 :)

RE: Output truncated after including a servlet

(OP)
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 "name" (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("text/html");
        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 "default.txt" file is used.
    String fileName = request.getParameter("fileName");
        if (fileName == null)
            fileName = "default.txt";

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

      //Now, we need to make sure that servlet users can ONLY access files in
    //the directory we want them too. So, and "..","/" or "\" 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("..") > 0  || fileName.indexOf("/") > 0 || fileName.indexOf("\\") > 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(""))
            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("<b>Error!</b> Could not load " + fileName + ". Please notify the webmaster.");

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

  private String pathToFiles = "coolservlets/files/quotes/";
  private Hashtable database;

RE: Output truncated after including a servlet

(OP)
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="webapp/servlet/CSRandomQuote" flush="true" />             
</p>                                                                         
<p>If this line is viewable than my issue with truncated data is solved.</p>
                
</body>                                                     </html>                                                                      

RE: Output truncated after including a servlet

I think you should not close the out ( out.close(); ) in this case.

Otto

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close