标签:array set top ini parameter 传递数据 efault vax str
filter过滤器主要使用于前台向后台传递数据是的过滤操作。程度很简单就不说明了,直接给几个已经写好的代码:
一、使浏览器不缓存页面的过滤器
三、字符编码的过滤器
四、记录用户的访问操作器
代码:
package com.qwserv.itm.pfl.log.svr;
import java.io.IOException;
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 java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import com.qwserv.itm.api.pfl.sm.vo.Person;
import java.sql.*;
import com.qwserv.itm.api.ServiceAccess;
import com.qwserv.itm.util.toolkit.DebugUtil;
public class ObserveFilter implements Filter {
protected static DebugUtil log = DebugUtil.getInstances("pfl-log", ObserveFilter.class);
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
//记录用户的访问操作
HttpServletRequest request1 = (HttpServletRequest)request;
StringBuffer url = request1.getRequestURL();
//对url进行过滤,如果是js/css/image则不进行处理
if (judgeFile(url.toString())){
String operTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").
format(new java.util.Date());
String hostIp = request.getRemoteAddr();
String sessionId = request1.getRequestedSessionId();
String userId = "";
Person person = (Person)request1.getSession().getAttribute("userObj");
if (null != person && null != person.getUser()){
userId = person.getUser().getId();
}
String queryString = request1.getQueryString();
if (null != queryString) {
url.append(‘?‘);
url.append(queryString);
}
//保存到数据库中
saveToDb(userId,hostIp,sessionId,url.toString(),operTime,"");
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
public boolean judgeFile(String url){
if (url.endsWith(".gif") || url.endsWith(".jpg") || url.endsWith(".png")
|| url.endsWith(".bmp") || url.endsWith(".css") || url.endsWith(".js")
|| url.endsWith(".jsx")){
return false;
} else {
return true;
}
}
public int saveToDb(String userId, String hostIp,String sessionId,String url,
String operTime,String desc){
//将报表任务数据保存到数据库中
Connection conn = null;
Statement st = null;
try {
//构造sql表达式,将数据插入数据库
conn = ServiceAccess.getSystemSupportService().getDefaultConnection();
st = conn.createStatement();
String sql = "insert into LOG_OBSERVE_HISTORY(USERID,URL,Detail,SessionID,HostName,StartDate) values(‘"+
userId + "‘,‘" + url + "‘,‘" + desc + "‘,‘" + sessionId
+ "‘,‘" + hostIp + "‘,‘" + operTime + "‘)";
if (ServiceAccess.getSystemSupportService().getConnectionType(conn)==ServiceAccess.getSystemSupportService().JCA_TYPE_ORACLE){
sql = "insert into LOG_OBSERVE_HISTORY(Id,USERID,URL,Detail,SessionID,HostName,StartDate) values(LOG_OBSERVE_SEQ.nextval,‘"+
userId + "‘,‘" + url + "‘,‘" + desc + "‘,‘" + sessionId
+ "‘,‘" + hostIp + "‘,TO_DATE(‘" + operTime
+ "‘,‘YYYY-MM-DD HH24:MI:SS‘))";
}
st.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
log.error("--------------------The url String is:" + url + "-------------------------------");
return 1; //表示操作失败
} finally {
if (null != st)
{
try{
st.close();
}
catch(Exception e)
{
e.printStackTrace();
}
st = null;
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}
}
return 0; //表示操作成功
}
}
<filter>
<filter-name>ObserveFilter</filter-name>
<filter-class>com.qwserv.itm.pfl.log.svr.ObserveFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObserveFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
五.Filter防止用户访问一些未被授权的资源
com.drp.util.filter;
<filter>
<filter-name>AuthFilter</filter-name>
<filter-class>com.drp.util.filter.AuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthFilter</filter-name>
<url-pattern>*.jsp</url-pattern>//表示对所有jsp文件有效
</filter-mapping>
标签:array set top ini parameter 传递数据 efault vax str
原文地址:http://www.cnblogs.com/chouji717/p/6063054.html