⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userdatabaseservlet.java

📁 Struts入门实例:通讯录 addressbook
💻 JAVA
字号:
package addressbook;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.MissingResourceException;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.digester.Digester;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.util.MessageResources;
import addressbook.model.UserBean;
/**
 * <strong>UserDatabaseServlet</strong> is used to read username/password information
 * to an XML file.
 *<p>
 * The Addressbook sample application uses this file for demonstration purposes by reading the database
 * file from <code> /WEB-INF/userdatabase.xml </code>. In a production
 * application you would not want to store username/password information in a clear text file. More
 * likely, you would have this information contained within a database for a lookup at logon time.
 */
public final class UserDatabaseServlet
    extends HttpServlet {

    private Hashtable database = null;

    private int debug = 0;

    private String pathname = "/WEB-INF/userdatabase.xml";

    public void destroy() {

	getServletContext().removeAttribute(Constants.DATABASE_KEY);

    }

    public int getDebug() {
		return (this.debug);
    }

    public void addUser(UserBean user){
        database.put(user.getUserName(),user);
    }
   public void init() throws ServletException {

	String value;
	value = getServletConfig().getInitParameter("debug");
	try {
	    debug = Integer.parseInt(value);
	} catch (Throwable t) {
	    debug = 0;
	}
	if (debug >= 1)
	    log("Initializing database servlet");
        value = getServletConfig().getInitParameter("pathname");
        if (value != null)
            pathname = value;

	try {
	/* Try and load the database.xml file. If we have loaded,
	 * store the contents into our context so that we can use it
	 * to compare against for user logons.
	 */
	    load();
	    getServletContext().setAttribute(Constants.DATABASE_KEY,
					     database);
	} catch (Exception e) {
	    log("Database load exception", e);
	    throw new UnavailableException
		("Cannot load database from '" + pathname + "'"+e.getMessage());
	}

    }


    /**
     * Load the current database.xml file. Using the <code>Digester<code>
     * @see org.apache.commons.digester.Digester
     */


    private synchronized void load() throws Exception {


	database = new Hashtable();


	if (debug >= 1) log("Loading database from '" + pathname + "'");
   	InputStream is = getServletContext().getResourceAsStream(pathname);
   	if (is == null) {
       log("No such resource available - loading empty database");
       return;
   	}
	BufferedInputStream bis = new BufferedInputStream(is);
	Digester digester = new Digester();
	digester.push(this);
	digester.setDebug(debug);
	digester.setValidating(false);
	digester.addObjectCreate("database/user", "addressbook.model.UserBean");
	digester.addSetProperties("database/user");
	digester.addSetNext("database/user", "addUser");

	digester.parse(bis);
	bis.close();

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -