servlet filters

servlet filters

Hi Any body please tell me how to work with servlet filters and can u post smple application to search webpage based on input values?

View Answers

April 10, 2013 at 3:42 PM

Hi venkatash,

In servlets the filters are used to develop the frientend controller design
patterns. It mean, wehenever we send
request to server with any  url, first
the request go through the fileter.
According the servlet specification
the filters are used for to store the
loggin information,for authentication
(checking get or post request),image
convertion , data compression etc. 
The filters contain the url like.

ex: authentication filers:

success.jsp:
 <html> 
  <body>
  we got success message
  </body>
 </html>

error.jsp:
----------
 get request is restricted.

 AuthenticationFilter.java:

 public class AuthentictionFilter implements Filter {

 public void init(FilterConfig config) {

  }

 public void doFilter(ServletRequest request,ServletResponse response,
                                                             FilterChain cahin) {
HttpServletRequest request1=(HttpServletRequest)request;
                  String method=request1.getMethod();

 if(method.equalsIgnoreCase("post")) {
                     chain.doFilter(request,response);
 }else {
  RequestDispatcher dis=request.getRequestDispatcher("error.jsp");
     dis.forward(request,response);
     }
   }

   public void destroy() {}
 }

 compile this one and palce in classes folder.

 web.xml:
    --------
 <web-app>
          <filter>
            <filter-name>af</filter-name>
            <filter-class>AuthenticationFilter</filter-class>
          </filter>

          <filter-mapping>
              <filter-name>af</filter-name>
              <url-pattern>/*</url-pattern>
          </filter-mapping>
       </web-app>


 description:
    -------------
for filters no need to use <load-on-startup> tag. At time of
> deployment the server create the
> filter object and call the
> init(FilterConfig) method. whenever we
> send the request to server like
> http://localhost:8080/filterproject/success.jsp
> , first the server call the
> doFilter(ServletRequest,ServletResponse)
> method . This method check the request
> get or post. If the request mthod is
> get it forward the  request to
> error.jsp. If the request method id
> post the
> chain.doFilter(request,response) send
> the request to success.jsp.

Ads









Related Tutorials/Questions & Answers:
servlet filters
. According the servlet specification the filters are used for to store the loggin...servlet filters  Hi Any body please tell me how to work with servlet filters and can u post smple application to search webpage based on input values
Filters
Filters  Where we use Filters in the project? give one example code, how to use Filters to provide security for the application
Advertisements
Servlet
Servlet  how to navigate one servlet page to another servlet page
servlet
servlet  is there any way to include pdf's in servlet
servlet
servlet  what are the methods and interfaces in the servlet api ?   Servlet Tutorials
servlet
servlet  what are the all necessary configuration to run a servlet
servlet
servlet  how to interact with a servlet from a swing program
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  i want to create a login page with servlet using database mysql? only in servlet not in jsp plzz help me out
Servlet
override Service method in the servlet when you extend GenericServlet to create servlet as it is mandatory to override it. But, when you extend HttpServlet to create a servlet then you can't override service method as there is a need to override
servlet
servlet  i want a program for counting the no of times the servlet has been invoked
servlet
servlet  how to create a login form using servlet using submit,edit delete button
servlet
servlet  dear sir servlet and html not run on eclips plz help me
servlet
servlet  can i stoar record in variable which selected from table in servlet
servlet
of the Servlet API. It contains the classes necessary for a standard, protocol-independent servlet. Every servlet must implement the Servlet interface in one
Servlet
Servlet  I want to know the steps to write a simple servlet program... .   Hello Friend, Follow these steps: Put servlet-api.jar inside the lib folder of apache tomcat. 1)create a servlet. import java.io.*; import
ModuleNotFoundError: No module named 'filters'
ModuleNotFoundError: No module named 'filters'  Hi, My Python... 'filters' How to remove the ModuleNotFoundError: No module named 'filters... to install padas library. You can install filters python with following command
ModuleNotFoundError: No module named 'filters'
ModuleNotFoundError: No module named 'filters'  Hi, My Python... 'filters' How to remove the ModuleNotFoundError: No module named 'filters... to install padas library. You can install filters python with following command
Servlet
Servlet  Can a user defined function be included in a servlet? I need information regarding servlet syntax and not JSP.   Yes, you can create user defined function in Servlets. Have a look at the following link: http
servlet
servlet  I want the full coding for uploading a file in local drive D usin jsp,java class and servlet... help me thanks in advance....   ... and servlet please and thanks in advance
Servlet
Servlet  Hi, Can any one please expalin me below topics SERVLET ENGINE 2.WHY SUPER.INIT(); Thanks alot in advance!! Regards: Akash
servlet
servlet   Dear Deepak, is it compulsary to write the sevice() becoz i ve seen some example which does not ve sevice()..is it tue? plz replay me with thanks praveen
servlet
servlet  plz can anyone give me the link of javax library jar file. i badly need that. thanks in advance   Please visit the following link: Download Servlet API
servlet
servlet  I want a fully readymade project on online voting system with code in java servlet and database backend as msaccess.can u plz send me as soon as possible
Servlet
the same error <web-app> <servlet> <servlet-name>InsertServlet</servlet-name> <servlet-class>InsertServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>
Version of com.jhlabs>filters dependency
List of Version of com.jhlabs>filters dependency
SERVLET
to the servlet and if the user id is correct then a new page will be displayed with his... pass to the servlet and all the fields brlongs to that id will be appear... will be done by using jsp and servlet... Plz help me,im really tensed...........  
servlet
servlet  hi sir,this is ashok.i installed tomcat 6.0 and jdk-150.i checked the server it's working but when execute servlet program for the .class file it shows the errors javax.servlet is not exist.what can i do,please tell me
servlet
servlet  How do we define an application level scope for servlet?   Application scope uses a single namespace, which means all your pages should be careful not to duplicate the names of application scope objects
servlet
servlet   try { con=DaoPack.createConnection(); } catch(Exception e) { e.printStackTrace(); } if(request.getParameter("addproduct")!=null) { ProductBean bean=new ProductBean

Ads