开发者

How to make run a servlet filter?

For my web appl开发者_如何学Cication, 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">
<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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜