Linking Files And Directory Structuring - java

I'm working on a web project using java/ jsp/ servlets/ html/ css in eclipse tomcat, where everything is in the WebContent folder.
In my jsp files... When I try to include other jsp files (using a link like "/fileName.jsp" in jsp include directive) I can do that successfully.
But When I try to include image files (using a link like "/fileName.jpg" in the <img src=""> tag) nothing happens.
Nothing happens because instead of looking in the WebContent folder for image file it looks in the tomcat home directory, i.e.
Instead of looking at "http ://localhost:port/projectName/..." it looks at "http: //localhost:port/..."
Why does it look at the wrong location only with <img src=""> tags but not in <%# /> tag.
A workaround for this is that I start giving absolute paths "/projectName/..." However doing this means I'm hardcoding project name everywhere. This is what I do not want.

Don't include binary content in an ascii output. Why not just use the img tag? If you need to do something to produce a jpeg, I would use a Servlet.

Because the jsp-Links in the website are getting processed and the image links not. Either change the image path or develop an filter that changes the images'links.

Yes Templar, that could have been a way to solve my problem.
However, I simply changed the Context Root of my project from "Project Name" to "/" in Eclipse. This solved my problem.

Related

Render image in SpringMvc

I want my image show up in receipt_failure.html via such thymeleaf expression :
<img th:src="#{/src/main/resources/images/icons-alert-circle.png}" />
Image itself has settled in such SpringMVC folder structure:
I do not get where issue comes from because seems like I keep path structure but I am getting such error on my local environment
I moved /images folder into /static folder then fix html like this:
<img th:src="#{/images/icons-alert-circle.png}" />
and issue has been fixed

cant read files from resources

Im trying to read files(xml, images) from src/main/resources. But it doesnt work.My resources folder is Source folder. For example, when i m trying to read log4j.xml tomcat looking for it in C:/bin... And i also cant read images from resources. I can read it only from webapp. I ve read, that tomcat automatically replace files from resources to webinf, but i think that it doesnt work in my case.
Please, help. I dont have any idea.
Even if the png is added to the WEB-INF folder it wouldn't be accessible from the JSP page in your case.
On runtime a JSP page renders into a servlet that return HTML code in the response. Then a browser will parse your tag <img src="/Pajero.png" ... /> and send a request onto YOUR_HOST/Pajero.png url to access the image and get the error code 404, because content of the WEB-INF folder is not accessible for a client side in a java web application.
Perhaps it will be better to place images on the webapp folder at your case.
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("log4j.xml").getFile());
You can use something like this to get resources from classpath.
Regards

unable to locate exact location of image file in jsp page

i am saving my images through java coding in a folder called "files".
and in my jsp page i am trying to display that image using
<img alt="" src="${pageContext.request.contextPath}/files/IMG168.jpg"> But the image is not displaying! What could be the reason.
Please see the below images and suggest me solution for my problem.
You should put the files folder in WebContent directory.
Web pages contains as below
Web pages
|______page1.jsp
|______index.jsp
|______page2.jsp
|______WEB-INF
|______images
|______css
|
Just try this and see
<img alt="" src="../../files/IMG168.jpg">
Updated,
I think you have to get physical location of files folder and put it in to src. Example if your files folder in C:\visionbook\files your
src="C:\visionbook\files\IMG168.jpg"

JSP Template tutorial. How do I use a jar file

I am trying to follow the tutorial for JSP Templates at:
http://java.sun.com/developer/technicalArticles/javaserverpages/jsp_templates/
I am struggling to understand the <%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %> tag.
What is the template.tld file, where does this come from?
I have tried to download the Resourses file of source code but this just contains a src.jar file. How do I use this? I can't even open the file!?! How do I see the source code example?
It's in the jstl.jar. You need standard.jar and jstl.jar in your WEB-INF/lib for JSTL.
You can get what you need here:
http://tomcat.apache.org/taglibs/
Download the standard tag library and all its JARs; put them in your WEB-INF/lib directory.
A uri, a uniform resource identifier is not necessarily pointing to any existing resource. It is an identifier, compare with url which is a uniform resource locator which helps you locate something. In many XML contexts for instance, it simply declares a namespace.
Sometimes the uri does however point to a real resource that you can read to get more information.
In this case you are declaring a namespace template to use in your jsp code (such as <template:dosomething/>) - the uri matches a uri declaration for whatever is implementing your tag functionality and can be found inside a jar (jstl.jar or standard.jar, can't remember which).

Deploying a jar in Jboss

I have some .jspf files that are fragments which I include in a new .jsp file. The reason they are fragments is that they are reused across multiple jsps with some additional components.
My issue now is that I want to use these .jsps in 2 different .war files.
So I created a new .jar file which includes these jspf, now I am trying to deploy this jar so that I am then import it in my new jsp which is inside 1 of the 2 wars.
I am not able to deploy the jar succesfully in Jboss 4.2. I am using Eclipse ide.
Any ideas on this? If there is an alternate approach I would appreciate any ideas.
Thanks for this suggestion, I have followed this idea and deployed my war1 which contains my .jspf files.
So in my war2 on a jsp I do:
<c:import context="/sharedComponents" url="/easyPayNamePaymentOption.jspf" var="easyPayName"/>
<%# include file="easyPayNamePaymentOption.jspf" %>
However I am not able to render this page with the included fragment, I am not sure how to address the imported jspf. I ahve tried several different ways like:
<%# include file="/sharedComponents/easyPayNamePaymentOption.jspf" %>
and also using var name like:
<%# include file="#{easyPayName}" %>
However it keeps looking inside the current war. How can I tell it to include the newly imported fragment and display it?
Thanks in advance.
I can see what you're trying to do, but this isn't going to work with a JAR file. All JSP files (including JSPF) have to inside a WAR, not a JAR.
The simplest solution is to put copies of the JSPF files into each WAR that needs to use them. Assuming that you don't want to do this, then there is an alternative, called a cross-context WAR.
By default, JBoss allows its webapps to request resources from each other. For example, say webapp1 (context path /app1) wants to import JSPF /my.jspf from webapp2 (context path /app2). You can use JSTL to do this, from inside webapp1:
<c:import context="/app2" url="/my.jspf"/>
So if you were to create a "shared" WAR file containing your JSPF files, and deployed this to JBoss, then your other webapps could use the above technique to include the contents of the JSPFs into their own JSPs.
edit: I've read your updated question, and I don't understand why you added var="easyPayName" to the <c:import> tag. All that's doing is importing the contents of easyPayNamePaymentOption.jspf and storing it in a variable called easyPayName, which seems completely unnecessary.
I think perhaps you're associating <c:import> with a java import? If so, don't - they're completely different. <c:import> should really have been called <c:include>, because that's what it does.
Just keep it simple, remove the attribute, and just have
<c:import context="/sharedComponents" url="/easyPayNamePaymentOption.jspf"/>
That's all you need to do - it will include the content of easyPayNamePaymentOption.jspf directly in the JSP.

Categories

Resources