- 论坛徽章:
- 0
|
Tomcat中的Filter问题相关
谢谢 .
请问下面这段程序什么情况下发生死锁:
ini.txt
- root=/,/index.jsp,index.htm,index.html
- log=/usr/local/jakarta-tomcat-4.1.18/webapps/ROOT/log
- ext=jsp,htm,html,xml
- count=123230
- [code]
- 代码:
- [code]
- package filter;
- import java.io.*;
- import java.util.*;
- import javax.servlet.*;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- public final class Tadmin
- implements Filter
- {
- private FilterConfig filterConfig;
- private int i;
- private Properties prop;
- private Properties prop1;
- private Stack stack;
- private int time;
- private int ip;
- private int count;
- private int temp;
- private File f;
- private long sleeptime;
- private String root;
- private String ext;
- private StringBuffer log;
- private boolean ok;
- private RandomAccessFile rf;
- private File logfile;
- private boolean can;
- FileInputStream is;
- int j;
- public Tadmin()
- {
- log = new StringBuffer();
- j = 0;
- filterConfig = null;
- System.out.println("filter begin......");
- Open();
- System.out.println("filter ok!");
- }
- public void Open()
- {
- try
- {
- prop = new Properties();
- f = new File(System.getProperty("user.dir"));
- f = new File(f.getParent(), "/webapps/ROOT/WEB-INF/classes/filter/ini.txt");
- is = new FileInputStream(f);
- prop.load(is);
- root = (String)prop.get("root");
- ext = (String)prop.get("ext");
- logfile = new File((String)prop.get("log"));
- try
- {
- count = Integer.parseInt((String)prop.get("count"));
- }
- catch(Exception exception) { }
- is.close();
- prop.clear();
- prop = null;
- }
- catch(Exception exception1)
- {
- System.out.println("filer_Open_error:" + exception1.toString());
- }
- }
- public synchronized void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
- throws IOException, ServletException
- {
- try
- {
- String s = "";
- HttpSession httpsession = ((HttpServletRequest)servletrequest).getSession();
- File file = new File(httpsession.getServletContext().getRealPath("/"), ((HttpServletRequest)servletrequest).getServletPath());
- if(file.exists())
- {
- String s1 = file.getName().substring(file.getName().indexOf(".") + 1);
- if(ext.indexOf(s1) >;= 0)
- {
- log.append(((HttpServletRequest)servletrequest).getRemoteAddr());
- log.append("\t");
- log.append("[" + (new Date()).toString() + "]");
- log.append("\t");
- log.append(((HttpServletRequest)servletrequest).getServletPath());
- char ac[] = {
- '\u0A0D', '\u0A0A'
- };
- log.append(ac);
- }
- String s2 = ((HttpServletRequest)servletrequest).getServletPath();
- if(root.indexOf(s2) >;= 0)
- {
- if(httpsession.isNew())
- {
- count++;
- }
- httpsession.setAttribute("count", (count + 1) + "");
- }
- }
- j++;
- if(j >;= 100)
- {
- j = 0;
- save();
- savelog();
- }
- }
- catch(Exception exception) { }
- filterchain.doFilter(servletrequest, servletresponse);
- }
- public void save()
- {
- try
- {
- FileOutputStream fileoutputstream = null;
- prop = new Properties();
- try
- {
- prop.put("count", count + "");
- prop.put("root", root);
- prop.put("ext", ext);
- prop.put("log", logfile.toString());
- fileoutputstream = new FileOutputStream(f);
- prop.store(fileoutputstream, "==========LG-8_514-kknd97==========");
- }
- catch(Exception exception1) { }
- prop.clear();
- prop = null;
- if(fileoutputstream != null)
- {
- fileoutputstream.close();
- }
- }
- catch(Exception exception)
- {
- System.out.println("filer-save-error:" + exception.toString());
- }
- }
- public void savelog()
- {
- try
- {
- try
- {
- rf.close();
- }
- catch(Exception exception) { }
- if(!logfile.exists())
- {
- logfile.mkdirs();
- }
- Calendar calendar = Calendar.getInstance();
- rf = new RandomAccessFile(new File(logfile, calendar.get(1) + "_" + (calendar.get(2) + 1) + "_" + calendar.get(5) + ".log"), "rws");
- rf.seek(rf.length());
- if(log.length() >; 0)
- {
- rf.writeBytes(log.toString());
- log.delete(0, log.length());
- }
- }
- catch(Exception exception1)
- {
- System.out.println("filer-savelog-error:" + exception1.toString());
- }
- try
- {
- rf.close();
- }
- catch(Exception exception2) { }
- }
- public synchronized void destroy()
- {
- }
- public void init(FilterConfig filterconfig)
- {
- filterConfig = filterconfig;
- }
- }
复制代码 |
|