I have custom tag library project (using maven) and have user project that uses my custom tag library. I want to add jsp template to custom tag library project so that jsp tags would be generated according to this template when used in user project. I've tried to locate the jsp template at resources/META-INF and resources/WEB-INF folders of custom tag project and HTTP-request it from custom tag class, but this is not working.
Locating JSPs in the folder resources/META-INF was correct. It have not worked on my case just because of old version of WAR-file with no JSP in it which maven couldn't delete because of the process that held my war-file.
I'm starting to work on an applet that will replace an existing one. Having never developed an applet before, I thought I'd get going with the popular HelloWorld example. I am able to run it a couple different ways: in the appletviewer, and also in a browser if I put the JAR file containing the HelloWorld class in the same directory as the HTML (i.e. http://localhost:8080/myApp). I also got it to work when I put the JAR in a directory called HelloWorld just below the myApp directory and specified the codebase parameter in the <applet> tag as HelloWorld. But when I try to specify WEB-INF directories such as classes or lib, I get a ClassNotFoundException. What am I doing wrong?
But when I try to specify WEB-INF directories such as classes or lib, I get a ClassNotFoundException. What am I doing wrong?
Those directories are only meant for classes/jars that are used in JSP and servlets (i.e. the stuff the server needs). The resources inside them are available to site visitors. In this sense 'visitor' means a User Agent (i.e. a browser) or a client side plug-in (such as Flash or the JRE).
You can confirm this for yourself by pasting the full URL to the Jar in the web browser address bar and hit 'enter' to browser to it. The server will give a message back to the effect 'forbidden'.
See also the WEB-INF info. page which expands:
WEB-INF is the name of a folder found in Java web applications. It is used to store deployment information such as the web.xml, required library files and compiled class files. It is normally not accessible from web. Any files which you want to put on war but do not want to make to public then web-inf is the place where you can keep those files.
I have created two projects 'webutils' and 'website'
In website I have created custom tag library and customTags.tld file for tag reference.
In my jsp's i am using this
<%# taglib uri="/WEB-INF/tags/customTags.tld" prefix="tt"%>
It works fine. and i am able to use
<tt:mytag/>
NOW problem is i want to move my tag library into webutils project. Moving only java package with java-files works, but i want to move customTags.tld file to webutils too. When i do that it does not work. I cannot refer .tld files.
let me know if more clarification on problem is required, as I am assuming lots of things.
I am using gradle & spring-boot.
Forgot to mention few things, 'website' depends on 'webutils'. And I am using gradle.
If you want to redistribute your tag files or implement your custom
tags with tag handlers written in Java, you must declare the tags in a
tag library descriptor (TLD). A tag library descriptor is an XML
document that contains information about a library as a whole and
about each tag contained in the library. TLDs are used by a web
container to validate the tags and by JSP page development tools.
Tag library descriptor file names must have the extension .tld and
must be packaged in the /WEB-INF/ directory or subdirectory of the WAR
file or in the /META-INF/ directory or subdirectory of a tag library
packaged in a JAR. If a tag is implemented as a tag file and is
packaged in /WEB-INF/tags/ or a subdirectory, a TLD will be generated
automatically by the web container, though you can provide one if you
wish.
http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags6.html
So, if using Maven, for example, place the .tld under
src/main/resources/META-INF
I'm developing a simple mail sender as Java EE application.
The project structure is shown as follows:
To properly setup email contents, I need to read the *.vm files placed inside the resource folder, that I supposed to have as path classpath:/templates/mail/*.vm (as with Spring)... But my supposition is wrong!
Which is the right path to use?
Should I have to use the META-INF folder? Is this solution more
java-ee-compliant? In that case, where have I to put the META-INF folder inside my project structure?
Update:
I packaged the project as .war, then I putted the files in:
/src/main/webapp/WEB-INF/classes/templates/mail/
Then:
org.apache.velocity.Template t = myVelocityEngine.getTemplate("classpath:/templates/mail/account_to_confirm.vm",
"UTF-8");
Nonetheless, the app returns an error at runtime:
Unable to find resource 'classpath:/templates/mail/account_to_confirm.vm'
What am I doing wrong?
Just to better understand:
Supposing that I'd like to deploy this app as jar (removing the servlet class, of course): in that case, should I have to edit the folder layout in order to still use the same path into the source code?
I think the problem is due to the prefix classpath:: where did you find that you have to use it?
You might find useful understanding how to initialize VelocityEngine reading Loading velocity template inside a jar file and how Configuring Resource Loaders in Velocity.
If you can, use Classloader.getResourceAsStream("templates/mail/*.vm"); or similar getResourceAsURL method.
If not, take a look at where files from resources are placed inside WAR. In your case, the file should be in /WEB-INF/classes/templates/mail .
Is it possible to store web content (such as JSPs, HTML, images, CSS etc) in a JAR file?
I've been looking at various options at modularising our web applications and this is one possibility.
We are currently using JSF and Facelets for our view technology - I'm thinking it may be possible to write some form of custom view resolver which would examine the classpath rather than a filesystem directory, but I'm not sure this would work.
Any ideas would be appreciated! :)
Update: I should probably clarify. How do you get the web container (such as Tomcat) to load resources from a JAR file? For example, I deploy a .war file with my web application. If I access /index.jsp, the container will try to look in the web content directory for a file named index.jsp.
Is there an easy way to configure your own resource loader using Tomcat or the like so that it searches the classpath as well as the filesystem?
If you are using Maven to build your webapp, you can build a WAR of your resources and overlay that WAR onto your webapp WAR at build time.
The resource WAR containing all of your JSPs, images, CSS, etc. is referred to as an "overlay," and is simply a dependency in your target webapp with the type set to "war."
When you package your webapp, the resource WAR will only copy over non-conflicting files. So, if you have a unique index.jsp in your project, and would like to use that instead of the index.jsp in the overlay, just include it in your target webapp, and Maven will not copy over that resource.
More info on the Maven War plugin page about overlays.
Yes, it is possible to store files e.g. properties, xml, xslt, image etc; in a JAR (or WAR) file and pull them at runtime.
To load a resource from your deployment jar, use the following code.
this.getClass().getClassLoader().getResourceAsStream( filename ) ;
In a maven project, folders & files placed in resources are included in the jar. The filename is relative to the root of jar file, so "./filename.xml" would match the file filename.xml placed in "/src/java/resources".
Absolutely. Heck, you can store content directly in a WAR file, which is basically a JAR file with a few extra bits. Yes, you may need to write a custom resolver to use ClassLoader.getResourceAsStream, but basically as you're given the ability to generate the content however you like, fetching it from a jar file seems perfectly reasonable. You'll probably want to make sure it only fetches a very specific set of extensions though :)
You can also use the weblets project (see https://weblets.dev.java.net/).
You store some resources in a JAR library (such as images, css, javascript...) and you write a really simple weblet-config.xml. Then in the JSF page, you can refer them directly with this syntax:
<h:graphicImage src="weblet://some-name/images/someimage.jpg" .../>
A tag file is like a JSP fragment that can be placed in a jar. Using tag files, could help you, but I have never tried to use images, CSS, etc. in a jar.
In Core JavaServer Faces, 3rd edition, under "Packaging Composite Components in JARs" on p. 382, it talks about packaging composite components in JAR files.
"All you have to do is put your composite component, and its artifacts, such as JavaScript, stylesheets, or properties files, under a META-INF directory in the JAR, as shown in Figure 9-14."
components.jar
+-- META-INF
+-- resources
+-- css
| +-- styles.css
+-- images
| +-- back.png
+-- util
+-- icon.xhtml
+-- login.js
+-- login.properties
I'm not sure how easily these resources can be accessed directly from other applications as opposed to the contained composite components.