有谁知道Servlet里的init() throws ServletException初始化部分是不是只加载一次?以后处理请求时是不是就不被调用了?

chenchi 2006-03-01 02:23:31
有谁知道Servlet里的init() throws ServletException初始化部分是不是只加载一次?以后处理请求时是不是就不被调用了?

也就是说ProvisionProcess.java这个Servlet程序初始化时init() throws ServletException部分会被调用,以后我每次接受doGet或doPost请求时init() throws ServletException就不会再被调用了?

还有,如果不被调用的话,init() throws ServletException部分的内容什么时候又被释放呢?

有谁知道呀?



程序内容:
package com.ray.sms.misc;
/**
*
* <p>Title: </p>
* <p>Description: Misc同步处理</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author 陈驰
* @version 1.0
*/

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import com.ray.sms.misc.LondinfoDetl;

public class ProvisionProcess extends HttpServlet {
static final private String CONTENT_TYPE = "text/xml";
Queue queue = Queue.getInstance();
//Initialize global variables
public void init() throws ServletException {//Servlet初始化部分
CenterLoadGlobal.getInstance().initSystem();
ArrayList array_InfoData = CenterLoadGlobal.array_InfoDate;
for (int i = 0; i < array_InfoData.size(); i++) {
String mobile = null;
LondinfoDetl InfoDetl = (LondinfoDetl) array_InfoData.get(i);
mobile = InfoDetl.getLINFO_DestTermID();
System.out.println("第 "+i+ " 条");
}
System.out.println("Load InfoData done.........");

DealQueue dq = new DealQueue();
dq.start();
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}

//Process the HTTP Get request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
BufferedOutputStream bout = new BufferedOutputStream(response.getOutputStream());
MiscSystem misc = MiscSystem.getInstance();
misc.syslog.info("Request from Client: " + request.getRemoteAddr());
BufferedInputStream bin = null;
SyncOrder sync = null;
try{

bin = new BufferedInputStream(request.getInputStream());
int len = request.getContentLength();
byte[] tmp = new byte[len];
bin.read(tmp);
// trim
tmp = new String(tmp).trim().getBytes();




// String myName = ""; // 第三步:获取HTTP 请求信息
// java.util.Enumeration keys = request.getParameterNames();
// while (keys.hasMoreElements());
// {
// String key = (String) keys.nextElement();
// if (key.equalsIgnoreCase("send_cent"))
// myName = request.getParameter(key);
// }
// String xml_content = request.getParameter("send_cent");
// System.out.println(xml_content);






misc.syslog.info("Get XML : \n");
misc.syslog.info(new String(tmp));
misc.syslog.info("get SyncOrder : \n");

//将接收到的所有数据名写入单独的日志
misc.receiver_log.info("Get XML : \n");
misc.receiver_log.info(new String(tmp) + "\n\n\n");

try {
sync = MiscFunc.getSyncOrder(tmp);
}
catch (Exception ex) {
misc.syslog.error("001 : ",ex);
}
try {
//MiscFunc.doDBRecord(sync);//数据库处理
queue.add(sync);

}
catch (Exception ex) {
misc.syslog.error("002 : ",ex);
}
SyncOrderResp resp = new SyncOrderResp();
resp.setHRet("0");
resp.setTransactionId(sync.getTransactionId());
resp.setVersion("1.5.0");
misc.syslog.info(resp);
bout.write(resp.toString().getBytes("UTF-8"));
bout.flush();
//System.out.println("哈,哈,我成功了!!!!");
misc.syslog.info("-------------------<sync.getActionId()----->"+sync.getActionId());
misc.syslog.info("-------------------<sync.getKeyStr()----->"+sync.getKeyStr()+"<----------");
misc.syslog.info("-------------------<sync.getFeatureStr()----->"+sync.getFeatureStr()+"<----------");
try{
Thread.currentThread().sleep(5);
}catch(Exception ex){
misc.syslog.error("",ex);
}
//ch.close();
//模拟一个MO发送结束
// }catch(ProvisionException e){
// misc.syslog.error("",e);
// SyncOrderResp resp = new SyncOrderResp();
// resp.setHRet("" + e.getErrorCode());
// resp.setTransactionId(sync.getTransactionId());
// resp.setVersion("1.5.0");
// bout.write(resp.toString().getBytes("UTF-8"));
// bout.flush();
}
catch(Throwable t){
misc.syslog.error("",t);
SyncOrderResp resp = new SyncOrderResp();
resp.setHRet("" + 1);
resp.setTransactionId(sync.getTransactionId());
resp.setVersion("1.5.0");
bout.write(resp.toString().getBytes("UTF-8"));
bout.flush();
}finally{
try{
if(bout != null) bout.close();
if(bin != null) bin.close();
}catch(Exception E){}
}
}
//Clean up resources
public void destroy() {
}
}
...全文
405 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
interpb 2006-03-01
  • 打赏
  • 举报
回复
答案是肯定的只调用一次


init()里面的资源当然是destroy()方法里面释放

捏造的信仰 2006-03-01
  • 打赏
  • 举报
回复
楼主你自己在init()里面加上一句输出,运行一下看看不就知道了。
treeroot 2006-03-01
  • 打赏
  • 举报
回复
en!
killme2008 2006-03-01
  • 打赏
  • 举报
回复
我没理解错的话,答案是肯定的.
servlet在第一次初始化之后就一直存在(所谓servlet进程),每当用户请求就增加一个线程来处理,servlet进程的存在于web应用的整个生命周期.

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧