Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on filters (HFSJ page :675)

 
georgy jacob
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know whether filters is supported by tomcat 4.1 .I am trying to execute the filter code in HFSJ page 675. But its not getting invoked. I have configured the same in web.xml as well.

the follwing is the code (MyFilter.java)


import javax.servlet.*;
import java.io.*;
import javax.servlet.http.HttpServletRequest;
public class MyFilter implements Filter {

private FilterConfig fc;
public void init(FilterConfig config) throws ServletException
{
System.out.println("Inside Init");
this.fc = config;
}
public void doFilter(ServletRequest req,ServletResponse resp,FilterChain chain) throws ServletException,IOException
{
HttpServletRequest httpReq = (HttpServletRequest) req;
String name=httpReq.getRemoteUser();
if(name!=null)
{
fc.getServletContext().log("User" + name + "accessed the server @" + new java.util.Date());
System.out.println("User " + name + "accessed the server @" + new java.util.Date());
}
chain.doFilter(req,resp);
}
public void destroy()
{
}
}

The web.xml file is




<web-app>
<!--
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/errorpage.jsp</location>
</error-page>
-->

<filter>
<filter-name>MyFil</filter-name>
<filter-class>MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFil</filter-name>
<url-pattern>/Authenticate.do</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>LoginAuthentication</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/Authenticate.do</url-pattern>
</servlet-mapping>
</web-app>


Though No errors or exceptions are not being thrown the filter is simply not getting called.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tomcat 4.1 supports Servlet 2.3, so filters are supported.

Do you access your page through the /Authenticate.do URL ?
[ January 27, 2006: Message edited by: Satou kurinosuke ]
 
georgy jacob
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. authenticate.do is a post method defined in a JSP page.

In my html i have given

<form method ="POST" action= "authenticate.do">


When i press a login button the above action gets invoked.But the Filter i i created is not getting invoked(i m not able to see the "inside filter" message printed in the console of my server)
 
georgy jacob
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When i press a login button the above action gets invoked.But the Filter i i created is not getting invoked(i m not able to see the "inside filter" message printed in the console of my server)
--------------------------------------------------------------------------------


I meant the inside INIT message .
System.out.println("User " + name + "accessed the server @" + new java.util.Date());
also doesnt get printed in the server console
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try with this :
<form method="POST" action="/myapp/Authenticate.do">

Replace myapp by the name of your webapp
 
georgy jacob
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satou,

When i am pressing a button the authenticate.do gets appended to my url. At this time the url is mapped to a servlet and the servlet gets executed.
But the filter alone is not getting executed. Changing the action to webapp/authenticate.do appends the url as webapp/webapp/authenticate.do resulting in the message that the page cant be found
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a long shot, but try keeping things case sensitive. In other words, make your form post to Authenticate.do! It's good practice because if you ever deploy to a Unix server (typical in real world) you could end up with a real mess if there are inconsistencies.

Everything looks ok to me on the filter. The only thing even slightly out of place that I can see is the 'A'/'a'.
[ January 27, 2006: Message edited by: Marc Peabody ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic