实现某电商系统的字符过滤器,要求对所有页面进行字符过滤功能。
选取合适的监听器实现统计商品列表页的访问人数(注:同一用户多次访问商品列表页时,计数为1次)。
提交代码要求如下:
1)提交商品列表页的关键代码;DealServlet
显示商品列表页的访问人数
2)提交字符过滤器的关键代码;
3) 提交相关监听器的关键代码;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>商品列表</h1>
<a href="d1">点我查看网页访问量</a>
</body>
</html>
package listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listener implementation class ContextListener
*
*/
@WebListener
public class ContextListener implements ServletContextListener {
/**
* Default constructor.
*/
public ContextListener() {
// TODO Auto-generated constructor stub
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
ServletContext context=sce.getServletContext();
context.setAttribute("userNum", 0);
}
}
package listener;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Application Lifecycle Listener implementation class PageListener
*
*/
@WebListener
public class PageListener implements HttpSessionListener {
/**
* Default constructor.
*/
public PageListener() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpSessionListener#sessionCreated(HttpSessionEvent)
*/
public void sessionCreated(HttpSessionEvent se) {
// TODO Auto-generated method stub
System.out.println("Session创建-------------------");
ServletContext context=se.getSession().getServletContext();
int count=(int)context.getAttribute("userNum");
count++;
context.setAttribute("userNum", count);
}
/**
* @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
*/
public void sessionDestroyed(HttpSessionEvent se) {
// TODO Auto-generated method stub
// System.out.println("Session销毁-------------------");
// ServletContext context=se.getSession().getServletContext();
// int count=(int)context.getAttribute("userNum");
// count--;
// context.setAttribute("userNum", count);
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class DealServlet
*/
@WebServlet("/d1")
public class DealServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DealServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer=response.getWriter();
HttpSession session=request.getSession();
ServletContext context=getServletContext();
int count = (int)context.getAttribute("userNum");
writer.write("<h1>访问人数为:</h1>"+count);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package filter;
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.annotation.WebFilter;
/**
* Servlet Filter implementation class CharacterFilter
*/
@WebFilter("/*")
public class CharacterFilter implements Filter {
/**
* Default constructor.
*/
public CharacterFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// pass the request along the filter chain
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("Filter is initing!");
}
}