Java Servlet中的过滤器无法正常工作?

时间:2014-06-17 19:51:17

标签: java jsp servlets web.xml servlet-filters

我是使用Java Servlets的初学者。我的servlet存在一些问题。 我的过滤器没有被应用。

这是我的web.xml:

<web-app>
<display-name>CP130 Assignment 8</display-name>
<filter>
    <description>Filter using XSLT to format servlet response</description>
    <display-name>CP130QuoteTransformFilter</display-name>
    <filter-name>QuoteResponseFormatter</filter-name>
    <filter-class>edu.uw.rgm.web.QuoteTransformFilter</filter-class>
    <init-param>
        <description>HTML transform</description>
        <param-name>html-transform</param-name>
        <param-value>xslt/quote2html.xslt</param-value>
    </init-param>
    <init-param>
        <description>JSON transform</description>
        <param-name>json-transform</param-name>
        <param-value>xslt/quote2json.xslt</param-value>
    </init-param>
    <init-param>
        <description>Plain transform</description>
        <param-name>plain-transform</param-name>
        <param-value>xslt/quote2plain.xslt</param-value>
    </init-param>
</filter>
<servlet>
    <description>Servlet obtaining stock quote from Yahoo Financial Services</description>
    <display-name>CP130QuoteServlet</display-name>
    <servlet-name>StockQuoteJsp</servlet-name>
    <servlet-class>edu.uw.rgm.web.YahooStockQuoteServlet</servlet-class>
    <jsp-file>/Quote.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>StockQuoteJsp</servlet-name>
    <url-pattern>/Quote.jsp</url-pattern>
</servlet-mapping>
<filter-mapping>
    <filter-name>QuoteResponseFormatter</filter-name>
    <servlet-name>StockQuoteJsp</servlet-name>
</filter-mapping>
<filter-mapping>
    <filter-name>QuoteResponseFormatter</filter-name>
    <url-pattern>/Quote.jsp</url-pattern>
</filter-mapping>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

我在申请响应的过滤器中放了一些println语句。当我在控制台中运行我的主应用程序时,我发现没有生成任何打印件。它可能表明我的回复没有通过过滤器。我仔细检查了过滤器名称和过滤器类和url模式,并且没有拼写错误。

我收到以下错误:

[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.io.FileNotFoundException: http://localhost:8080/StockQuote/Quote.jsp?symbol=GOOG&rstype=json
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1624)
    at app.QuoteApp.exec(QuoteApp.java:73)
    at app.QuoteApp.main(QuoteApp.java:100)
    ... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.820s
[INFO] Finished at: Tue Jun 17 12:49:02 PDT 2014
[INFO] Final Memory: 8M/245M

web.xml中元素的顺序是否重要?

为什么我的过滤器未应用于响应?

由于

修改

以下是我尝试的web.xml的最新版本。我刚刚将filter元素移到servlet web.xml元素下面 这次我没有任何错误。但是再次没有应用过滤器。

web.xml

<web-app>
    <display-name>CP130 Assignment 8</display-name>
    <servlet>
        <description>Servlet obtaining stock quote from Yahoo Financial Services</description>
        <display-name>CP130QuoteServlet</display-name>
        <servlet-name>StockQuoteJsp</servlet-name>
        <jsp-file>/Quote.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>StockQuoteJsp</servlet-name>
        <servlet-class>edu.uw.rgm.web.YahooStockQuoteServlet</servlet-class>
        <url-pattern>/Quote.jsp</url-pattern>
    </servlet-mapping>
    <filter>
        <description>Filter using XSLT to format servlet response</description>
        <display-name>CP130QuoteTransformFilter</display-name>
        <filter-name>QuoteResponseFormatter</filter-name>
        <filter-class>edu.uw.rgm.web.QuoteTransformFilter</filter-class>
        <init-param>
            <description>HTML transform</description>
            <param-name>html-transform</param-name>
            <param-value>xslt/quote2html.xslt</param-value>
        </init-param>
        <init-param>
            <description>JSON transform</description>
            <param-name>json-transform</param-name>
            <param-value>xslt/quote2json.xslt</param-value>
        </init-param>
        <init-param>
            <description>Plain transform</description>
            <param-name>plain-transform</param-name>
            <param-value>xslt/quote2plain.xslt</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>QuoteResponseFormatter</filter-name>
        <servlet-name>StockQuoteJsp</servlet-name>
    </filter-mapping>
    <filter-mapping>
        <filter-name>QuoteResponseFormatter</filter-name>
        <url-pattern>/Quote.jsp</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

1 个答案:

答案 0 :(得分:1)

根据web-app_2_3.dtd

<!ELEMENT servlet (icon?, servlet-name, display-name?, description?,
(servlet-class|jsp-file), init-param*, load-on-startup?, run-as?, security-role-ref*)>

使用<jsp-file>下的<servlet-class><servlet>。由于JSP最终转换为Servlet类,因此您不能同时使用它们。

<servlet>
    <description>Servlet obtaining stock quote from Yahoo Financial Services</description>
    <display-name>CP130QuoteServlet</display-name>
    <servlet-name>StockQuoteJsp</servlet-name>
    <servlet-class>edu.uw.rgm.web.YahooStockQuoteServlet</servlet-class>
    <jsp-file>/Quote.jsp</jsp-file>
</servlet>

根据异常FileNotFoundExceptionQuote.jsp是否直接放在webapp / webcontent文件夹下?


修改

对于特定的URL模式,您可以将其重定向到JSP页面或Servlet类。

在下面的情况下,只要在URL中传递Quote.jsp,就会调用Servlet。

<servlet>
    <description>Servlet obtaining stock quote from Yahoo Financial Services</description>
    <display-name>CP130QuoteServlet</display-name>
    <servlet-name>StockQuoteJsp</servlet-name>
    <servlet-class>edu.uw.rgm.web.YahooStockQuoteServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>StockQuoteJsp</servlet-name>
    <url-pattern>/Quote.jsp</url-pattern>
</servlet-mapping>