我的问题是这样的:
有一个servlet用于在tomcat启动时从数据库中取得一些数据,放在ServletContext中,tomcat启动时没有问题,问题在于,当我在启动后再一次调用这个servlet的时候,就抛出“空指针”的例外,不知道为什么。我的serlvet程序是这样的
public class InitCrWay extends HttpServlet{
public void init(){
this.initway();
} public void initway(){
String[] wayCodeList;
String[] wayNameList;
ServletContext ctx = null;
ctx = this.getServletContext(); CrccomcustBO wayBO = new CrccomcustBO();
ArrayList wayList = new ArrayList();
wayList = wayBO.searchWay("00","");
if(wayList!=null && wayList.size()!=0){
int count = wayList.size();
wayCodeList = new String[count];
wayNameList = new String[count];
for(int i=0;i<count;i++){
Crccomcust way = (Crccomcust)wayList.get(i);
wayCodeList[i] = way.getCkind();
wayNameList[i] = way.getCkindvalue();
} if((ctx.getAttribute("wayCodeList")!=null)||(ctx.getAttribute("wayNameList")!=null)){
ctx.removeAttribute("wayCodeList");
ctx.removeAttribute("wayNameList");
}
ctx.setAttribute("wayCodeList",wayCodeList);
ctx.setAttribute("wayNameList",wayNameList);
System.out.println("成功取得所属行业表!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
} }
}
启动后在别的类中调用该方法是
InitCrWay icw = new InitCrWay();
icw.initway();
有一个servlet用于在tomcat启动时从数据库中取得一些数据,放在ServletContext中,tomcat启动时没有问题,问题在于,当我在启动后再一次调用这个servlet的时候,就抛出“空指针”的例外,不知道为什么。我的serlvet程序是这样的
public class InitCrWay extends HttpServlet{
public void init(){
this.initway();
} public void initway(){
String[] wayCodeList;
String[] wayNameList;
ServletContext ctx = null;
ctx = this.getServletContext(); CrccomcustBO wayBO = new CrccomcustBO();
ArrayList wayList = new ArrayList();
wayList = wayBO.searchWay("00","");
if(wayList!=null && wayList.size()!=0){
int count = wayList.size();
wayCodeList = new String[count];
wayNameList = new String[count];
for(int i=0;i<count;i++){
Crccomcust way = (Crccomcust)wayList.get(i);
wayCodeList[i] = way.getCkind();
wayNameList[i] = way.getCkindvalue();
} if((ctx.getAttribute("wayCodeList")!=null)||(ctx.getAttribute("wayNameList")!=null)){
ctx.removeAttribute("wayCodeList");
ctx.removeAttribute("wayNameList");
}
ctx.setAttribute("wayCodeList",wayCodeList);
ctx.setAttribute("wayNameList",wayNameList);
System.out.println("成功取得所属行业表!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
} }
}
启动后在别的类中调用该方法是
InitCrWay icw = new InitCrWay();
icw.initway();
解决方案 »
- 问下SWT的LABEL垂直居中的问题
- 关于JAVA购物车,有一句代码一点都没看懂
- Point类的x,y属性==MouseEvent的getPoint方法 有关问题,请教.
- 输入一个数组,怎样画出坐标系,并且每相邻的几个点用不同颜色表示?(求源码)
- 内隐类为什么能调用外部类的private定义的属性
- 这个是个计算器的界面 可是我做的有错误 我调节不出来 希望哪位高手给我解决了 谢谢!~~~
- 关于在Java程序中显示数据的问题
- 如何向jbuilder工程增加一个目录
- 向前辈请教
- 关于播放声音文件!JMF
- JFC中的Struts&Spring布局管理器
- java.sql.SQLException ?? (http://community.csdn.net/Expert/TopicView3.asp?id=4588841)
java.lang.NullPointerException
at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:15
9)
at app.creditmanage.InitCrWay.initway(InitCrWay.java:23)
我是想在tomcat启动的时候从数据库取出一些数据,放在ServletContext中,这是可以的。问题是,当我需要的数据发生改变时,我想及时更新保存在ServletContext中的数据,请问我这样用ServletContext不可以吗?那应该怎么用呢?或者说我应该怎么实现我这个功能。因为这些数据是所用访问该应用都用到的,而且我不想每次用到都访问一回数据库,这样效率比较低。
private Singleton(){}
//注意这是private 只供内部调用
private static Singleton instance = new Singleton();
//这里提供了一个供外部访问本class的静态方法,可以直接访问
public static Singleton getInstance() {
return instance;
}
}
InitCrWay icw = new InitCrWay();
icw.initway();
应该在web.xml文件里配置的,不是new出来的。