2

iam using the below code in servlet filters to avoid direct acess of pages from the URL but i get Error 404--Not Found - From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1 irrespective of the URL i try.

Code:

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

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;

    String uri = req.getRequestURI();
    this.context.log("Requested Resource::"+uri);

    HttpSession session = req.getSession(false);
    if(session == null && !(uri.endsWith("html") || uri.endsWith("LoginServlet"))){
        this.context.log("Unauthorized access request");
        res.sendRedirect("login.html");
    }else{
        // pass the request along the filter chain
        chain.doFilter(request, response);
    }   

}

My Web.xml

    <?xml version="1.0" encoding="UTF-8"?>
 <!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>ServletFilterExample</display-name>
 <filter>
<filter-name>RequestLoggingFilter</filter-name>
<filter-class>com.journaldev.servlet.filters.RequestLoggingFilter</filter-class>
</filter>
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>com.journaldev.servlet.filters.AuthenticationFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>RequestLoggingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
 </web-app>

Environment used: Weblogic server 11gR1 and web app 2.5

I'm Hitting the url :

http://localhost:7001/Filters/LoginServlet

Could you please help.

Thanks.

user1251490
  • 69
  • 2
  • 2
  • 5
  • Can you post the content of your web.xml and the uri that you try to join ? – F. Geraerts Mar 11 '14 at 07:15
  • The context root of your application is named Filters ? It's seems strange. Your filter code is only call when you call an url into your application. The url to call has probably this form http://localhost:7001/application_context_name/something.html – F. Geraerts Mar 11 '14 at 07:31
  • Hi, I have posted my web xml along with the URL. Refer my above question. Please help. My Context name is Filters. – user1251490 Mar 11 '14 at 07:34
  • You must access to your application by an url containing the server name, the port and the application/web app name. What is the name of your application ? – F. Geraerts Mar 11 '14 at 07:38

1 Answers1

0

I strongly recommend you to check whether you are application is properly deployed in your server. I see that the configurations in web.xml are fine, mapped with filters properly with URL. Please double check it.

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53