本文主要是介绍Filter和ServletContext和Listener,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
Filter案例
解决全站乱码问题
登录权限校验
ServletContext对象
Listener(监听器)
Filter案例
解决全站乱码问题
我们每次访问每个servlet都要书写处理请求和响应乱码的代码,这样代码十分冗余,所以我们可以在过滤中
@WebFilter("/*") public class EncodeFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) servletRequest;HttpServletResponse response = (HttpServletResponse) servletResponse;//your code..//解决请求乱码request.setCharacterEncoding("utf-8");//解决响应乱码response.setContentType("text/html;charset=utf-8");//放行filterChain.doFilter(request,response);}@Overridepublic void destroy() {} }
@WebServlet("/servletDemo1") public class servletDemo1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request.getParameter("username");System.out.println(username);response.getWriter().print("哈哈哈");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <form action="/servletDemo1" method="post"><input type="text" name="username"><input type="submit" value="提交"> </form> </body> </html>
登录权限校验
步骤
1.访问hhh.html这个页面时必须先要登录,不登录不能访问,使用过滤器代码书写代码让其跳转到登录页面login.html
2.登录loginServlet中获取用户名和密码,存到user对象中,然后存在session中(不关闭服务器,这个session就一直在服务器里面),最后重定向到hhh.html
3. 使用一个过滤器对hhh.html进行拦截,先从session中获取用户信息,如果没有登录,获取的是null,就跳转到登录页面,如果不为null,说明登录成功,放行
hhh.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>这就是终极文件</h1> </body> </html>
login.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>登录</title> </head> <body> <form action="/loginServlet" method="post">用户名:<input type="text" name="username"><br>密码:<input type="password" name="pwd"><br><input type="submit" value="提交"> </form> </body> </html>
loginServlet
@WebServlet("/loginServlet") public class loginServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取请求参数String username = request.getParameter("username");String pwd = request.getParameter("pwd");User user = new User();user.setUsername(username);user.setPwd(pwd);//这里在实际开发中,应该调用业务层的方法,与数据库交互//获取session对象HttpSession session = request.getSession();session.setAttribute("u",user);//登陆成功,就跳转到hhh.htmlresponse.sendRedirect("/hhh.html");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }
loginFilter过滤器,过滤hhh.html
@WebFilter("/hhh.html") public class loginFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) servletRequest;HttpServletResponse response = (HttpServletResponse) servletResponse;//your code..//从session中获取对象HttpSession session = request.getSession();User u = (User) session.getAttribute("u");if(u==null){//没有登录//跳转到登录页面response.sendRedirect("/login.html");}else{//放行filterChain.doFilter(request,response);}}@Overridepublic void destroy() {} }
先登录
在显示页面
ServletContext对象
可以共享数据的对象
1.request:只能在一次请求一次响应中进行数据共享-->请求转发
2.session:只能在一次会话过程中,可以有多次请求和响应
3.ServletContext:只要项目存在就可以共享数据,多次会话,多次请求多次响应都可以共享数据,操作整个项目的配置文件
范围大小:
ServletContext>session>request
ServletContext属于接口,在tomcat启动的时候tomcat创建门面类(ServletContext的子类)
我们可以在Servlet类中直接获取ServletContext对象;getServletContext()属于父类GenericServlet中的方法
@WebServlet("/servletContextDemo1") public class servletContextDemo1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取ServletContext对象ServletContext servletContext = getServletContext();//获取指定文件的MIME类型String mimeType = servletContext.getMimeType("demo1.html");//相对于webapp的路径System.out.println("mimeType="+mimeType);//mimeType=text/html//获取指定文件的真实路径String realPath = servletContext.getRealPath("demo1.html");System.out.println("realPath="+realPath);//realPath=D:\java code\web6_filter_listener\target\web6_filter_listener-1.0-SNAPSHOT\demo1.html}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }
Listener(监听器)
说明:
javaweb的监听器就是监听ServletContext HttpServletRequest HttpSession这三个对象的创建和销毁,同时监听是哪个对象数据的变化,就是监听属性的变化:setAttibute(),removeAttribute()
1.ServletContext是tomcat启动创建,关闭tomcat销毁
2.HttpServlet是浏览器第一次访问执行request.getSession()创建,销毁时间 1)30min 2)执行invalidate()
3.HttpServletRequest时浏览器第一次访问Servlet创建,浏览器收到服务器的响应就销毁
//配置监听器 @WebListener public class conTextListerner implements ServletContextListener {//实现接口//当ServletContext创建(启动tomcat)时执行该方法@Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) {System.out.println("ServletContext创建");}//当ServletContext(关闭tomcat)销毁时执行该方法@Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {System.out.println("ServletContext销毁");} }
不使用注解可以在web.xml中配置
<!-- 配置监听器--><listener><listener-class>com.hhh.listener.conTextListerner</listener-class></listener>
这篇关于Filter和ServletContext和Listener的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!