JavaWeb-6, filter, listener
1. Filter
Is there such a pain when we write servlets? Every time, we need to set the page code at the beginning of the servlet. If there are fewer servlets in the project, it's OK. But if there are dozens or even hundreds of servlets in the project, it's a bit hard. At this time, we can use the filter to set the corresponding code.
Writing a filter is also quite simple. You only need to let a class implement the filter interface and implement its methods to get a filter class.
Here we write a servlet first
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().println("Hello, dirty Lili!"); } }
Here we don't code him. We do it in the filter.
Next, we write the relevant filter, and we set the relevant code in the filter
import javax.servlet.*; import java.io.IOException; public class MyFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { System.out.println("Filter initializing"); } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding("utf-8"); servletResponse.setCharacterEncoding("utf-8"); servletResponse.setContentType("text/html;charset=UTF-8"); //It is equivalent to passing related requests to the filter chain. If this sentence is not written, the filter will not continue to execute filterChain.doFilter(servletRequest,servletResponse); } public void destroy() { System.out.println("The filter has been destroyed"); } }
To be a filter class, you only need to implement three methods in the filter interface,
Here we need to go after we finish writing web.xml Register servlets and related filters in the same way as servlet classes
<servlet> <servlet-name>myserv</servlet-name> <servlet-class>MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myserv</servlet-name> <url-pattern>/m/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>myserv</servlet-name> <url-pattern>/s</url-pattern> </servlet-mapping> <filter> <filter-name>myfilter</filter-name> <filter-class>MyFilter</filter-class> </filter> <filter-mapping> <filter-name>myfilter</filter-name> <url-pattern>/m/*</url-pattern> </filter-mapping>
Add the page executed by the filter in the URL pattern attribute of filter mapping.
Here we add two different mappings to the servlet, one is filter walking and the other is not filter walking. Let's take a look at the results.
The above is the mapping of walk filter. We find that the coding is normal.
Those who don't walk through the filter have a mess
2. Monitor
The listener, as we all know, is one of the things that detects the action of our current program. In jsp, we usually listen to the creation of session to count the number of people online at the current web address. Here is also a small demo to let you know. The listener is not the key here, so you can understand it.
import javax.servlet.ServletContext; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; //Inherit related interfaces public class Listenner implements HttpSessionListener { public void sessionCreated(HttpSessionEvent httpSessionEvent) { ServletContext sc = httpSessionEvent.getSession().getServletContext(); Integer nowOnlineCount = (Integer) sc.getAttribute("nowOnlineCount"); if(nowOnlineCount==null){ //If it is empty, create a new one int i = 0; nowOnlineCount = new Integer(1); } else{ int count = nowOnlineCount.intValue(); count++; nowOnlineCount = new Integer(count); } sc.setAttribute("nowOnlineCount",nowOnlineCount); } public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { ServletContext sc = httpSessionEvent.getSession().getServletContext(); Integer nowOnlineCount = (Integer) sc.getAttribute("nowOnlineCount"); if(nowOnlineCount==null){ int i = 0; nowOnlineCount = new Integer(0); } else{ int count = nowOnlineCount.intValue(); count++; nowOnlineCount = new Integer(count); } sc.setAttribute("nowOnlineCount",nowOnlineCount); } }
Here we write a monitor to monitor the number of people online, mainly through the number of session s. After writing the monitor, we will go to web.xml Registered in
<listener> <listener-class>Listenner</listener-class> </listener>
Just write the corresponding class here.
Remove the corresponding value at the front end
<h1>There are currently<span>${pageContext.findAttribute("nowOnlineCount")}</span>People online</h1>
Discovery is successful!
All in all! Listeners and filters will definitely be used in the later project development, here you can do it first!