Tutorials Scripts Free Q&A Books


 







:שמתשמ
:המסיס
  

1 :םויה תוסינכ
48 :שדוחה תוסינכ
1 :ןיילנוא םישלוג
רחא PHP CGI ASP JavaScript Cold Fusion JSP

JSPב םיטפירקס


ילמודנר טסקט
םיזיע בלוח :רבחמה םש
http://www.builder.co.il :תיב-רתא

:תורעה\תויחנה
ןפואב גצויש םיצור םתאש טסקטה םע ץבוקל טסקטה ץבוק תא ונש
..השדח הרושב טסקט לכ .ילמודנר

> SERVLET CODE=CSRandomQuote>
> PARAM name="fileName" value="ChangeMe.txt">


:דוק

File 1
####################################

/**
* CSRandomQuote.java
* Arend Miller & Matt Tucker
* CoolServlets.com
* May 16, 1999
* Version 1.1
*
* Any errors or feature requests can be reported on CoolServlets.com.
* We hope you enjoy this program!
*
* Copyright (C) 1999 Arend Miller & Matt Tucker
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

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;
}

File 2
################################
/**
* QuoteCollection.java
* Arend Miller & Matt Tucker
* CoolServlets.com
* May 16, 1999
* Version 1.1
*
* Copyright (C) 1999 Arend Miller & Matt Tucker
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
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;
}

 

הרזח >>
...