Servlet监听器,过滤器演示代码:
package com.wfc.ipfilter; 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 javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class IPFilter implements Filter { private String allIP=""; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request=(HttpServletRequest)arg0; HttpServletResponse response=(HttpServletResponse)arg1; // response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); // TODO Auto-generated method stub System.out.println(allIP); //indexOf()的用法:没有就返回-1 System.out.println(arg0.getRemoteAddr()+"-------------2222222----------------"); if(allIP.indexOf(arg0.getRemoteAddr())==-1){ // arg1.getWriter().print("yyyyyyyy"); //response.getWriter().print("我的顶我的"); arg2.doFilter(arg0, arg1); //response.sendRedirect("http://baidu.com/"); }else{ arg1.getWriter().print("nnnnnnnnnnnnnnnnnnnnn"); } } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub allIP=arg0.getInitParameter("ipKey"); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>ip</filter-name> <filter-class>com.wfc.ipfilter.IPFilter</filter-class> <init-param> <param-name>ipKey</param-name> <!-- <param-value>192.168.30.27,127.0.0.1</param-value> --> <param-value>192.168.30.27,192.168.30.26</param-value> </init-param> </filter> <filter-mapping> <filter-name>ip</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>A</servlet-name> <servlet-class>com.wfc.ipfilter.A</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>B</servlet-name> <servlet-class>com.wfc.ipfilter.B</servlet-class> </servlet> <servlet-mapping> <servlet-name>A</servlet-name> <url-pattern>/A</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>B</servlet-name> <url-pattern>/B</url-pattern> </servlet-mapping> </web-app>
下面继续写了两个Servlet,上下文ServletContext的演示:
package com.wfc.ipfilter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class B extends HttpServlet { /** * Destruction of the servlet. <br> * */ String str="womendoasuddsfjweqtewweohe"; public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); } public void init() throws ServletException { // Put your code here System.out.println("------------------------------初始化方法--------------------"); this.getServletContext().setAttribute("ggg", str); System.out.println("B要得到值:-------------"+this.getServletContext().getAttribute("ggg")); } }
package com.wfc.ipfilter; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class A extends HttpServlet { public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //System.out.println("我要得到值1:"+this.getServletContext().getAttribute("ggg")); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.print("传递的值:"+this.getServletContext().getAttribute("ggg")); // System.out.println("我要得到值2:"+this.getServletContext().getAttribute("ggg")); } public void init() throws ServletException { // Put your code here } }