• Post Reply Bookmark Topic Watch Topic
  • New Topic

Get the URL of a JSP page from the calling servlet

 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use iText to create a PDF document. I found a nice tutorial online but one thing stumps me. How do I get the URL of the JSP page that contains the content? Since this will not be static, I don't want to hard code this. I am sure this is something simple but I am fairly new to servlets and JSP.



How do I get the url of the JSP page?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't find it because it's irrelevant. Most often -- at least in properly formed web applications -- the JSP rendering is as a result of a forward to the JSP. The name of the JSP is irrelevant to everything else.

So the question is: why try to use the name of the JSP? Anything you do should be independent of the inconsequential file name.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Search term: strong binding
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpServletRequest#getRequestURL()?

Or if you just want to the part before the query string then getRequestURI()

Edit: as Bear mentioned, if the page is forward, this would not work (getting the servlet name that did the forward instead)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote: if you just want to the part before the query string then getRequestURI()

Sorry, but I think is very bad advice. The name of the file, or URL, or servlet, or whatever, is irrelevant, and trying to use it in a business sense is an example of strong binding that is almost guaranteed to create problems for future refactoring. Don't do it.
 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The page is rendered. I am using JQGrid along with XML to build and display a table.

So I suppose my best bet would be to simply call the database and build a table into the the PDF document. Would this make the most sense then?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what that has to do with the original question of grabbing the name of the JSP.
 
Anindya Roy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no it's not a good idea or it's not possible also most of the time to retrieve the jsp page name. Because if you see the URL in the browser it's all about how the URL pattern was mapped in deployment descriptor file - web.xml. sometimes you never know the actual URL in the browser. So it's really difficult to get the page name.
 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:I'm not sure what that has to do with the original question of grabbing the name of the JSP.


I am creating a viewable report that is displayed on a webpage in a table via JQGrid. A few of the fields are editable but I suppose that does'nt really matter. What I would like to do is create the functionality to export this to PDF and I would like it to look much like the webpage. I wasn't at first thinking about what happens if there are more rows than what is displayed on the page and the next button needs to be clicked.

So that is why I said that. I guess it really doesn't have anything to do with the original question but it is still related to the problem I am trying to solve.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not try to capture the rendered JSP as the PDF, but use iText (or something) to create the PDF from the data.
 
Raymond Gillespie
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was my second thought. I have been looking at iText tryibg to make sense of and it and figure out the best method to do this.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!