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?

3
  • 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 Commented Aug 23, 2013 at 17:52
  • this demonstrates the spring solution: stackoverflow.com/questions/6047150/… Commented Aug 23, 2013 at 17:54
  • I dont use Spring. I use only servlets and JSP. can you give me an example, how to do that? Commented Aug 23, 2013 at 18:00

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

So, use empty as the special url pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.