Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

javascript - Unable to load java script files in Jetty webapplication

I have a very simple webapp where I am trying to plot graphs using flot javascript library. My JSP file goes this way where I am importing the required *.js files as follows.

Relative to the hello.jsp file at WEB-INF/pages/hello.jsp, scripts directory with the relevant .js files are at ../../scripts (i.e. directly under ROOT)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Flot Examples: Categories</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
    <script language="javascript" type="text/javascript" src="../../scripts/jquery.flot.js"></script>
    <script type="text/javascript">

    $(function() {
     //code draw the graph 
    </script>
</head>
<body>
        <h2>Categories</h2>
        <div id="placeholder" style="width:788px;height:400px"></div>
        <h3>End of Page</h3>
</body>
</html>

Directory structure is as follows root@domU-12-31-39-13-C5-F5:/usr/local/jetty/webapps# tree

.
|-- README.TXT
`-- ROOT
    |-- META-INF
    |   |-- MANIFEST.MF
    |   `-- maven
    |       `-- com.mkyong.common
    |           `-- SpringMVC
    |               |-- pom.properties
    |               `-- pom.xml
    |-- scripts
    |   |-- jquery.colorhelpers.js
    |   |-- jquery.flot.time.js
    |   |-- jquery.flot.time.min.js
    |   |-- jquery.js
    |   |-- jquery.min.js
    |   |-- LICENSE.txt
    |   |-- Makefile
    |   |-- NEWS.md
    |   |-- PLUGINS.md
    |   `-- README.md
    `-- WEB-INF
        |-- classes
        |   `-- com
        |       `-- mkyong
        |           `-- common
        |               `-- controller
        |                   `-- HelloController.class
        |-- hello.html
        |-- lib
        |   |-- aopalliance-1.0.jar
        |   |-- commons-logging-1.1.1.jar
        |   |-- spring-aop-3.0.5.RELEASE.jar
        |   |-- spring-asm-3.0.5.RELEASE.jar
        |   |-- spring-web-3.0.5.RELEASE.jar
        |   `-- spring-webmvc-3.0.5.RELEASE.jar
        |-- mvc-dispatcher-servlet.xml
        |-- pages
        |   |-- hello.html
        |   `-- hello.jsp
        `-- web.xml

Observations

  1. Using firefox eclipse, I can see that browser is requesting for GET http://ec2-184-73-14-57.compute-1.amazonaws.com:5809/scripts/jquery.flot.js but unable to find it hence fails with 404 Not found
  2. Moving the scripts directory to under WEB-INF and changing the relative paths is also resulting in the same error.
  3. Similar application deployed in tomcat is picking .js files from ROOT directory when referenced as /scripts/*.js
  4. Unable to reference the js files using absolute path and when I do, it tries to append the absolute path to http://ec2-184-73-14-57.compute-1.amazonaws.com:5809/ and trying to fetch it .. thus resulting in 404 error

Never used Jetty before, is this problem specific to Jetty? What is a better way to import javascript files. Please advice

Here is the dump after the start of the webapp

 2013-04-12 19:08:04.829:INFO:oejs.ServerConnector:main: Started ServerConnector@3ae0e5b7{HTTP/1.1}{0.0.0.0:5809}
    org.eclipse.jetty.server.Server@6a7ef378 - STARTING
     += qtp578065504{STARTED,10<=10<=200,i=8,q=0} - STARTED
     |   +- 12 qtp578065504-12-selector-0 RUNNABLE @ sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
     |   +- 13 qtp578065504-13-acceptor-0-ServerConnector@3ae0e5b7{HTTP/1.1}{0.0.0.0:5809} RUNNABLE @ sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
     |   +- 14 qtp578065504-14 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
     |   +- 15 qtp578065504-15 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
     |   +- 16 qtp578065504-16 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
     |   +- 17 qtp578065504-17 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
     |   +- 18 qtp578065504-18 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
     |   +- 19 qtp578065504-19 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
     |   +- 20 qtp578065504-20 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
     |   +- 21 qtp578065504-21 TIMED_WAITING @ sun.misc.Unsafe.park(Native Method) IDLE
     += org.eclipse.jetty.util.thread.ScheduledExecutorScheduler@7c8b3bd8 - STARTED
     += org.eclipse.jetty.server.handler.HandlerCollection@582b0e7b - STARTED
     |   += org.eclipse.jetty.server.handler.ContextHandlerCollection@7e999262 - STARTED
     |   |   +~ org.eclipse.jetty.jmx.MBeanContainer@3a55b9b
     |   |   += o.e.j.w.WebAppContext@2f7e1b09{/,file:/usr/local/osmosix/jetty/webapps/ROOT/,AVAILABLE}{/ROOT} - STARTED
     |   |       += org.eclipse.jetty.server.session.SessionHandler@8a6bf88 - STARTED
     |   |       |   += org.eclipse.jetty.server.session.HashSessionManager@3620f49e - STARTED
     |   |       |   += org.eclipse.jetty.security.ConstraintSecurityHandler@5daa3e56 - STARTED
     |   |       |   |   +- org.eclipse.jetty.security.DefaultAuthenticatorFactory@12959ac0
     |   |       |   |   += org.eclipse.jetty.servlet.ServletHandler@72140bcb - STARTED
     |   |       |   |   |   += default@5c13d641==org.eclipse.jetty.servlet.DefaultServlet,0,false - STARTED
     |   |       |   |   |   |   +- maxCacheSize=256000000
     |   |       |   |   |   |   +- etags=true
     |   |       |   |   |   |   +- dirAllowed=true
     |   |       |   |   |   |   +- gzip=true
     |   |       |   |   |   |   +- maxCachedFileSize=200000000
     |   |       |   |   |   |   +- redirectWelcome=false
     |   |       |   |   |   |   +- acceptRanges=true
     |   |       |   |   |   |   +- welcomeServlets=false
     |   |       |   |   |   |   +- aliases=false
     |   |       |   |   |   |   +- useFileMappedBuffer=true
     |   |       |   |   |   |   +- maxCachedFiles=2048
     |   |       |   |   |   +- [/]=>default
     |   |       |   |   |   += jsp@19c47==org.apache.jasper.servlet.JspServlet,0,false - STARTED
     |   |       |   |   |   |   +- logVerbosityLevel=DEBUG
     |   |       |   |   |   |   +- fork=false
     |   |       |   |   |   |   +- scratchdir=/tmp/jetty-0.0.0.0-5809-ROOT-_-any-/jsp
     |   |       |   |   |   |   +- xpoweredBy=false
     |   |       |   |   |   +- [*.jsp, *.jspf, *.jspx, *.xsp, *.JSP, *.JSPF, *.JSPX, *.XSP]=>jsp
     |   |       |   |   |   += mvc-dispatcher@f974527a==org.springframework.web.servlet.DispatcherServlet,1,true - STARTED
     |   |       |   |   |   +- [/]=>mvc-dispatcher
     |   |       |   |   |   +~ org.eclipse.jetty.jmx.MBeanContainer@3a55b9b
     |   |       |   |   +~ org.eclipse.jetty.jmx.MBeanContainer@3a55b9b
     |   |       |   |   +~ HashLoginService[Test Realm] - STARTED
     |   |       |   |   +- org.eclipse.jetty.security.DefaultIdentityService@77df3248
     |   |       |   |   +- org.eclipse.jetty.security.authentication.BasicAuthenticator@652520bb
     |   |       |   |   |
     |   |       |   |   +> HashLoginService[Test Realm] - STARTED
     |   |       |   |   +> org.eclipse.jetty.security.DefaultIdentityService@77df3248
     |   |       |   |   +> org.eclipse.jetty.security.authentication.BasicAuthenticator@652520bb
     |   |       |   |   +> []
     |   |       |   |   +> /={TRACE={RoleInfo,F,C[]}}
     |   |       |   +~ org.eclipse.jetty.jmx.MBeanContainer@3a55b9b
     |   |       += org.eclipse.jetty.servlet.ErrorPageErrorHandler@4ec93402 - STARTED
     |   |       |   +~ org.eclipse.jetty.jmx.MBeanContainer@3a55b9b
     |   |       +~ org.eclipse.jetty.jmx.MBeanContainer@3a55b9b
     |   |       |
     |   |       +> WebAppClassLoader=Spring Web MVC Application@bf6140f
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/classes/
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/spring-aop-3.0.5.RELEASE.jar
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/spring-beans-3.0.5.RELEASE.jar
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/spring-expression-3.0.5.RELEASE.jar
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/aopalliance-1.0.jar
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/spring-context-support-3.0.5.RELEASE.jar
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/spring-web-3.0.5.RELEASE.jar
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/spring-core-3.0.5.RELEASE.jar
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/spring-webmvc-3.0.5.RELEASE.jar
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/spring-asm-3.0.5.RELEASE.jar
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/spring-context-3.0.5.RELEASE.jar
     |   |       |   +- file:/usr/local/osmosix/jetty/webapps/ROOT/WEB-INF/lib/commons-logging-1.1.1.jar
     |   |       |   +- startJarLoader@518cb416
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jetty-xml-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/servlet-api-3.0.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jetty-http-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jetty-continuation-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jetty-server-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jetty-security-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jetty-servlet-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jetty-webapp-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jetty-deploy-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jetty-client-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jetty-jmx-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jsp/com.sun.el-2.2.0.v201108011116.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jsp/javax.el-2.2.0.v201108011116.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jsp/javax.servlet.jsp.jstl-1.2.0.v201105211821.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jsp/javax.servlet.jsp-2.2.0.v201112011158.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jsp/org.apache.jasper.glassfish-2.2.2.v201112011158.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jsp/org.apache.taglibs.standard.glassfish-1.2.0.v201112081803.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/jsp/org.eclipse.jdt.core-3.7.1.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/resources/
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/websocket/websocket-api-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/websocket/websocket-common-9.0.0.v20130308.jar
     |   |       |       +- file:/usr/local/osmosix/jetty/lib/websocket/websocket-server-9.0.0.v2013030

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a bad servlet-mapping setup.

The servlet named mvc-dispatcher is setup at url-pattern of "/", also known as the servlet spec default servlet path spec. Something that the web container itself provides to allow it to serve static content (like css and javascript files).

With your mvc-dispatcher at "/" that means the Default Servlet itself cannot serve static content.

That also means that org.springframework.web.servlet.DispatcherServlet is now responsible for serving all content from the WebApp. This is an atypical servlet-mapping and I doubt that's the role of the spring DispatcherServlet.

Consider using a suffix based servlet path spec like "*.html" or a prefix based servlet path spec like "/*" (this is outlined in the Section 12 of the Servlet Spec)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...