views:

118

answers:

2

Hi,

For my web application, i have created login page. To block the access to the other pages i setup a filter. But while running the web app, it gives Servlet class com.pricar.grid.AuthenticationFilter is not a javax.servlet.Servlet.

I also cant able to get the proper result.

Here my Code: filter config in web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"&gt;
<web-app>
<display-name>Staff Management</display-name>
<filter>
    <filter-name>AuthenticationFilter</filter-name>
    <display-name>AuthenticationFilter</display-name>
    <description></description>
        <filter-class>com.pricar.grid.AuthenticationFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>AuthenticationFilter</filter-name>
    <url-pattern>/StaffManagementSystem/*</url-pattern>
    </filter-mapping>
   <servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <display-name>DWR Servlet</display-name>
        <description>Direct Web Remoter Servlet</description>
        <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
    <servlet-name>dwr-invoker</servlet-name>
    <url-pattern>/dwr/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>UserLogin.html</welcome-file>
    </welcome-file-list> 
    </web-app>

The Filter code is:

package com.pricar.grid;

public class AuthenticationFilter implements Filter {
public AuthenticationFilter() {
    // TODO Auto-generated constructor stub
}
public void destroy() {
    // TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    WebContext ctx = WebContextFactory.get();
    HttpServletRequest req = ctx.getHttpServletRequest();
    HttpSession session = req.getSession(false);
    String name = (String) session.getAttribute("userName");
    System.out.print ("Within Simple Filter ... ");
    System.out.println ("Filtering the Request ...");
    System.out.print ("Within Simple Filter ... ");
    System.out.println ("Filtering the Response ...");
    if ( name == null ){
        //I have to redirect to the person to index page.
        ((HttpServletResponse) response).sendRedirect("index.html");
    }
    chain.doFilter(request, response);  
}
public void init(FilterConfig fConfig) throws ServletException {
    // TODO Auto-generated method stub
}
}

The URL i am trying to test is http://localhost:8080/StaffManagementSystem/EmployeeManagement.html

i am using jetty as a server.

Any suggestions would be appreciative!!! Thanks in Advance!!


Final Update:

All the changes that mentioned, were done. Its getting compiled. I cant even able to get the "sysout" output in my console. Its simply passing the URL.

+2  A: 

If your URL is http://localhost:8080/StaffManagementSystem/EmployeeManagement.html don't you mean to use this pattern for your filter mapping :

    <url-pattern>/StaffManagementSystem/*</url-pattern>

And you may have an error in your web.xml somewhere else. Didn't you use your Filter as a Servlet anywhere in your configuration ?


Your doFilter() method does a chain.doFilter (request, response); before even checking the values. You should remove this line too.


At the end your doFilter() does a redirect anyway. You should remove the entire else part.


And implementing Filter means that your must write the method :

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

(Not one with HttpServlet**)


You should avoid to use System.out in a web application. Instead choose loggers and check the logs of your servlet container.

Colin Hebert
@Colin HEBERT: ea.. i have seen it.. and deleted it. I thought it is the problem maker. is that right??!!!
NooBDevelopeR
There is still one more major problem in the code.
BalusC
I updated the answer.
Colin Hebert
@ BalusC: What is the major problem?
NooBDevelopeR
@Colin: Those are not relevant to the *actual* problem. It's still there (and the OP removed the hint during an edit).
BalusC
Could it be treating a request as a response? `((HttpServletResponse) request)` Or as a WebContext? `((WebContext) request)` My guess would be the real problem is a programmer who doesn't understand the API he/she is using :-)
rsp
@rsp: the OP changed that afterwards, in an apparent shoot in the dark. The code as it now is doesn't even compile.
BalusC
@BalusC, Holly c... I just don't understand how the OP didn't notice that its code doesn't compile...
Colin Hebert
@All: Ok, Leave That... What is mean by OP? It may not be asked here. But, it seems i have to ask it here. Is that a 'professionals word'?
NooBDevelopeR
"Original Poster" - http://en.wikipedia.org/wiki/Internet_forum#Thread
Colin Hebert
+1  A: 

Not sure but I think you have an issue here:

if ( name == null ){ //I have to redirect to the person to index page. ((HttpServletResponse) request).sendRedirect("index.html");

Change that to response

timanzo