看一段代码,很多的疑问.
1 这个类extends HttpServlet 说明是1个Servlet
他到底和其他类有什不同
2BufferedInputStream bis = new BufferedInputStream(is);
Digester digester = new Digester();
package addressbook;这个2行代表什么意思?3 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;
}
这里synchronized 什么意思 , database = new Hashtable();---hashtable什么意思
if (debug----这个debug什么地方定义的?InputStream is = getServletContext().getResourceAsStream(pathname); ---这句又是什么意思?附后 详细代码:
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(); }
}
1 这个类extends HttpServlet 说明是1个Servlet
他到底和其他类有什不同
2BufferedInputStream bis = new BufferedInputStream(is);
Digester digester = new Digester();
package addressbook;这个2行代表什么意思?3 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;
}
这里synchronized 什么意思 , database = new Hashtable();---hashtable什么意思
if (debug----这个debug什么地方定义的?InputStream is = getServletContext().getResourceAsStream(pathname); ---这句又是什么意思?附后 详细代码:
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(); }
}
解决方案 »
- 请大神助,java多线程读写最简单问题,先谢了!
- jdbc 连接数据库的问题
- 为什么我写的applet程序无法在没装java runtime的浏览器上运行,急
- 很简单的问题,可就是不知道问题在哪里
- 关于 和Array (数组)?
- 如何定位jb中的光标,我的光标总和字符的位置不相符。
- C++ ----> JAVA ??
- 关于《java2核心技术卷I:基础知识》 这本书中例子在jcreator2.5运行的问题.
- 想各位大侠求教了,为什么我的jdbc驱动不支持absolute这个方法呢?
- 一个SCJP的问题,请解释一下~~~~~~~
- iframe和frameset得用法 不知道怎么实现?
- 两个小问题
2.Digester应该你你包中自定义的一个类。
3.synchronized 线程同步,也就是说,当一个访问这个方法的时候需要享有锁,当锁释放后其他线程才能访问。
4.返回web资源信息的InputStream
2synchronized关键字的作用域有二种:1)是某个对象实例内,synchronized aMethod(){}可以防止多个线程同时访问这个对象的synchronized方法(如果一个对象有多个synchronized方法,只要一个线程访问了其中的一个synchronized方法,其它线程不能同时访问这个对象中任何一个synchronized方法)。这时,不同的对象实例的synchronized方法是不相干扰的。也就是说,其它线程照样可以同时访问相同类的另一个对象实例中的synchronized方法;2)是某个类的范围,synchronized static aStaticMethod{}防止多个线程同时访问这个类中的synchronized static 方法。它可以对类的所有对象实例起作用。3)除了方法前用synchronized关键字,synchronized关键字还可以用于方法中的某个区块中,表示只对这个区块的资源实行互斥访问。用法是: synchronized(this){/*区块*/},它的作用域是当前对象;4)synchronized关键字是不能继承的,也就是说,基类的方法synchronized f(){} 在继承类中并不自动是synchronized f(){},而是变成了f(){}。继承类需要你显式的指定它的某个方法为synchronized方法;