【永恒的瞬间】
☜Give me hapy ☞
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();//取得IP
String path= ((HttpServletRequest)req).getRequestURI();//取得路徑
String prg=path.substring(path.lastIndexOf("/")+1);//取得程式名稱
String which=((HttpServletRequest)req).getQueryString();//查詢字串
String cookiename=null;
String cookievalue=null;
//將path存入Cookie,ErrorPage用!
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 阅读(399) 评论(0)  编辑  收藏 所属分类: java_code

只有注册用户登录后才能发表评论。


网站导航: