aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt in running Filters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt in running Filters" Watch "Doubt in running Filters" New topic
Author

Doubt in running Filters

Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
Hi,
when i include filters in my application,it is not getting deployed.

Here is my web.xml,
<servlet>
<servlet-name>mapping</servlet-name>
<servlet-class>mapServlet</servlet-class>
</servlet>

<filter>
<filter-name>Filter1</filter-name>
<filter-class>filterTest.Filter1</filter-class>
</filter>

<filter-mapping>
<filter-name>Filter1</filter-name>
<servlet-name>mapping</servlet-name>
</filter-mapping>

<servlet-mapping>
<servlet-name>mapping</servlet-name>
<url-pattern>/mapping</url-pattern>
</servlet-mapping>

And the Filter pgm goes like this,

package filterTest;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

class Filter1 implements Filter
{
private FilterConfig fg;

public Filter1()
{
System.out.println("Inside Filter1's constructor");
}
public void init(FilterConfig fg) throws ServletException
{
System.out.println("Filter1's initialization ");
this.fg = fg;
}

public void doFilter(ServletRequest req,ServletResponse res,FilterChain fc) throws IOException
{
System.out.println("Filter1's service ");
HttpServletResponse res1 = (HttpServletResponse) res;
PrintWriter pw = res.getWriter();
pw.println(" <br> Inside Filter ");
pw.close();
}
public void destroy()
{
System.out.println("Filter1's destroy");
}

}


when i try to deploy this application it says startup failed due to previous errors.Meanwhile if i remove the filter declarations from the DD it gets deployed.


when you really want something, all the universe always conspires in your favour.<br /> <br />SCJP1.5-77%<br />SCWCD-89%
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Your filter-mapping is wrong - you should be using a <url-pattern> rather than a <servlet-name>

e.g.

Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
Daniel,
<servlet-name> can be given in filter mapping.As a matter fact i've tried what u've suggested,but still i'm getting the same error.



Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Yup - you're right - I stand corrected.

Here's an example taken from a real, working (admittedly 2.3 descriptor) I have:

<CODE]
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>utils.LoginFilter</filter-class>
</filter>


<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/pages/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

[/CODE]


By the way, you realise that your filter blocks the following servlet (which is legal of course) because it doesn't invoke filterChains doFilter method?

Have you checked the logs? It's not because the response is already committed is it?
Senthil Kumar
Ranch Hand

Joined: Mar 13, 2006
Posts: 264
Daniel,
i reckon the catch is in the Filter code.This is what i'm getting in the
console log

"SEVERE: Error filterStart"

becoz of this application is not getting deployed.
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Hi Senthil

after some experimenting, I believe that your filter doesn't work due to your constructor. If you either remove your constructor, or put a call to super() into it, it works for me (using apache geronimo).

I'll admit that I don't understand this one. Filter is an interface, so by giving your class a constructor and not calling super(), you aren't preventing a container supplied superclass constructor from running.

Can anyone explain?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in running Filters
 
Similar Threads
SEVERE: Error filterStart what does Error means
Q: Filter on Page 668 and page 695
Problem with web fragments
Filters and GZip
fiter help needed