4

I am developing a website in struts. My folder structure is as follows :

enter image description here

Now, I have a jsp page register.jsp, in which I want to add jquery.validate.js file. I have followed suggestion from the following link :

Can not include javascript file from WEB-INF directory in JSP.

My code was as follows :

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Insert title here</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://use.fontawesome.com/0c2573c7d6.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript"  src='<c:url value="/WEB-INF/scripts/jquery.validate.js"/>'></script>

The above code generated the following html :

<script type="text/javascript"  src='/LoginApp/WEB-INF/scripts/jquery.validate.js'></script>

The jsp file could not access the javascript file jquery.validate.js. How can I access it from my jsp file ?

Regards, Tanvir

Community
  • 1
  • 1
Rumel
  • 319
  • 1
  • 3
  • 19
  • 2
    Any reason why you put your resource in `/WEB-INF/scripts` instead of directly '/scripts' ? – Nicolas Filotto Sep 29 '16 at 09:54
  • You should use something like src="${pageContext.request.contextPath}/scripts/jquery.validate.js" – dsp_user Sep 29 '16 at 09:54
  • @NicolasFilotto I do not know. I am new to struts and java. It has been one week I have started this. Where do you suggest to put the javascript ? Can you please tell me which is the standard in this case ? And how will I access it then ? – Rumel Sep 29 '16 at 09:55
  • 1
    Usually, all the static files(JS, CSS, Images, ...) are placed outside WEB-INF and your private files (JSP, Java classes, configuration files ...) inside WEB-INF. – David SN Sep 29 '16 at 10:10
  • @DavidSN Thanks. Valuable information. From now on I will put static files outside WEB-INF. – Rumel Sep 29 '16 at 10:16

2 Answers2

6

You should move your file jquery.validate.js outside WEB-INF in webapp/scripts as it is somehow a protected directory since resources inside this directory are by default non public, then you will be able to access it using:

<script type="text/javascript" src='<c:url value="/scripts/jquery.validate.js"/>'></script>
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • Thanks. Your solution worked correctly. I have one question. Does the path always start from webapp. I mean why dont we use value="/webapp/scripts/jquery.validate.js />" – Rumel Sep 29 '16 at 10:13
  • 1
    yes the path starts from the webapp directory or more precisely from what would be the root of the context path – Nicolas Filotto Sep 29 '16 at 10:15
4

WEB-INF is not publicly accessible, your HTML page can't access your JS files inside WEB-INF.

Probably the best approach is to move your JS files outside WEB-INF and change the link to that location.

The other alternative, is to generate a <script> tag in the html with the content of the JS file:

<script>
<%@include file="/WEB-INF/scripts/jquery.validate.js" %>
</script>

Please note you are losing the benefits of the browser cache, as you are including the content of the JS files in the HTML every time.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
David SN
  • 3,389
  • 1
  • 17
  • 21