Solved

JSP image relative path problem

Posted on 2003-10-21
10
2,117 Views
Last Modified: 2012-05-04
Hi,

I have a JSP which writes out HTML file using out.write(). The path to images in the HTML are relative to the HTML file and hence they don't get displayed as the browser is trying to figure it out using the JSP's cuurent path.

Is there a way for me to let the browser know the correct path based on which it should compute the image's complete location.

I have to use the out.write() as the HTML is read in and updated by the JSP.

Thanks
- Kamal
0
Comment
Question by:xviper
  • 4
  • 4
  • 2
10 Comments
 
LVL 19

Expert Comment

by:cheekycj
ID: 9592303
what is your current out.write() statement

I would use something like

out.write("<img src=\"../relative/path/to/img.jpg\">");

CJ
0
 

Author Comment

by:xviper
ID: 9592844
Here is what the out.write looks like

FileInputStream in = new FileInputStream('../path/to/afile.html');
while((lenght  = in.read(buffer)) != -1) {
   String s = new String(buffer,0,lenght);
   out.write(s);
}

I have to do it this way because, my future code will be modifying the file data after reading it in.

the 'afile.html' has <img src="someimg.jpg"> in it. The src is relative to 'path/to/' where the html file is located. But the jsp making the call to out.write is in some other location and hence the browser is unable to render the images.

-

0
 
LVL 3

Expert Comment

by:applekanna
ID: 9592874
  //to include the header page in my app we use
 <%
    ServletContext servletContext = getServletContext();
    RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/folderinROOTdir/fileName.html");
    requestDispatcher.include(request,response);

%>
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:xviper
ID: 9593044
It would help if I knew a way to transfer the data (html file read in and modified) from one JSP to another in a chaining fashion. In that case I can have JSP in the same location as the HTML and it would just output the data it gets from another JSP. The browser would take location relative to the JSP in the same location as the HTML and things would work out just fine.

- Kamal
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 9593078
do a forward.
    RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/fileName.jsp");
    requestDispatcher.forward(request,response);

now that jsp can forward to another too.

CJ
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 9593088
if file.html has the img src and a jsp/servlet is reading it in from a completely different path than I would suggest changing your img src to full path http://yousite.com/imgs/img.jpg

CJ
0
 

Author Comment

by:xviper
ID: 9593202
I cannot change the image src as the HTML along with the images are uploaded by the user (in a zip file which I unzip). I don't want to put the HTML parsing and processing code in a JSP that gets duplicated in each of the directory containing the HTML.

I figure, if I could some how process the HTML and the send the processed HTML to a simple JSP whose job is to just output the processed HTML, then it might solve my problem.  I was thinking in the link of chaining, using requestDispatcher.forward(req,res).

But,

1. I am not sure how to pass large file from one JSP to another (can I use setAttribute() and getAttribute()? Will it be too large to put the entire HTML file data in them)

2. If there is a better solution, as the only purpose the second JSP is for setting the browser's relative path to the same place where the HTML is.

- K
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 50 total points
ID: 9593235
can you map your serlvet to something that in the user's directory?

You can attach the file to the request object and retreive it.

using setAttribute and getAttribute().  I have attached full html onto the request.

I don't understand the second point.

CJ
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9593315
>img src to full path http://yousite.com/imgs/img.jpg

expanding on that suggestion -

put all your image in a folder under ROOT/images/
as ROOT/images/a.gif
    ROOT/images/b.gif etc.

In your html whenever yourefer an image call it as

<img src="http://www.ursite.com/images/a.gif">


0
 

Author Comment

by:xviper
ID: 9593354
I got it to working with sending the enitre HTML(after processing) in BufferedInputStream format using setAttribute and getAttribute and sendRedirect to the second JSP which just outputs the BufferedInputStream. But since the second JSP is located in the same path as the HTML page, the image links work.

CJ, I think you should get the points for all the trouble and help.

- K
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven project import to eclipse problems 13 104
build fail in maven project 11 184
Spring MVC - sending raw charset to backend 3 199
JavaScript parse form with colon separated values 2 77
Learn how the use of a bunch of disparate tools requiring a lot of manual attention led to a series of unfortunate backup events for one company.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now