首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

怎么在servlet中使用多线程,求解?

2013-10-22 
如何在servlet中使用多线程,求解???要求如下:?在一个web Service项目中,使用servlet编程,如何防止Tomcat被

如何在servlet中使用多线程,求解???

要求如下:

?

在一个web Service项目中,使用servlet编程,如何防止Tomcat被非正常关闭而导致服务器端的servletContext数据丢失?

?

?

本人思路如下:

?

在该项目初始化时,启动一个线程,每隔一个小时就将servletContext的值存储到一个txt文件中核心代码如下:

?

public class ServletContextThread extends HttpServlet implements Runnable{

//servletContext的值已先在该工程的其他处设置好了,结构为(name:---:nums,value:---:0),该线程已在初始化时就启动了

??????? public void run() {

??
??????? }
????????public void init() throws ServletException {???
??????????????? FileWriter fw = null;
??????????????? BufferedWriter bw = null;
????????????????String path = this.getServletContext().getRealPath("recorder.txt");??
????????????????try {
??????????????????? ??while(true){
???????????????????????????? ??Thread.sleep(60*1000);????
???????????????????????????????String nums = this.getServletContext().getAttribute("nums").toString();??
??????????????????????????? ? ?fw = new FileWriter(path);
?????????????????????????????? bw = new BufferedWriter(fw);
???????????????????????????????bw.write(nums);
?????????????????????}
???
?????????????? } catch (InterruptedException e) {
?????????????????????? ??e.printStackTrace();
????????????? ?} catch (IOException e) {
??????????????????????? ?e.printStackTrace();
???????????????}finally{
??????????????????????????try {
????????????????????????????????? ??bw.close();
?????????????????????????????????????fw.close();
????????????????????????????} catch (IOException e) {
??????????????????????????????????????e.printStackTrace();
????????????????????????????}
??
????????????????}
??
?一直说}

?

但是它总是说?bw.close();处空指针异常,求解????????????????

2013-9-11 16:51:35 org.apache.catalina.core.ApplicationContext log
严重: Servlet servletContextBiz threw unload() exception
javax.servlet.ServletException: Servlet.destroy() for servlet servletContextBiz threw exception
?at org.apache.catalina.core.StandardWrapper.unload(StandardWrapper.java:1401)
?at org.apache.catalina.core.StandardWrapper.stop(StandardWrapper.java:1727)
?at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4488)
?at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1097)
?at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1203)
?at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
?at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
?at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1307)
?at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1571)
?at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1580)
?at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1560)
?at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
?at com.lx.biz.servletContextBiz.destroy(servletContextBiz.java:35)
?at org.apache.catalina.core.StandardWrapper.unload(StandardWrapper.java:1382)
?... 11 more
2013-9-11 16:51:36 org.apache.catalina.core.ApplicationContext log
严重: StandardWrapper.Throwable
java.lang.NullPointerException
?at com.lx.biz.ServletContextThread.init(ServletContextThread.java:45)
?at javax.servlet.GenericServlet.init(GenericServlet.java:212)
?at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
?at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
?at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4044)
?at org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
?at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1105)
?at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1203)
?at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
?at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
?at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1307)
?at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1571)
?at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1580)
?at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1560)
?at java.lang.Thread.run(Unknown Source)
2013-9-11 16:51:36 org.apache.catalina.core.StandardContext loadOnStartup
严重: Servlet /counter threw load() exception
java.lang.NullPointerException
?at com.lx.biz.ServletContextThread.init(ServletContextThread.java:45)
?at javax.servlet.GenericServlet.init(GenericServlet.java:212)
?at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
?at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
?at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4044)
?at org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
?at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1105)
?at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1203)
?at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
?at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
?at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1307)
?at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1571)
?at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1580)
?at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1560)
?at java.lang.Thread.run(Unknown Source)

?

热点排行