怎样手机网站建设,临沂seo公司稳健火星,新媒体营销策略有哪些,网站用什么软件做败spFilter及应用 Filter有什么用?一、Filter处理中文乱码二、监听器#xff0c;统计网站在线人数1.监听器引入2.统计网站在线人数 三、Filter实现权限拦截 Filter有什么用?
Filter:过滤器#xff0c;可以用来过滤网站的数据。 比如处理中文乱码#xff0c;每次写servlet统计网站在线人数1.监听器引入2.统计网站在线人数 三、Filter实现权限拦截 Filter有什么用?
Filter:过滤器可以用来过滤网站的数据。 比如处理中文乱码每次写servletreq和resp都需要重新设置编码要是有一个机制能够在调用servlet之前就把中文乱码处理好。Filter就可以做到。
一、Filter处理中文乱码 Filter导包不要导错 过滤器Filter: public class CharacterEncodingFilter implements Filter {//初始化,web服务器启动的时候就已经初始化了随时等待过滤对象出现Overridepublic void init(FilterConfig filterConfig) throws ServletException {}Overridepublic void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {req.setCharacterEncoding(UTF-8);resp.setCharacterEncoding(UTF-8);resp.setContentType(text/html);System.out.println(CharacterEncodingFilter执行前....);
// chain:链起一个放行的作用不写这个代码到这里就停住了。chain.doFilter(req,resp);System.out.println(CharacterEncodingFilter执行后.....);}//销毁,web服务器关闭时过滤器会销毁。Overridepublic void destroy() {//System.gc():通知垃圾回收站清理东西。System.gc();System.out.println(CharacterEncodingFilter销毁);}
} 3.对应的Servlet: protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().write(你好世界!);}4.配置web.xml文件: 处理前 处理后
二、监听器统计网站在线人数
1.监听器引入
浏览器是一个客户端软件为什么点击错号就能关闭
2.统计网站在线人数 2.1建立监听器 //统计网站在线人数统计session
public class OnLineCountListener implements HttpSessionListener {
// 创建session监听监视你的一举一动。
// 一旦创建session就会触发一次这个事件。不管是哪个sessionOverridepublic void sessionCreated(HttpSessionEvent httpSessionEvent) {ServletContext sc httpSessionEvent.getSession().getServletContext();Integer onLineCount (Integer) sc.getAttribute(OnLineCount);if (onLineCount null){onLineCountnew Integer(1);}else {int value onLineCount.intValue();onLineCountnew Integer(value1);}sc.setAttribute(OnLineCount,onLineCount);}Overridepublic void sessionDestroyed(HttpSessionEvent httpSessionEvent) {System.out.println(已销毁1个);ServletContext sc httpSessionEvent.getSession().getServletContext();Integer onLineCount (Integer) sc.getAttribute(OnLineCount);if (onLineCount null){onLineCountnew Integer(0);}else {int value onLineCount.intValue();onLineCountnew Integer(value-1);}sc.setAttribute(OnLineCount,onLineCount);}
} 2.2 注册监听器 !-- 注册监听器 --listenerlistener-classcom.kuang.listener.OnLineCountListener/listener-class/listener2.3获取网站在线人数 h1当前有 span % this.getServletConfig().getServletContext().getAttribute(OnLineCount)% /span 人在线!/h1 2.4session的销毁 session.invalidate();//手动注销session-config
!-- 自动销毁 1分钟后session失效以分钟为单位。--session-timeout1/session-timeout/session-config设置销毁之后才会触发销毁的方法减少网站的人数。
一个浏览器是一个session: 刚开始有3个人在线网站默认开始就有3个session,重新发布项目Redeploy就没了。
后面SpringMVC,SpringBoot里面的东西都是用过滤器去实现的。
三、Filter实现权限拦截
用户登录之后才能进入主页用户注销后就不能进入主页了。 要实现这个就要用到权限拦截。
用户登录之后向session中存放用户的数据进入主页的时候要判断用户是否已经登录要在过滤器中实现。 1.LoginServlet: protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取前端请求的参数String username req.getParameter(username);if (username.equals(admin)){//登录成功req.getSession().setAttribute(Constant.USER_SESSION,req.getSession().getId());resp.sendRedirect(/sys/success.jsp);}else{resp.sendRedirect(/error.jsp);}}LogoutServlet: protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Object USER_SESSION req.getSession().getAttribute(Constant.USER_SESSION);if (USER_SESSION!null){req.getSession().removeAttribute(Constant.USER_SESSION);}resp.sendRedirect(/Login.jsp);}3.过滤器 在servlet之前加一层过滤器实现注销后不能进入主页的功能 Overridepublic void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
//ServletRequest HttpServletRequest 两个不一样。前者是后者的上一级但上一级无法得到session。HttpServletRequest request(HttpServletRequest)req;HttpServletResponse response(HttpServletResponse)resp;if (request.getSession().getAttribute(Constant.USER_SESSION)null){response.sendRedirect(/error.jsp);}filterChain.doFilter(request, response);}4.注册过滤器: filterfilter-nameSysFilter/filter-namefilter-classcom.kuang.filter.SysFilter/filter-class/filterfilter-mappingfilter-nameSysFilter/filter-name!-- 过滤sys下面的所有的页面--url-pattern/sys/*/url-pattern/filter-mappingConstant 类 public class Constant {public final static String USER_SESSION USER_SESSION;
}