【发布时间】:2013-08-26 20:35:13
【问题描述】:
我创建了名为 MainContent 的 servlet。我有这样的映射
<servlet>
<display-name>MainContent</display-name>
<servlet-name>MainContent</servlet-name>
<servlet-class>ge.test.servlet.MainContent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainContent</servlet-name>
<url-pattern>/main</url-pattern>
</servlet-mapping>
所以,当我转到链接时: //localhost:8080/MyAppl/main 我进入了servlets doGet() 方法。然后我创建 RequestDispatcher 转发到 index.jsp。
一切正常!
RequestDispatcher rd = context.getRequestDispatcher("/index.jsp?language="+ lang);
rd.forward(request, response);
一切正常!
问题:
现在我需要更改 url 模式。我需要类似的东西-:当我进入 localhost:8080/MyAppl/ 时,我需要被重定向到我的 servlet。 所以我创造了这样的东西:
<url-pattern>/</url-pattern>
好的,它有效!我被重定向到 servlet。 但是这里出了点问题。当 Servlet 创建 RequestDispatcher forward 时,我的 index.jsp 中没有图像和 css。 当我在萤火虫控制台中看到时,我看到了错误:
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8080/MyApp/font/font_big.css". localhost/:15
Resource interpreted as Image but transferred with MIME type text/html: "http://localhost:8080/MyApp/IMG/company.gif".
我该如何解决这个问题?
【问题讨论】:
-
你遇到的问题是你所有的资源路径都以/开头。您需要添加一些东西来处理资源(spring 有一个资源 servlet)并使用通用 URL 模式来识别资源(例如,所有资源都以“/Resourcecde”开头),然后在“之前添加资源处理程序 servlet 映射” /" 在您的 web.xml 文件中映射
-
这演示了弹簧解决方案:stackoverflow.com/questions/6047150/…
-
我不使用 Spring。我只使用 servlet 和 JSP。你能举个例子吗,怎么做?
标签: java jsp servlets servlet-filters servlet-3.0