用"/"创建servlet url-pattern. [英] create servlet url-pattern with "/"

查看:113
本文介绍了用"/"创建servlet url-pattern.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为MainContent的servlet.而且我有这样的映射

I've created servlet named MainContent. and I have such mapping

<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 我进入servlet的doGet()方法.然后我将RequestDispatcher创建到index.jsp.

so, when I go to the link: //localhost:8080/MyAppl/main I enter into the servlets doGet() method. Then I create RequestDispatcher forward to the index.jsp.

一切正常!

RequestDispatcher rd = context.getRequestDispatcher("/index.jsp?language="+ lang);
rd.forward(request, response);

一切正常!

问题:

现在,我需要更改url模式.我需要这样的东西-:当我输入 localhost:8080/MyAppl/时,我需要重定向到我的servlet. 所以我创建了这样的东西:

Now I need to change url-pattern. I need something like that-:when I enter to the localhost:8080/MyAppl/ I need to be redirected to my servlet. So I create something like that:

<url-pattern>/</url-pattern>

好的,它可以正常工作!!我已重定向到servlet. 但是这里发生了错误.当Servlet向前创建RequestDispatcher时,我的index.jsp中没有图像和CSS. 当我在Firebug控制台中看到时,我看到了这些错误:

ok, it works! I'm redirected to the servlet. but something wrong happend here. when Servlet created RequestDispatcher forward , there was no images and css in my index.jsp. when I see in the firebug console, I've seen that errors:

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

我该如何解决?

推荐答案

是的,就像@DwB指出的那样,"/"上下文是有问题的URL模式,它会引起您的问题.

Yes, like @DwB pointed, '/' context is problematic URL pattern and it causes your problem.

使用

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

相反.这是"Servlet 3.0方式".

instead. It is "the servlet 3.0 way" to do this.

来源

[1] http://www. coderanch.com/t/366340/Servlets/java/servlet-mapping-url-pattern

[2]

这篇关于用"/"创建servlet url-pattern.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆