1

I am trying to add js source file to my jspt page, but it is throwing error

GET http://localhost:8084/JvnrAlumniPortal/jsLibs/check.js 404 (Not Found) (index):11 Uncaught ReferenceError: check is not definedonclick @ (index):11

My jsp file is as below:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>JNVR</title>
<script type="text/javascript" src="jsLibs/check.js"></script>
</head>
<body >
<h1>done123</h1>

<input type="button" onclick="check();" />

</body>
</html>

Directory structure looks like,

Folder structure

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
Swanand
  • 29
  • 5
  • can you share a screen of your project-structure (folders and files)? – Christoph-Tobias Schenke Apr 27 '16 at 14:01
  • @Swanand There's problem with the source of the file which is not correct that is why you are getting this error. For future questions, just be very clear what you're asking and be very elaborative to explain your questions, errors and the things you want to achieve, also don't forget to include some screenshots which will help other users to answer you well. – imrealashu Apr 27 '16 at 14:12
  • Sorry for being unclear..... my html file is not able to locate check.js file......... I guess the path i have given is correct...... – Swanand Apr 27 '16 at 14:13
  • Just added the pic – Swanand Apr 27 '16 at 14:13
  • how does the project look like if it is deployed to tomcat or jetty? – Christoph-Tobias Schenke Apr 27 '16 at 14:15
  • I have just started with project and just wanted if the js file is located correctly...... So added one function in js file and trying to call it from onclick even of button on page............ – Swanand Apr 27 '16 at 14:17
  • contents of check.js function check(){ alert('hiii'); } – Swanand Apr 27 '16 at 14:17
  • Is the location for js files OK?? or shall I move them some where else???? – Swanand Apr 27 '16 at 14:26
  • Possible duplicate of [How to use relative paths without including the context root name?](http://stackoverflow.com/questions/4764405/how-to-use-relative-paths-without-including-the-context-root-name) – Alan Hay Apr 27 '16 at 14:47
  • Ensure resources are resolved relative to the toot of the web application and not the current URL. See: http://stackoverflow.com/questions/4764405/how-to-use-relative-paths-without-including-the-context-root-name – Alan Hay Apr 27 '16 at 14:47
  • I read these docs..... also i feel paths given by me are correct but still not working – Swanand Apr 27 '16 at 16:34
  • Thanks guys..... I was able to get the solution.... I had not registered the resources to spring..... – Swanand May 09 '16 at 09:39

1 Answers1

0

I assume that you're using either Tomcat or Jetty (and the url pattern in your web.xml file is set to / ). To serve static content such as Javascript, image or css files, you may need to add a mapping to web.xml for every static resource your web application serves. These static resources will be served by the (web container's) default servlet, not the servlet you create.

//web.xml
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>

Hope this helps.

dsp_user
  • 2,061
  • 2
  • 16
  • 23