4

I use NetBeans and Tomcat 7.0.4.2 and would like to change URL address of my project from localhost:8080/Servlet to localhost:8080/. In web.xml I changed servlet URL address from <url-pattern>/Servlet</url-pattern> to <url-pattern>/</url-pattern>.

The problem is I can't load resource files now and get errors in browser console log:

  Failed to load resource: the server responded with a status of 404 (Not Found) (11:45:14:149 | error, network)
  at src/main/webapp/scripts/aui-min_2.0.0.js

The path to resource files is src/main/webapp/scripts and in JSP file I use this path

<script type="text/javascript" src="scripts/aui-min_2.0.0.js"></script>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>Servlet</servlet-name>
        <servlet-class>socialgraphui.Servlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Matt
  • 8,195
  • 31
  • 115
  • 225

1 Answers1

1

The url pattern / is is used by default servlet which is capable of loading static resources that isn't mapped by other servlets. When you switched to this pattern it's stopped working. So, you should keep a servlet mapping.

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

If you want to start from the index page use a index.jsp that is listed in <welcome-file-list> configuration. The index page can redirect to the servlet using

response.sendRedirect(request.getContextPath() +"/Servlet");

To load static resource use servlet context path like that

<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/aui-min_2.0.0.js"></script>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Thanks for response Roman, but after that tomcat deploys my project in a loop and can't stop – Matt Nov 11 '14 at 13:16
  • Ok, redeploy it again. – Roman C Nov 11 '14 at 14:21
  • Thanks Roman, but it still doesn't work. This is my new web.xml http://pastebin.com/ZZjQ0EgE and now i can load my sources on localhost:8080/ address, but with this solution i can't pass variables from Servlet to index.jsp file. In Servlet class i tried to replace request.getRequestDispatcher("index.jsp").forward(request, response); by response.sendRedirect("/Servlet"); in processRequest method, but still can't pass variables. – Matt Nov 11 '14 at 16:49
  • The `index.jsp` should have only code `<%response.sendRedirect("/Servlet");%>`. But you can dispatch to another page, to avoid an endless loop. – Roman C Nov 11 '14 at 22:43