HTTP状态404 - 未找到

时间:2015-12-22 12:12:42

标签: java servlets filter

过滤

import java.io.IOException;  
import java.io.PrintWriter;  

import javax.servlet.*;  

public class MyFilter implements Filter{  

@Override
public void init(FilterConfig arg0) throws ServletException {}  

@Override
public void doFilter(ServletRequest req, ServletResponse resp,  
    FilterChain chain) throws IOException, ServletException {  

    PrintWriter out=resp.getWriter();  
    out.print("filter is invoked before");  

    chain.doFilter(req, resp);//sends request to next resource  

    out.print("filter is invoked after");  
    }  
    public void destroy() {}  
}  

的servlet

import java.io.IOException;  
import java.io.PrintWriter;  

import javax.servlet.ServletException;  
import javax.servlet.http.*;  

public class HelloServlet extends HttpServlet {  
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  

        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();  

        out.print("<br>welcome to servlet<br>");  

    }  

}  

的web.xml

<web-app>  

<servlet>  
 <servlet-name>HelloServlet</servlet-name>  
 <servlet-class>HelloServlet</servlet-class>  
</servlet>  

<servlet-mapping>  
 <servlet-name>HelloServlet</servlet-name>  
 <url-pattern>/HelloServlet</url-pattern>  
</servlet-mapping>  

<filter>  
 <filter-name>MyFilter</filter-name>  
 <filter-class>MyFilter</filter-class>  
</filter>  

<filter-mapping>  
 <filter-name>MyFilter</filter-name>  
 <url-pattern>/servlet1</url-pattern>  
</filter-mapping>  


</web-app>  

的index.html

     <a href="servlet1">click here</a>  

错误是:

HTTP状态404 - 未找到

输入状态报告

消息未找到

说明请求的资源不可用。 GlassFish Server开源版4.0

我已经给出了完整的代码plz帮助我找到我的错误 在新的sevlet 问题可能听起来很愚蠢,但PLZ帮助我

2 个答案:

答案 0 :(得分:0)

  

HTTP状态404 - 未找到

因为您的servlet已映射到/HelloServlet,并且您的过滤器正在尝试拦截servlet1。由于servlet1没有servlet,容器表示找不到资源。

将您的过滤器映射更改为/HelloServlet<a href="/HelloServlet">click here</a>

答案 1 :(得分:0)

尝试改变这一点:

   <a href="/servlet1">click here</a>  

使用:

  <a href="/HelloServlet">click here</a>