import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.UnavailableException;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class LogFilter extends HttpServlet implements Filter
{
protected FilterConfig filterConfig = null;
Connection cn =null;
DataSource ds = null;
Context ctx = null;
public void destroy()
{
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
String clientIPAddress = req.getRemoteAddr();
String path= ((HttpServletRequest)req).getRequestURI();
String prg=path.substring(path.lastIndexOf("/")+1);
String which=((HttpServletRequest)req).getQueryString();
String cookiename=null;
String cookievalue=null;
Cookie[] allcookie = ((HttpServletRequest)req).getCookies();
int i=0;
if (allcookie.length>0)
{
while(i<=allcookie.length)
{
cookiename=allcookie[i].getName();
cookievalue=allcookie[i].getName();
if (cookiename.equals("URI"))
break;
i++;
}
}
if (cookiename.equals("URI"))
{
if(path != "ErrPage.jsp")
allcookie[i].setValue(path);
}
else
{
Cookie CURI = new Cookie("URI",path);
((HttpServletResponse)res).addCookie(CURI);
}
try
{
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/csc");
cn = ds.getConnection();
PreparedStatement st = cn.prepareStatement("INSERT INTO system_log_info (user_ip, datetime, place, act, which) VALUES (?,?,?,?,?)");
st.setString(1,clientIPAddress);
st.setString(2,cscBean.common.datetime.getNowDate());
st.setString(3,path);
st.setString(4,prg);
st.setString(5,which);
st.executeUpdate();
st.close();
cn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
}
}
posted on 2007-04-25 21:57
☜♥☞MengChuChen 阅读(498)
评论(0) 编辑 收藏 所属分类:
java_code