Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

url link in a portlet

 
Rick Murphy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone:

Please shed some light on this one:

I need to create a link in a porlet to point to a pdf file in the application directory, to have the same effect that I would get from a link in a regular page. I know that url doesn't mean much for a portlet since a portlet is within a portal container. It seems like that the options left are limited: create a controller or use a redirect. But what do I put in there? It is OK to forward to a jsp or html, but I am asking for a pdf or word doc.

some sample code would help.


Thank you.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Mister XY"-
Congrats! You win the prize for the first "can you please fix your display name" post in the new Portal/Portlets forum.

I'm psyched you're checking out the new forum, and are interested in Portlets. I'm just getting into them myself. But, can you do me a favor and adjust your displayed name to meet the JavaRanch Naming Policy?
You can change it here.

In the mean time, I'll see if I can find an answer to your question...

- Jess
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about this -->

use request.getContextPath() to get the path of your web application, then you can use that to build the relative URL for your PDF file

String pdfPath = request.getContextPath() + "/myFiles/blah.pdf";

Then just generate a link in your application to that URL and it can be accessed directly -- is that what you're looking for?
 
Rick Murphy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK Jess:

Do I get a award by doing that? you see I already corrected...

Well, to your suggestion, kind of. I am new to this portlet stuff so bear with me a little...

As I learned from internet, I put my jsp and all other directories such as myFiles underneath /WEB-INF/, say /WEB-INF/myFiles/blah.pdf.

This is not working for me in a portlet environment, since I don't seem to find a way to point to there? In your reply what do you mean by ...just generate a link in your application to that URL and it can be accessed directly... could you elaborate it? How to generate a link? I think that might help. I am doing all this in JSP right? Thank you again!!!

Ricky
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't directly serve up anything in your /WEB-INF directory, as per the spec.
SRV 9.5: "Also, any requests from the client to access the resources in WEB-INF/ directory must be returned with a SC_NOT_FOUND(404) response."
Meaning if you try to go to a URL like "http://host/webapp/WEB-INF/resources/blah.pdf" you'll get a 404 error.

So you've got two options as I see it (and please verify everything I say -- I'm just getting back into all this stuff).

1- move your PDF (and any other resources you'd like to serve up, outside the /WEB-INF directory... so you'll be like this:


Then in your JSP, you can create the link to your pdf like this


2- Read in the PDF and store it as a byte array. Store the byte array into session, and then in your portlet, write the byte array to the output stream. With this solution, you can (and should) keep your resources in the /WEB-INF directory, this way they can't be accessed directly from a URL, and are only served up by your web application.



Note: These aren't really portlet-specific answers, more general to a webapp. However, I believe they do help fix the problem that you don't know the URL of the page you're currently on -- so relative links don't work.

P.S. Thanks for tweaking your display name -- its much appreciated. I wouldn't want to get all mean and nasty on my first week in the new forum
 
Rick Murphy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jess:

What you described was absolutely right. I used that kind of directory in a regular web application. Yes, anything beyond /WEB-INF/ is prohibited from direct access via http.

My apology that I need to say more in my original post... When I looked at some articles from the internet, sorry can't recall where. To avoid direct http access from portal we purposely move jsp folder behind /WEB-INF/ and use xml file to control controller's behavior (in MVC model). For example, in struts-config.xml you may forward a success action to /WEB-INF/jsp/myJSP.jsp. That is where I got the idea. In stead of a http call, it seems like some mechanism there treats this as a directory path, and an url was then created somehow meaningful for the portal container. If you print out that URL, it is totally meaningless.

All those require user to call a controller class (action class) from a link and then do a forward. It would be easier if you can get a simple url link generated using some function takes /WEB-INF/myFiles/blah.pdf as input and generate a url that is meaningful to portal.

Thank you for your help. I do appreciate it.


Rick
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're exactly right re: MVC/controller/etc.

In that case, I'd think that something along option #2 would be your best bet. That way you're keeping with the non-direct access thing you've got going with MVC and whatnot.

Sorry if I shot too low with my explanation -- its tough to guage the experience level of an audience from a couple line post.
 
Rick Murphy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ended up using iframe. It seems to be fine for now. Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic