Advertisement
andersonalmada

Untitled

Jun 27th, 2022
893
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None
  1. package br.ufc.mandacaru.filter;
  2.  
  3. import java.io.IOException;
  4.  
  5. import jakarta.servlet.Filter;
  6. import jakarta.servlet.FilterChain;
  7. import jakarta.servlet.FilterConfig;
  8. import jakarta.servlet.ServletException;
  9. import jakarta.servlet.ServletRequest;
  10. import jakarta.servlet.ServletResponse;
  11. import jakarta.servlet.http.HttpFilter;
  12.  
  13. /**
  14.  * Servlet Filter implementation class HelloFilter
  15.  */
  16. public class HelloFilter extends HttpFilter implements Filter {
  17.        
  18.     /**
  19.      * @see HttpFilter#HttpFilter()
  20.      */
  21.     public HelloFilter() {
  22.         super();
  23.         // TODO Auto-generated constructor stub
  24.     }
  25.  
  26.     /**
  27.      * @see Filter#destroy()
  28.      */
  29.     public void destroy() {
  30.         // TODO Auto-generated method stub
  31.     }
  32.  
  33.     /**
  34.      * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
  35.      */
  36.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  37.         // TODO Auto-generated method stub
  38.         response.getWriter();
  39.          
  40.         // This will print output on console
  41.         System.out.println(
  42.             "Before filter - Preprocessing before servlet");        
  43.         String firstName = request.getParameter("first_name");
  44.        
  45.         if(firstName.equals("almada")) {
  46.             // some authentication if required
  47.             chain.doFilter(request, response);         
  48.         } else {
  49.             response.setContentType("text/html");
  50.             response.getWriter().append("Não autorizado");
  51.         }
  52.          
  53.         // This will print output on console
  54.         System.out.println(
  55.             "After servlet - Following code will execute after running the servlet - PostProcessing");
  56.    
  57.     }
  58.  
  59.     /**
  60.      * @see Filter#init(FilterConfig)
  61.      */
  62.     public void init(FilterConfig fConfig) throws ServletException {
  63.         // TODO Auto-generated method stub
  64.     }
  65.  
  66. }
  67.  
Advertisement
RAW Paste Data Copied
Advertisement