| Author |
question on filters (HFSJ page :675)
|
georgy jacob
Ranch Hand
Joined: Nov 23, 2005
Posts: 53
|
|
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
Joined: Nov 24, 2005
Posts: 14672
|
|
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 ]
|
[My Blog]
All roads lead to JavaRanch
|
 |
georgy jacob
Ranch Hand
Joined: Nov 23, 2005
Posts: 53
|
|
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
Joined: Nov 23, 2005
Posts: 53
|
|
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
Joined: Nov 24, 2005
Posts: 14672
|
|
try with this : <form method="POST" action="/myapp/Authenticate.do"> Replace myapp by the name of your webapp
|
 |
georgy jacob
Ranch Hand
Joined: Nov 23, 2005
Posts: 53
|
|
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
Joined: Feb 05, 2003
Posts: 4725
|
|
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 ]
|
A good workman is known by his tools.
|
 |
 |
|
|
subject: question on filters (HFSJ page :675)
|
|
|