使用“/”创建servlet url-pattern

我创建了名为MainContent的servlet。 我有这样的映射

 MainContent MainContent ge.test.servlet.MainContent   MainContent /main  

所以,当我转到link: // localhost:8080 / MyAppl / main时,我进入了servlet的doGet()方法。 然后我创建RequestDispatcher转发到index.jsp。

一切正常!

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

一切正常!

题:

现在我需要更改url-pattern。 我需要这样的东西 – 当我进入localhost:8080 / MyAppl /我需要被重定向到我的servlet。 所以我创造了类似的东西:

/

好的,它的确有效! 我被重定向到servlet。 这里发生了一些错误。 当Servlet创建RequestDispatcher时,我的index.jsp中没有图像和css。 当我在firebug控制台中看到时,我看到了错误:

 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模式,它会导致您的问题。

使用

  MainServlet   

代替。 这是“servlet 3.0方式”。

来源

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

[2] 如何映射“根”Servlet以便其他脚本仍然可以运行?