gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:50:17 24 4
gpt4 key购买 nike

我是使用 Java Servlet 的初学者。我的 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 元素移至 web.xml 中的 servlet 元素下方这次我没有任何错误。但同样没有应用过滤器。

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>

最佳答案

根据 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>

根据异常 FileNotFoundException ,是Quote.jsp直接放在 webapp/webcontent 文件夹下?

<小时/>

编辑

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

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

<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>

关于java - Java Servlet 中的过滤器无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24272201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com