怎样在JavaWeb项目中自定义一个配置文件。 20
你直接写个过滤类就可以了(使用 filter 的3个方法 你懂得)
package com.enation.eop;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.Collection;import java.util.Date;import java.util.List;import java.util.Locale;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.enation.eop.processor.back.BackendProcessor;import com.enation.eop.processor.core.freemarker.FreeMarkerPaser;import com.enation.eop.processor.facade.FacadeProcessor;import com.enation.eop.processor.facade.InstallProcessor;import com.enation.eop.processor.facade.ResourceProcessor;import com.enation.eop.resource.IAppManager;import com.enation.eop.resource.model.EopApp;import com.enation.eop.sdk.context.EopContext;import com.enation.eop.sdk.context.EopContextIniter;import com.enation.eop.sdk.context.EopSetting;import com.enation.framework.context.spring.SpringContextHolder;import com.enation.framework.context.webcontext.ThreadContextHolder;public class DispatcherDemoFilter implements Filter { public void init(FilterConfig config) {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// HttpServletRequest req= ServletActionContext.getRequest();// String flag= req.getHeader("X-Requested-With");// HttpServletResponse response = ServletActionContext.getResponse();
String ip = ((HttpServletRequest) request).getHeader("x-forwarded-for"); if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = ((HttpServletRequest) request).getHeader("Proxy-Client-IP");
} if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = ((HttpServletRequest) request).getHeader("WL-Proxy-Client-IP");
} if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
ip = request.getRemoteAddr();
}
String authtoken = request.getParameter("auth-token");
System.out.println(authtoken);
StringBuffer sb = new StringBuffer() ;
InputStream is = request.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String s = "" ;
while((s=br.readLine())!=null){
sb.append(s) ;
}
System.out.println(sb);
String str =sb.toString();
System.out.println(ip+"时间"+new Date().getTime()); //response.setCharacterEncoding("utf-8");
response.setContentType("application/xml");
String write = "<cart><items><item feed-id=\"449573\" offer-id=\"15\" price=\"825\" count=\"1\" delivery=\"true\"/>" +
"</items><delivery-options><delivery service-name=\"Курьер\" type=\"DELIVERY\" price=\"1490.00\">" +
"<dates from-date=\"24-10-2016\" to-date=\"26-10-2016\"/></delivery>" +
"</delivery-options><payment-methods>" +
"<payment-method>CASH_ON_DELIVERY</payment-method></payment-methods></cart>";
System.out.println(write);
response.getWriter().write(write);
} public void destroy() {
}
private static boolean isExinclude(String uri) {
String[] exts = new String[] { "jpg", "gif", "js", "png", "css", "doc", "xls", "swf","ico" };
for (String ext : exts) { if (uri.toUpperCase().endsWith(ext.toUpperCase())) { return true;
}
} return false;
}
}
我不能在web.xml中配置,要新建一个配置文件,怎么才能读到这个配置文件?这就是我的问题:怎么新建另一个类似web.xml的其他.xml。
读文件本身并不在乎文件在哪里,你可以把文件放在类的目录里,放在jar里,放在web的访问目录里,也可以放在服务器任何可以被访问的目录里,唯一需要的只是通知xml解析程序去哪里读,和怎么解析而已。最简单的是放在类目录里,直接就可以访问呢了。
给个过滤器配置例子,仅供参考.需要自己写过滤器类,并读取新的xml保存的ip实现过滤功能
<filter>
<!--定义过滤器,需实现javax.servlet.Filter 接口-->
<filter-name>IPFilter</filter-name>
<filter-class>
cn.com.Filter <!--过滤器类路径-->
</filter-class>
<!--定义过滤器输入参数,可以忽略直接在类中定义要读取的xml-->
<init-param>
<param-name>fileName</param-name>
<param-value>D:/ipfilter.xml</param-value>
</init-param>
</filter>
<!--过滤器匹配条件-->
<filter-mapping>
<filter-name>IPFilter</filter-name>
<!-- /*表示所有请求经过过滤器 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
XML file 会自动生成XML头,在下面加入内容就可以了,首先要有一个根节点,然后如果需要用到一些类,如:spring的一些类,就需要引入包,如:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.dist.*.controller" />
</beans >
其中<?xml ……就是头,<beans 是根节点,下面的<content:……是内容。
如果添加的事properties文件,格式如下:
# 连接池配置
pool.size = 2
pool.max = 50