2

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?

grep
  • 5,465
  • 12
  • 60
  • 112
  • The problem you are encountering is that all your resource paths start with /. You need to add something to handle resources (spring had a resource servlet for that) and using a common URL pattern to identify the resources (for example, all resources start with "/Resourcde") then adding the resource handler servlet mapping before the "/" mapping in your web.xml file – DwB Aug 23 '13 at 17:52
  • this demonstrates the spring solution: http://stackoverflow.com/questions/6047150/using-spring-resourceservlet-to-serve-multiple-resources-simultaneously – DwB Aug 23 '13 at 17:54
  • I dont use Spring. I use only servlets and JSP. can you give me an example, how to do that? – grep Aug 23 '13 at 18:00

1 Answers1

2

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?

Community
  • 1
  • 1
mico
  • 12,730
  • 12
  • 59
  • 99