免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6385 | 回复: 5

Tomcat中的Filter问题相关 [复制链接]

论坛徽章:
0
发表于 2003-11-13 11:58 |显示全部楼层
Tomcat  中的某一应用运行时, Filter是只有一个实例吗?

论坛徽章:
0
发表于 2003-11-13 15:30 |显示全部楼层

Tomcat中的Filter问题相关

是的。

论坛徽章:
0
发表于 2003-11-13 16:24 |显示全部楼层

Tomcat中的Filter问题相关

谢谢 .

  请问下面这段程序什么情况下发生死锁:


ini.txt

  1. root=/,/index.jsp,index.htm,index.html
  2. log=/usr/local/jakarta-tomcat-4.1.18/webapps/ROOT/log
  3. ext=jsp,htm,html,xml
  4. count=123230
  5. [code]


  6. 代码:
  7. [code]
  8. package filter;

  9. import java.io.*;
  10. import java.util.*;
  11. import javax.servlet.*;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpSession;

  14. public final class Tadmin
  15.     implements Filter
  16. {

  17.     private FilterConfig filterConfig;
  18.     private int i;
  19.     private Properties prop;
  20.     private Properties prop1;
  21.     private Stack stack;
  22.     private int time;
  23.     private int ip;
  24.     private int count;
  25.     private int temp;
  26.     private File f;
  27.     private long sleeptime;
  28.     private String root;
  29.     private String ext;
  30.     private StringBuffer log;
  31.     private boolean ok;
  32.     private RandomAccessFile rf;
  33.     private File logfile;
  34.     private boolean can;
  35.     FileInputStream is;
  36.     int j;

  37.     public Tadmin()
  38.     {
  39.         log = new StringBuffer();
  40.         j = 0;
  41.         filterConfig = null;
  42.         System.out.println("filter begin......");
  43.         Open();
  44.         System.out.println("filter ok!");
  45.     }

  46.     public void Open()
  47.     {
  48.         try
  49.         {
  50.             prop = new Properties();
  51.             f = new File(System.getProperty("user.dir"));
  52.             f = new File(f.getParent(), "/webapps/ROOT/WEB-INF/classes/filter/ini.txt");
  53.             is = new FileInputStream(f);
  54.             prop.load(is);
  55.             root = (String)prop.get("root");
  56.             ext = (String)prop.get("ext");
  57.             logfile = new File((String)prop.get("log"));
  58.             try
  59.             {
  60.                 count = Integer.parseInt((String)prop.get("count"));
  61.             }
  62.             catch(Exception exception) { }
  63.             is.close();
  64.             prop.clear();
  65.             prop = null;
  66.         }
  67.         catch(Exception exception1)
  68.         {
  69.             System.out.println("filer_Open_error:" + exception1.toString());
  70.         }
  71.     }

  72.     public synchronized void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
  73.         throws IOException, ServletException
  74.     {
  75.         try
  76.         {
  77.             String s = "";
  78.             HttpSession httpsession = ((HttpServletRequest)servletrequest).getSession();
  79.             File file = new File(httpsession.getServletContext().getRealPath("/"), ((HttpServletRequest)servletrequest).getServletPath());
  80.             if(file.exists())
  81.             {
  82.                 String s1 = file.getName().substring(file.getName().indexOf(".") + 1);
  83.                 if(ext.indexOf(s1) >;= 0)
  84.                 {
  85.                     log.append(((HttpServletRequest)servletrequest).getRemoteAddr());
  86.                     log.append("\t");
  87.                     log.append("[" + (new Date()).toString() + "]");
  88.                     log.append("\t");
  89.                     log.append(((HttpServletRequest)servletrequest).getServletPath());
  90.                     char ac[] = {
  91.                         '\u0A0D', '\u0A0A'
  92.                     };
  93.                     log.append(ac);
  94.                 }
  95.                 String s2 = ((HttpServletRequest)servletrequest).getServletPath();
  96.                 if(root.indexOf(s2) >;= 0)
  97.                 {
  98.                     if(httpsession.isNew())
  99.                     {
  100.                         count++;
  101.                     }
  102.                     httpsession.setAttribute("count", (count + 1) + "");
  103.                 }
  104.             }
  105.             j++;
  106.             if(j >;= 100)
  107.             {
  108.                 j = 0;
  109.                 save();
  110.                 savelog();
  111.             }
  112.         }
  113.         catch(Exception exception) { }
  114.         filterchain.doFilter(servletrequest, servletresponse);
  115.     }

  116.     public void save()
  117.     {
  118.         try
  119.         {
  120.             FileOutputStream fileoutputstream = null;
  121.             prop = new Properties();
  122.             try
  123.             {
  124.                 prop.put("count", count + "");
  125.                 prop.put("root", root);
  126.                 prop.put("ext", ext);
  127.                 prop.put("log", logfile.toString());
  128.                 fileoutputstream = new FileOutputStream(f);
  129.                 prop.store(fileoutputstream, "==========LG-8_514-kknd97==========");
  130.             }
  131.             catch(Exception exception1) { }
  132.             prop.clear();
  133.             prop = null;
  134.             if(fileoutputstream != null)
  135.             {
  136.                 fileoutputstream.close();
  137.             }
  138.         }
  139.         catch(Exception exception)
  140.         {
  141.             System.out.println("filer-save-error:" + exception.toString());
  142.         }
  143.     }

  144.     public void savelog()
  145.     {
  146.         try
  147.         {
  148.             try
  149.             {
  150.                 rf.close();
  151.             }
  152.             catch(Exception exception) { }
  153.             if(!logfile.exists())
  154.             {
  155.                 logfile.mkdirs();
  156.             }
  157.             Calendar calendar = Calendar.getInstance();
  158.             rf = new RandomAccessFile(new File(logfile, calendar.get(1) + "_" + (calendar.get(2) + 1) + "_" + calendar.get(5) + ".log"), "rws");
  159.             rf.seek(rf.length());
  160.             if(log.length() >; 0)
  161.             {
  162.                 rf.writeBytes(log.toString());
  163.                 log.delete(0, log.length());
  164.             }
  165.         }
  166.         catch(Exception exception1)
  167.         {
  168.             System.out.println("filer-savelog-error:" + exception1.toString());
  169.         }
  170.         try
  171.         {
  172.             rf.close();
  173.         }
  174.         catch(Exception exception2) { }
  175.     }

  176.     public synchronized void destroy()
  177.     {
  178.     }

  179.     public void init(FilterConfig filterconfig)
  180.     {
  181.         filterConfig = filterconfig;
  182.     }
  183. }
复制代码

论坛徽章:
0
发表于 2003-11-13 22:08 |显示全部楼层

Tomcat中的Filter问题相关

代码有些乱,能否告知想实现什么功能?

有两个建议:

1.把功能分解,一个 filter 只实现一个功能会比较好些,简单的事情比复杂的事情容易解决。

2.java 已经有个很强大的 log 功能包:log4j,只需要配置一个文件就可以实现强大的 log 功能,search google for "log4j"

论坛徽章:
0
发表于 2003-11-17 08:49 |显示全部楼层

Tomcat中的Filter问题相关

cinc: 上面那段代码,是实现访问日志和记数器的功能。

我们怀疑这段代码出现死锁,导致JAVA占满CPU,其他用户得不到应答,我们的理由是: JAVA占满CPU的前一刻,总是这段代码出现异常。

论坛徽章:
0
发表于 2003-11-18 14:03 |显示全部楼层

Tomcat中的Filter问题相关

哪位再帮忙分析一下上面的程序在什么情况下产生死锁,谢谢.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP