• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Maven Netbeans jar file include in jsp

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

How would I go about including a jar file that I added as a dependency on netbeans maven project on my jsp file?

the path is something like C:\user\user\.m\repository\webjars\jquery\3.3.1-1\jquery-3.3.1-1.jar
its a maven dependency.

I would like to include this onto my jsp page.


Thanks,
Ed
 
Saloon Keeper
Posts: 15487
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you have to do anything special. Just declare the dependency in Maven and then just add something like this to your JSP:
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a Maven project. The fact that you're editing it under NetBeans is secondary. The same rules would apply if you were using Eclipse as your editor.

Maven's normal action for a dependency marked as run-time when executing the "war" goal is to copy it from the cached repository to the webapp's target WEB-INF/lib directory.

But there are 2 problems with that.

First, anything under WEB-INF is invisible to client URL access. Also, URLs cannot resolve directly members of archive files such as JAR files, only discrete files and directories.

Second, JavaScript doesn't work on JARs, only on text. And, as I just mentioned, the text cannot be inside a JAR in that case.

So there's something missing. There should be a servlet in your webapp that can resolve a URL to return a script(s) from the jar. At least that's what happens when you build JSF webapps, where JSF itself is working with jars full of JavaScript.

If that sort of mechanism isn't an option, then the alternative would be to manually pull the scripts from the jar in the Maven repository and copy them to a suitable subdirectory under the Maven src/main/webapp directory. For example, if the JAR contained "/jquery-min.js", then if you copied it to PROJECT/src/main/webapp/scripts/jquery-min.js", the context-relative URL would be "/scripts/jquery-min.js". Assuming that no servlet was mapped to handle a URL beginning with "/scripts/".
 
Stephan van Hulst
Saloon Keeper
Posts: 15487
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe Eddy is using WebJars.
 
Stephan van Hulst
Saloon Keeper
Posts: 15487
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the example code, you would use something like this in a JSP:
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I see. But it's a little trickier than that.

For Servlet 2 containers (for example, Tomcat 6), you would indeed have to include a resolving servlet. WebJars provides one and you'd code the URL path to use it. That would also require require mapping a URL pattern, for example, map "/webjars" to the webjars servlet.

Servlet 3 is different, however. I'd wager money that you could actually retro-fit Tomcat 6's default servlet, but why bother - user Tomcat 7 or later (or some other Servlet 3 container).

In Servlet 3, you do not have to provide a servlet mapping, so the resource is mapped to the root of the webapp. Any fine-tuning is optional.

I think that the webjars docs for Servlet 3 are defective. Based on what I saw of the static resource spec, the correct URL for their resource would not be:

Instead it should be:

Aside from the improper use of single-quotes in the tag, the Servlet 3 docs would imply that the JAR path /META-INF/resources/css/bootstrap.min.css would cause /css/bootstrap.min.css to be presented as a public URL.

Note that I haven't address the higher-level parts of the URL (server and context root and relative versus absolute URL syntax). Just the internal resource path. I'll leave the rest to you.

Incidentally, the attribute syntax using the subtag "wj:locate" is presumably a custom JSP tag injected by the webjars infrastructure and which probably renders a webjars servlet path. I didn't find anything about it on the webjars site, though.

WebJars appears to be redundant in Servlet3, since the container's default servlet can already serve up WEB-INF/lib jar resources. Its primary virtue would appear to be for Servlet2 containers. So maybe their Servlet 3 example is actually correct, but is injecting and mapping a webjars servlet even though it's not necessary in Servlet 3. Which would presumably be for backwards compatibility.
 
Eddy Haryanto
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Thanks for the replies.
I am glad that this forum is alive and kicking!

Eddy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic