grep
grep

Reputation: 5623

create servlet url-pattern with "/"

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>

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.

everything works!

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

everything works!

Question:

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>

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

How can I fix that?

Upvotes: 2

Views: 1794

Answers (1)

mico
mico

Reputation: 12738

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

Use

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

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

Sources

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

[2] How can I map a "root" Servlet so that other scripts are still runnable?

Upvotes: 2

Related Questions