This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes display html in jsp using struts action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "display html in jsp using struts action" Watch "display html in jsp using struts action" New topic
Author

display html in jsp using struts action

paresh doshi
Ranch Hand

Joined: Nov 20, 2006
Posts: 81
I have a struts action. I am getting a url from database which consist the path of html file on server. I want to display that file on my jsp.

Can anyone help out with a sample code. How to render the html page on my struts forward.

Please help...its urgent.


Thanks.<br />Paresh Doshi<br />SCJP 1.4
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Add the url in the request attribute and take it out in the JSP to include the html in the JSP.

You could also do a redirect in your action to the html page from your action class.


Amit Tank
Linked In
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You could simply define a local forward that forwards to the HTML page and have your action class find and return that forward. There's no law that says a forward has to be a JSP.


Merrill
Consultant, Sima Solutions
paresh doshi
Ranch Hand

Joined: Nov 20, 2006
Posts: 81
Hi Amit,

As per your suggestion, i have forwarded to an jsp which has included the html..

but it is not able to get the absolute path of the html file. I have used the following tag.

<%@ include file="C:\Javaranch.html" %> //my file is in C:\Javaranch.html

but it is not able to get the path as it is searching in the forward path jsp folder.

Can you please help out.
swapnil dangore
Ranch Hand

Joined: Jun 05, 2006
Posts: 46
Hi paresh,
execute method requires an ActionForward object....I think this might help you...


Cheers<br />-------------<br />Swapnil<br /> <br />SCJP5-81%<br /> <br />"Dictionary is the only place where Success come before Work"
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by paresh doshi:
my file is in C:\Javaranch.html

None of the techniques suggested by any of us will work unless the html file is in the context root of your application or one of its subdirectories.

If you need to display a file that is outside of the context root, you would need to use the classes in the java.io package to read the file and output it to the HTTPServletResponse object's output stream.
paresh doshi
Ranch Hand

Joined: Nov 20, 2006
Posts: 81
Hi Merrill,

Thanks for your reply. I have tried in similar manner using File objects. I have used the following code.

response.setContentType("text/html");
File f = new File ( "C:\\jboss-4.0.3SP1\server\\default","Javaranch.html");
FileInputStream fis = new FileInputStream(f);
byte[] btData = new byte[(int)f.length()];
int iBytesRead = fis.read(btData);
System.out.println("Before Creating OutputStream");
ServletOutputStream out = response.getOutputStream();
DataOutputStream out = new DataOutputStream( res.getOutputStream() );
System.out.println("Before Writing OutputStream");
out.write( btData);


This code is rendering the html file, but the images are not loaded.

Can you help me out in that... or is there any other way to render html file(lying in JBOSS_HOME location or any absolute path) using struts action.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all, is there a reason these files have to be outside the context root? It would be so much simpler to deal with them if you could just move them inside the context root.

You run into the same problem with image files outside the web context root as you do with html files. You will have to create an Action class that will read an image file and output it to the HTTPServletResponse object's output stream.

Then in your HTML file, you will have to reference the Action class you created in your <img> tag. Example:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: display html in jsp using struts action
 
Similar Threads
Problems deploying struts 2 app on weblogic 10gR3
Struts Application to a Portlet
display value from resources file on jsp with struts tags?
JSP---> Struts
JSP---> Struts